Skip to content

Commit

Permalink
fix(services): Fix onServiceUnavailable not triggered if service regi…
Browse files Browse the repository at this point in the history
…stration is triggered after node is available
  • Loading branch information
iamyellow authored and aholstenson committed May 23, 2022
1 parent aaab2f5 commit 0ccf852
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/services/src/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ export class Services {
this.updateServiceReflect(existing, reflect);
} else {
// Register that the service is reachable through this node
data.services.set(id, reflect);
this.registerServiceReflect(reflect);
}

Expand Down
45 changes: 45 additions & 0 deletions packages/services/test/remote-calls.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ServiceContract, stringType } from 'ataraxia-service-contracts';
import { TestNetwork } from 'ataraxia/test';

import { ServiceHandle } from '../src/ServiceHandle';
import { Services } from '../src/Services';

interface TestService {
Expand Down Expand Up @@ -172,6 +173,50 @@ describe('Services: Remote Calls', () => {
await testNetwork.shutdown();
});

it('onServiceUnavailable triggers (delayed service registration)', async () => {
const testNetwork = new TestNetwork();
testNetwork.bidirectional('a', 'b');

const a = testNetwork.network('a');
const b = testNetwork.network('b');

const aServices = new Services(a);
const bServices = new Services(b);

await aServices.join();
await bServices.join();

let unavailableCount = 0;
bServices.onServiceUnavailable(s => {
unavailableCount++;
});

let handle: ServiceHandle | undefined;
await new Promise<void>(r => {
setTimeout(async () => {
handle = aServices.register('test', TestService.implement({
async hello(what: string) {
return 'Hello ' + what + '!';
}
}));

await testNetwork.consolidate();

r();
}, 500);
});

handle?.unregister();

await testNetwork.consolidate();

expect(unavailableCount).toBe(1);

await aServices.leave();
await bServices.leave();
await testNetwork.shutdown();
});

it('Service.onAvailable triggers', async () => {
const testNetwork = new TestNetwork();
testNetwork.bidirectional('a', 'b');
Expand Down

0 comments on commit 0ccf852

Please sign in to comment.