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: add basic pubsub #1277

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export function throwIfInSubscriberMode(commandName, RedisMock) {
'psubscribeBuffer',
'unsubscribe',
'unsubscribeBuffer',
'pubsub',
'pubsubBuffer',
'punsubscribe',
'punsubscribeBuffer',
'ping',
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export * from './psetex'
export * from './psubscribe'
export * from './pttl'
export * from './publish'
export * from './pubsub'
export * from './punsubscribe'
export * from './quit'
export * from './randomkey'
Expand Down
24 changes: 24 additions & 0 deletions src/commands/pubsub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import patternMatchesString from '../commands-utils/patternMatchesString'

export function pubsub(subCommand, pattern) {
switch (subCommand) {
case 'CHANNELS': {
let channels = []

this.channels?.instanceListeners?.forEach((instanceMap, channel) => {
channels.push(channel)
})

if (pattern) {
channels = channels.filter(x => patternMatchesString(pattern, x))
}

return channels
}
default: {
throw new Error('Currently not implemented as a mock')
}
}
}

export const pubsubBuffer = pubsub
57 changes: 57 additions & 0 deletions test/integration/commands/pubsub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Redis from 'ioredis'

// eslint-disable-next-line import/no-relative-parent-imports
import { runTwinSuite } from '../../../test-utils'

const testChannels = ['emails', 'messages', 'feed']

runTwinSuite('pubsub', command => {
// @TODO Rewrite test suite so it runs on a real Redis instance
;(process.env.IS_E2E ? describe.skip : describe)(command, () => {
describe('CHANNELS', () => {
let redis

beforeEach(() => {
redis = new Redis({
host: 'pubsub',
})
})

afterEach(async () => {
await Promise.all(testChannels.map(x => redis.unsubscribe(x)))
})

test('should return 0 when publishing without subscribers', async () => {
expect(await redis[command]('CHANNELS')).toMatchObject([])
})

it('should return the single active channel', async () => {
redis.subscribe('emails')

expect(await redis[command]('CHANNELS')).toMatchObject(['emails'])
})

it('should return multiple active channels', async () => {
redis.subscribe('emails')
redis.subscribe('messages')
redis.subscribe('feed')

expect(await redis[command]('CHANNELS')).toMatchObject([
'emails',
'messages',
'feed',
])
})

it('should return filtered channels', async () => {
redis.subscribe('emails')
redis.subscribe('messages')
redis.subscribe('feed')

expect(await redis[command]('CHANNELS', 'email*')).toMatchObject([
'emails',
])
})
})
})
})