Skip to content

Commit

Permalink
fix(#71): properly clean internal states when schema is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Dec 12, 2023
1 parent 1ab8a77 commit 97be502
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/server/StateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ class StateManager extends BaseStateManager {
attached.transport.emit(`${DELETE_NOTIFICATION}-${state.id}-${remoteId}`);
}

this._serverStatesById.delete(this.id);
this._serverStatesById.delete(state.id);
}
}

Expand Down
37 changes: 31 additions & 6 deletions tests/states/StateManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ describe(`# StateManager`, () => {
});

describe('## registerSchema(schemaName, definition)', () => {
it('should register schema', () => {
it('should throw if reusing same schema name', () => {
assert.throws(() => {
server.stateManager.registerSchema('a', a);
}, Error, '[stateManager.registerSchema] cannot register schema with name: "a", schema name already exists');

});

it ('should register another schema reusing same definition', () => {
it ('should register same definition with another name', () => {
server.stateManager.registerSchema('aa', a);
});
});
Expand Down Expand Up @@ -185,7 +185,7 @@ describe(`# StateManager`, () => {
});

describe('## deleteSchema(name)', () => {
it.skip('[FIXME #72] should call state.onDetach and state.onDelete on all states of its kind', async () => {
it('should call state.onDetach and state.onDelete on all states of its kind', async () => {
server.stateManager.registerSchema('a-delete', a);

const a0 = await server.stateManager.create('a-delete');
Expand All @@ -212,6 +212,33 @@ describe(`# StateManager`, () => {
}, 200);
});
});

// cf. https://github.com/collective-soundworks/soundworks/issues/71
it(`should not propagate deleted schema in observe`, async () => {
server.stateManager.registerSchema('a-delete-observe', a);

const state = await client.stateManager.create('a-delete-observe');
let deleteCalled = false;;
// assert
state.onDelete(() => {
deleteCalled = true;
});

server.stateManager.deleteSchema('a-delete-observe');
await delay(100);

assert.equal(deleteCalled, true);

let observeCalled = false;
const unobserve = await server.stateManager.observe((schemaName, stateId) => {
observeCalled = true;
});

await delay(100); // is not needed as observe should await, but just to make sure

assert.equal(observeCalled, false);
unobserve();
});
});

describe('## observe([schemaName,] callback) => Promise<unobserve>', async () => {
Expand Down Expand Up @@ -403,7 +430,7 @@ describe(`# StateManager`, () => {
await state4.delete();
});

it.skip(`[FIXME #71] should properly behave with filtered schema name: observe(schemaName, callback)`, async () => {
it(`should properly behave with filtered schema name: observe(schemaName, callback)`, async () => {
const a1 = await client.stateManager.create('a');
const b1 = await client.stateManager.create('b');

Expand All @@ -414,15 +441,13 @@ describe(`# StateManager`, () => {
await other.start();

const unobserveStar = await other.stateManager.observe(async (schemaName, stateId) => {
console.log(schemaName);
starCalled += 1;
});

const unobserveFiltered = await other.stateManager.observe('a', async (schemaName, stateId) => {
filteredCalled += 1;
});

console.log(starCalled, filteredCalled);
assert.equal(starCalled, 2);
assert.equal(filteredCalled, 1);

Expand Down

0 comments on commit 97be502

Please sign in to comment.