Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Emits events on disconnect call #1353

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.nyc_output
.vscode
.idea
coverage
junit
lib
Expand Down
5 changes: 5 additions & 0 deletions src/commands-utils/emitDisconnectEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function emitConnectEvent(redisMock) {
process.nextTick(() => {
redisMock.emit('end')
})
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import createCommand, { Command } from './command'
import * as commands from './commands'
import * as commandsStream from './commands-stream'
import emitConnectEvent from './commands-utils/emitConnectEvent'
import emitDisconnectEvent from './commands-utils/emitDisconnectEvent'
import contextMap, { createContext } from './context'
import { createData } from './data'
import { createExpires } from './expires'
Expand Down Expand Up @@ -201,6 +202,8 @@ class RedisMock extends EventEmitter {

removeFrom(this.channels)
removeFrom(this.patternChannels)

emitDisconnectEvent(this)
// no-op
}

Expand Down
9 changes: 8 additions & 1 deletion test/integration/disconnect.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import Redis from 'ioredis'

describe('disconnect', () => {
it('should be available, but do nothing', () => {
it('should be available and emit "end" event', async () => {
const redis = new Redis({})
const events = []
redis.on('end', () => events.push('end'))

expect(redis.disconnect()).toBe(undefined)

await new Promise(resolve => setImmediate(resolve));

expect(events).toEqual(['end'])
})
})