Skip to content

Commit

Permalink
test: refactor and add todos
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Dec 14, 2023
1 parent 0788387 commit 6cf3170
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 37 deletions.
32 changes: 32 additions & 0 deletions tests/states/StateCollection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,39 @@ describe(`# SharedStateCollection`, () => {
await state0.delete();
await state1.delete();
await collection.detach();
});

it.skip(`test several collections from same schema`, async () => {});
it.skip(`test socket message numbers`, async () => {});

it(`"normal" state communication should work as expected`, async () => {
const state0 = await clients[0].stateManager.create('a');
const state1 = await clients[1].stateManager.create('a');
// cross attached states
const attached0 = await clients[1].stateManager.attach('a', state0.id);
const attached1 = await clients[0].stateManager.attach('a', state1.id);

const collection = await clients[2].stateManager.getCollection('a');

let onUpdateCalled = false;
collection.onUpdate((state, updates) => {
onUpdateCalled = true;
assert.equal(state.id, state0.id);
assert.deepEqual(updates, { bool: true });
});

await state0.set({ bool: true });
await delay(50);
// should be propagated to everyone
assert.equal(state0.get('bool'), true);
assert.equal(state1.get('bool'), false);
assert.equal(attached0.get('bool'), true);
assert.equal(attached1.get('bool'), false);
assert.isTrue(onUpdateCalled);

await state0.delete();
await state1.delete();
await collection.detach();
});
});

Expand Down
70 changes: 33 additions & 37 deletions tests/states/stress-tests.js → tests/states/stress-tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,17 @@ describe('# SharedStates - Stress test', () => {
}

client = clients[0];

console.log(`
> created ${numClients} clients
`);
});

afterEach(async function() {
server.stop();
});

describe('WebSocket transport', () => {
describe('## WebSocket transport', () => {
it('should work properly with async transport - brute force testing', async function() {
this.timeout(5 * 1000);

console.time(' + brute force time');
console.time(' + brute force time');
const global = await server.stateManager.create('a');
const attached = [];

Expand Down Expand Up @@ -132,7 +128,7 @@ describe('# SharedStates - Stress test', () => {
}

await global.detach();
console.timeEnd(' + brute force time');
console.timeEnd(' + brute force time');
// wait for message propagation
await delay(100);

Expand Down Expand Up @@ -232,57 +228,54 @@ describe('# SharedStates - Stress test', () => {
});
});

describe.skip(`## [should fail...] stateManager quick requests`, async () => {
['server', 'client'].forEach(s => {
it(`${s} should be able to quickly create states (await)`, async () => {
const source = s === 'server' ? server : client;
describe(`## stateManager quick requests`, async () => {
['server', 'client'].forEach(sourceName => {
it(`${sourceName} should be able to quickly create states (await)`, async () => {
const source = sourceName === 'server' ? server : client;
const states = [];

for (let s = 0; s < 50; ++s) {
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Passing a string `s` should throw an error
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const state = await source.stateManager.create('a', { int: s });
for (let i = 0; i < 50; ++i) {
const state = await source.stateManager.create('a', { int: i });
states.push(state);
}

assert.equal(states.length, 50);
});

it(`${s} should be able to quickly create states (no await)`, async () => {
const source = s === 'server' ? server : client;
it(`${sourceName} should be able to quickly create states (no await)`, async () => {
const source = sourceName === 'server' ? server : client;
const states = [];

for (let s = 0; s < 50; ++s) {
for (let i = 0; i < 50; ++i) {
// no await
const state = source.stateManager.create('a', { int: s });
const state = source.stateManager.create('a', { int: i });
states.push(state);
}
await Promise.all(states);
});

it(`${s} should be able to quickly create states (await), then delete them (await)`, async () => {
const source = s === 'server' ? server : client;
it(`${sourceName} should be able to quickly create states (await), then delete them (await)`, async () => {
const source = sourceName === 'server' ? server : client;
const states = [];

for (let s = 0; s < 50; ++s) {
const state = await source.stateManager.create('a', { int: s });
for (let i = 0; i < 50; ++i) {
const state = await source.stateManager.create('a', { int: i });
states.push(state);
}

states.forEach( async (state) => {
// warning: `forEach` is not asynchronous
for (let state of states) {
await state.delete();
});

}
});

it(`${s} should be able to quickly create states (no await), then delete them (no await)`, async () => {
const source = s === 'server' ? server : client;
it(`${sourceName} should be able to quickly create states (no await), then delete them (no await)`, async () => {
const source = sourceName === 'server' ? server : client;
const states = [];

for (let s = 0; s < 50; ++s) {
for (let i = 0; i < 50; ++i) {
// no await
const state = source.stateManager.create('a', { int: s });
const state = source.stateManager.create('a', { int: i });
states.push(state);
}

Expand All @@ -295,13 +288,13 @@ describe('# SharedStates - Stress test', () => {
});

// client seem to fail except on last run
it(`again, ${s} should be able to quickly create states (no await), then delete them (no await)`, async () => {
const source = s === 'server' ? server : client;
it(`again, ${sourceName} should be able to quickly create states (no await), then delete them (no await)`, async () => {
const source = sourceName === 'server' ? server : client;
const states = [];

for (let s = 0; s < 50; ++s) {
for (let i = 0; i < 50; ++i) {
// no await
const state = source.stateManager.create('a', { int: s });
const state = source.stateManager.create('a', { int: i });
states.push(state);
}

Expand All @@ -312,7 +305,10 @@ describe('# SharedStates - Stress test', () => {

await Promise.all(deletions);
});
}); // source: server or client
}); // quick state create and deletion
});
});

describe(`## StateCollection`, () => {
it.skip(`brute force test`, async () => {})
});
});

0 comments on commit 6cf3170

Please sign in to comment.