successful tests with multiple rhizome nodes

This commit is contained in:
Ladd Hoffman 2024-12-25 17:24:18 -06:00
parent 7b590ee77e
commit a6f65ef99f
3 changed files with 98 additions and 23 deletions

View File

@ -1,30 +1,10 @@
import Debug from 'debug'; import {App} from "../../util/app";
import {RhizomeNode, RhizomeNodeConfig} from "../src/node";
import {TypedCollection} from '../src/typed-collection';
const debug = Debug('test:run');
type User = {
id?: string;
name: string;
nameLong?: string;
email?: string;
age: number;
};
class App extends RhizomeNode {
constructor(config?: Partial<RhizomeNodeConfig>) {
super(config);
const users = new TypedCollection<User>("users");
users.rhizomeConnect(this);
}
}
describe('Run', () => { describe('Run', () => {
let app: App; let app: App;
beforeAll(async () => { beforeAll(async () => {
app = new App({ app = new App({
// TODO expose more conveniently as test config options
httpPort: 5000, httpPort: 5000,
httpEnable: true, httpEnable: true,
requestBindPort: 5001, requestBindPort: 5001,
@ -34,12 +14,12 @@ describe('Run', () => {
}); });
afterAll(async () => { afterAll(async () => {
debug('attempting to stop app');
await app.stop(); await app.stop();
}); });
it('can put a new user', async () => { it('can put a new user', async () => {
const res = await fetch('http://localhost:5000/users', { const {httpAddr, httpPort} = app.config;
const res = await fetch(`http://${httpAddr}:${httpPort}/users`, {
method: 'PUT', method: 'PUT',
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ body: JSON.stringify({

View File

@ -0,0 +1,61 @@
import {App} from '../../util/app';
describe('Run', () => {
const apps: App[] = [];
beforeAll(async () => {
apps[0] = new App({
httpEnable: true,
});
apps[1] = new App({
httpEnable: true,
});
await Promise.all(apps.map((app) => app.start()));
});
afterAll(async () => {
await Promise.all(apps.map((app) => app.stop()));
});
it('can create a record on node 0 and read it on node 1', async () => {
const res = await fetch(`${apps[0].apiUrl}/users`, {
method: 'PUT',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
name: "Peon",
id: "peon-1",
age: 263
})
});
const data = await res.json();
expect(data).toMatchObject({
properties: {
name: "Peon",
id: "peon-1",
age: 263
}
});
await new Promise((resolve) => setTimeout(resolve, 500));
const res2 = await fetch(`${apps[0].apiUrl}/users`, {
method: 'PUT',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
name: "Peon",
id: "peon-1",
age: 263
})
});
const data2 = await res2.json();
expect(data2).toMatchObject({
properties: {
name: "Peon",
id: "peon-1",
age: 263
}
});
});
});

34
util/app.ts Normal file
View File

@ -0,0 +1,34 @@
import {RhizomeNode, RhizomeNodeConfig} from "../src/node";
import {TypedCollection} from "../src/typed-collection";
type User = {
id?: string;
name: string;
nameLong?: string;
email?: string;
age: number;
};
const start = 5000;
const range = 5000;
const getRandomPort = () => Math.floor(start + range * Math.random());
export class App extends RhizomeNode {
apiUrl: string;
constructor(config?: Partial<RhizomeNodeConfig>) {
// Randomizing ports to try to avoid collisions between tests.
super({
publishBindPort: getRandomPort(),
requestBindPort: getRandomPort(),
httpPort: getRandomPort(),
...config,
});
const users = new TypedCollection<User>("users");
users.rhizomeConnect(this);
this.apiUrl = `http://${this.config.httpAddr}:${this.config.httpPort}`;
}
}