Skip to content

Commit

Permalink
fix(events): dispatch disconnected event on connection
Browse files Browse the repository at this point in the history
  • Loading branch information
tahubu committed Aug 2, 2021
1 parent 654d7a0 commit 5e2f21e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/service/amqp/amqp.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ describe('AMQPService', () => {
},
linkOptions: options,
})),
_connection: {
dispatch: () => {},
},
}));
});

Expand Down Expand Up @@ -162,7 +165,16 @@ describe('AMQPService', () => {

await AMQPService.createConnection({ connectionUri: connectionSecureUri });

connectionEvents.forEach(event => event.callback({ error: new Error('test') }));
connectionEvents.forEach(event =>
event.callback(
new EventContextMock({
error: new Error('test'),
connection: {
open: () => Promise.resolve({}),
},
}),
),
);

expect(connectionEvents.length).toBeGreaterThan(0);
});
Expand Down
2 changes: 1 addition & 1 deletion src/service/amqp/amqp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class AMQPService {
logger.error(`connection closed with error: ${context.error.name} - ${context.error.message}`, context.error.stack);

const timeoutHandler = setTimeout(async () => {
context.connection.emit(ConnectionEvents.disconnected);
(context.connection as any)._connection.dispatch(ConnectionEvents.disconnected, void 0);
await context.connection
.open()
.then(() => {
Expand Down
3 changes: 3 additions & 0 deletions src/test/event-context.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export class EventContextMock implements EventContext {
public connection: any = {
emit: jest.fn(),
open: jest.fn(),
_connection: {
dispatch: () => {},
},
};
public container: any = jest.fn();
public _context: any = jest.fn();
Expand Down

0 comments on commit 5e2f21e

Please sign in to comment.