Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions packages/client/lib/sentinel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ export default class RedisSentinel<
this._self.#reservedClientInfo = await this._self.#internal.getClientLease();
}

this.emit('connect');
this.emit('ready');

return this as unknown as RedisSentinelType<M, F, S, RESP, TYPE_MAPPING>;
}

Expand Down Expand Up @@ -537,11 +540,19 @@ export default class RedisSentinel<
multi = this.MULTI;

async close() {
return this._self.#internal.close();
const wasOpen = this._self.isOpen;
await this._self.#internal.close();
if (wasOpen) {
this.emit('end');
}
}

destroy() {
return this._self.#internal.destroy();
async destroy() {
const wasOpen = this._self.isOpen;
await this._self.#internal.destroy();
if (wasOpen) {
this.emit('end');
}
}

async SUBSCRIBE<T extends boolean = false>(
Expand Down
26 changes: 26 additions & 0 deletions packages/client/lib/sentinel/lifecycle-events.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { strict as assert } from 'node:assert';
import { once } from 'node:events';
import testUtils, { GLOBAL } from '../test-utils';

describe('RedisSentinel lifecycle events', () => {
testUtils.testWithClientSentinel('should emit connect, ready and end events', async sentinel => {
const events: string[] = [];

sentinel
.on('connect', () => events.push('connect'))
.on('ready', () => events.push('ready'))
.on('end', () => events.push('end'));

await sentinel.connect();
assert.deepEqual(events, ['connect', 'ready']);

const endPromise = once(sentinel, 'end');
await sentinel.close();
await endPromise;

assert.deepEqual(events, ['connect', 'ready', 'end']);
}, {
...GLOBAL.SENTINEL.OPEN,
disableClientSetup: true
});
});