Skip to content

Commit 0e4e802

Browse files
author
Jan Rocek
committed
fix(microservices): prevent error logs during redis client shutdown
1 parent 6fe958d commit 0e4e802

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

packages/microservices/client/client-redis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ export class ClientRedis extends ClientProxy<RedisEvents, RedisStatus> {
5454
}
5555

5656
public async close() {
57+
this.isManuallyClosed = true;
5758
this.pubClient && (await this.pubClient.quit());
5859
this.subClient && (await this.subClient.quit());
5960
this.pubClient = this.subClient = null;
60-
this.isManuallyClosed = true;
6161
this.pendingEventListeners = [];
6262
}
6363

packages/microservices/test/client/client-redis.spec.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,12 @@ describe('ClientRedis', () => {
190190
beforeEach(() => {
191191
pubClose = sinon.spy();
192192
subClose = sinon.spy();
193-
pub = { quit: pubClose };
194-
sub = { quit: subClose };
193+
pub = {
194+
quit: pubClose,
195+
};
196+
sub = {
197+
quit: subClose,
198+
};
195199
untypedClient.pubClient = pub;
196200
untypedClient.subClient = sub;
197201
});
@@ -213,6 +217,36 @@ describe('ClientRedis', () => {
213217
await client.close();
214218
expect(subClose.called).to.be.false;
215219
});
220+
it.only('should have isManuallyClosed set to true when "end" event is handled during close', async () => {
221+
let endHandler: Function | undefined;
222+
sub.on = (event, handler) => {
223+
if (event === 'end') endHandler = handler;
224+
};
225+
sub.quit = async () => {
226+
if (endHandler) {
227+
endHandler();
228+
expect(untypedClient.isManuallyClosed).to.be.true;
229+
}
230+
};
231+
client.registerEndListener(sub);
232+
await client.close();
233+
});
234+
235+
it.only('should not log error when "end" event is handled during close', async () => {
236+
let endHandler: Function | undefined;
237+
const logError = sinon.spy(untypedClient.logger, 'error');
238+
sub.on = (event, handler) => {
239+
if (event === 'end') endHandler = handler;
240+
};
241+
sub.quit = async () => {
242+
if (endHandler) {
243+
endHandler();
244+
}
245+
};
246+
client.registerEndListener(sub);
247+
await client.close();
248+
expect(logError.called).to.be.false;
249+
});
216250
});
217251
describe('connect', () => {
218252
let createClientSpy: sinon.SinonSpy;

0 commit comments

Comments
 (0)