|
| 1 | +import { EventEmitter } from 'node:events' |
| 2 | +import { redisConfigMockFns } from '@sim/testing/mocks/redis-config.mock' |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +const { clients } = vi.hoisted(() => ({ |
| 6 | + clients: [] as Array<EventEmitter & { subscribed: Array<(err: Error | null) => void> }>, |
| 7 | +})) |
| 8 | + |
| 9 | +vi.mock('ioredis', () => ({ |
| 10 | + default: class extends EventEmitter { |
| 11 | + subscribed: Array<(err: Error | null) => void> = [] |
| 12 | + |
| 13 | + constructor() { |
| 14 | + super() |
| 15 | + clients.push(this) |
| 16 | + } |
| 17 | + |
| 18 | + subscribe(_channel: string, done: (err: Error | null) => void) { |
| 19 | + this.subscribed.push(done) |
| 20 | + } |
| 21 | + |
| 22 | + publish = vi.fn(async () => 1) |
| 23 | + unsubscribe = vi.fn(async () => undefined) |
| 24 | + quit = vi.fn(async () => 'OK') |
| 25 | + }, |
| 26 | +})) |
| 27 | + |
| 28 | +import { createPubSubChannel } from '@/lib/events/pubsub' |
| 29 | + |
| 30 | +/** Whether the promise has settled by the time already-queued work runs. */ |
| 31 | +async function settled(promise: Promise<void>): Promise<boolean> { |
| 32 | + return Promise.race([promise.then(() => true), Promise.resolve().then(() => false)]) |
| 33 | +} |
| 34 | + |
| 35 | +/** The subscriber connection: the channel opens its publisher first. */ |
| 36 | +function subscriber() { |
| 37 | + const client = clients.at(-1) |
| 38 | + if (!client) throw new Error('no Redis client') |
| 39 | + return client |
| 40 | +} |
| 41 | + |
| 42 | +describe('createPubSubChannel over Redis', () => { |
| 43 | + beforeEach(() => { |
| 44 | + clients.length = 0 |
| 45 | + redisConfigMockFns.mockGetConfiguredRedisUrl.mockReturnValue('redis://localhost:6379') |
| 46 | + }) |
| 47 | + |
| 48 | + it('is ready only once its connection has subscribed', async () => { |
| 49 | + const channel = createPubSubChannel({ channel: 'test', label: 'Test' }) |
| 50 | + expect(await settled(channel.ready())).toBe(false) |
| 51 | + |
| 52 | + subscriber().emit('ready') |
| 53 | + expect(await settled(channel.ready())).toBe(false) |
| 54 | + |
| 55 | + subscriber().subscribed[0](null) |
| 56 | + expect(await settled(channel.ready())).toBe(true) |
| 57 | + }) |
| 58 | + |
| 59 | + it('is not ready again until a dropped connection has resubscribed', async () => { |
| 60 | + const channel = createPubSubChannel({ channel: 'test', label: 'Test' }) |
| 61 | + subscriber().emit('ready') |
| 62 | + subscriber().subscribed[0](null) |
| 63 | + |
| 64 | + subscriber().emit('close') |
| 65 | + expect(await settled(channel.ready())).toBe(false) |
| 66 | + |
| 67 | + subscriber().emit('ready') |
| 68 | + subscriber().subscribed[1](null) |
| 69 | + expect(await settled(channel.ready())).toBe(true) |
| 70 | + }) |
| 71 | + |
| 72 | + it('is not ready while its subscribe fails, and retries on the next connection', async () => { |
| 73 | + const channel = createPubSubChannel({ channel: 'test', label: 'Test' }) |
| 74 | + subscriber().emit('ready') |
| 75 | + |
| 76 | + subscriber().subscribed[0](new Error('NOPERM')) |
| 77 | + expect(await settled(channel.ready())).toBe(false) |
| 78 | + |
| 79 | + subscriber().emit('close') |
| 80 | + subscriber().emit('ready') |
| 81 | + subscriber().subscribed[1](null) |
| 82 | + expect(await settled(channel.ready())).toBe(true) |
| 83 | + }) |
| 84 | + |
| 85 | + it('ignores a subscribe answered after its connection closed', async () => { |
| 86 | + const channel = createPubSubChannel({ channel: 'test', label: 'Test' }) |
| 87 | + subscriber().emit('ready') |
| 88 | + |
| 89 | + subscriber().emit('close') |
| 90 | + subscriber().subscribed[0](null) |
| 91 | + expect(await settled(channel.ready())).toBe(false) |
| 92 | + |
| 93 | + subscriber().emit('ready') |
| 94 | + subscriber().subscribed[1](null) |
| 95 | + expect(await settled(channel.ready())).toBe(true) |
| 96 | + }) |
| 97 | +}) |
0 commit comments