Skip to content

Commit 0b91bb4

Browse files
committed
fix(microservices): ensure all the amqp connections are closed properly
1 parent 34b6c2d commit 0b91bb4

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

packages/microservices/server/server-rmq.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ export class ServerRMQ extends Server<RmqEvents, RmqStatus> {
100100
}
101101
}
102102

103-
public close(): void {
104-
this.channel && this.channel.close();
105-
this.server && this.server.close();
103+
public async close(): Promise<void> {
104+
this.channel && (await this.channel.close());
105+
this.server && (await this.server.close());
106106
this.pendingEventListeners = [];
107107
}
108108

@@ -135,7 +135,7 @@ export class ServerRMQ extends Server<RmqEvents, RmqStatus> {
135135
this.pendingEventListeners = [];
136136

137137
const connectFailedEvent = 'connectFailed';
138-
this.server!.once(connectFailedEvent, (error: Record<string, unknown>) => {
138+
this.server!.once(connectFailedEvent, async (error: Record<string, unknown>) => {
139139
this._status$.next(RmqStatus.DISCONNECTED);
140140

141141
this.logger.error(CONNECTION_FAILED_MESSAGE);
@@ -150,7 +150,7 @@ export class ServerRMQ extends Server<RmqEvents, RmqStatus> {
150150
return;
151151
}
152152
if (++this.connectionAttempts === maxConnectionAttempts) {
153-
this.close();
153+
await this.close();
154154
callback?.(error.err ?? new Error(CONNECTION_FAILED_MESSAGE));
155155
}
156156
});

packages/microservices/test/server/server-rmq.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ describe('ServerRMQ', () => {
7878
untypedServer.server = rmqServer;
7979
untypedServer.channel = rmqChannel;
8080
});
81-
it('should close server', () => {
82-
server.close();
83-
expect(rmqServer.close.called).to.be.true;
81+
it('should close server', async () => {
82+
await server.close();
83+
expect(rmqServer.close.calledOnce).to.be.true;
8484
});
85-
it('should close channel', () => {
86-
server.close();
87-
expect(rmqChannel.close.called).to.be.true;
85+
it('should close channel', async () => {
86+
await server.close();
87+
expect(rmqChannel.close.calledOnce).to.be.true;
8888
});
8989
});
9090

0 commit comments

Comments
 (0)