Skip to content

Commit

Permalink
fix: wait the server to be closed when calling agent.close (#1107)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghusse committed Apr 15, 2024
1 parent 0422c1f commit cc44b7e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
17 changes: 13 additions & 4 deletions packages/agent/src/framework-mounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,25 @@ export default class FrameworkMounter {
'Info',
`Successfully mounted on Standalone server (http://${host ?? '0.0.0.0'}:${port})`,
);

this.onStop.push(async () => {
await new Promise<void>((resolveStop, rejectStop) => {
server.close((err: any) => {
if (err) {
rejectStop(err);
} else {
resolveStop();
}
});
});
});

resolve();
});
server.on('error', reject);
});
});

this.onStop.push(async () => {
server.close();
});

return this;
}

Expand Down
34 changes: 18 additions & 16 deletions packages/agent/test/framework-mounter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ describe('Builder > Agent', () => {
describe('standalone mode', () => {
describe('when starting the agent is a success', () => {
it('should send a response', async () => {
expect.assertions(1);

const mounter = new FrameworkMounter('my-api', logger);
mounter.mountOnStandaloneServer(9997, 'localhost');

Expand All @@ -45,34 +43,38 @@ describe('Builder > Agent', () => {

describe('when the agent is not started', () => {
it('should throw an error', async () => {
expect.assertions(1);
const mounter = new FrameworkMounter('my-api', logger);
mounter.mountOnStandaloneServer(9994, 'localhost');

await expect(() =>
superagent.get('http://localhost:9994/my-api/forest').timeout(100),
).rejects.toThrow();
});

it('should not throw an error to call stop', async () => {
expect.assertions(0);
const mounter = new FrameworkMounter('my-api', logger);
mounter.mountOnStandaloneServer(9994, 'localhost');

await expect(() => superagent.get('http://localhost:9994/my-api/forest')).rejects.toThrow();
await mounter.stop();
});
});

describe('when starting the agent fails', () => {
it('should throw an error catchable', async () => {
expect.assertions(1);

const mounter = new FrameworkMounter('my-api', logger);
const mounter2 = new FrameworkMounter('my-api', logger);

try {
mounter.mountOnStandaloneServer(9997, 'localhost');
// @ts-expect-error: testing a protected method
await mounter.mount(router);
// throw error on start by mount two servers on the same port
mounter2.mountOnStandaloneServer(9997, 'localhost');
// @ts-expect-error: testing a protected method
await mounter2.mount(router);
} catch (e) {
// eslint-disable-next-line jest/no-conditional-expect
expect(e.message).toContain('9997');
await expect(async () => {
mounter.mountOnStandaloneServer(9997, 'localhost');
// @ts-expect-error: testing a protected method
await mounter.mount(router);
// throw error on start by mount two servers on the same port
mounter2.mountOnStandaloneServer(9997, 'localhost');
// @ts-expect-error: testing a protected method
await mounter2.mount(router);
}).rejects.toThrow('9997');
} finally {
await mounter.stop();
await mounter2.stop();
Expand Down

0 comments on commit cc44b7e

Please sign in to comment.