|
| 1 | +import * as Redis from 'ioredis'; |
| 2 | +import { RedisClient } from 'src/modules/pub-sub/model/redis-client'; |
| 3 | +import { RedisClientEvents, RedisClientStatus } from 'src/modules/pub-sub/constants'; |
| 4 | + |
| 5 | +const getRedisClientFn = jest.fn(); |
| 6 | + |
| 7 | +const nodeClient = Object.create(Redis.prototype); |
| 8 | +nodeClient.subscribe = jest.fn(); |
| 9 | +nodeClient.psubscribe = jest.fn(); |
| 10 | +nodeClient.unsubscribe = jest.fn(); |
| 11 | +nodeClient.punsubscribe = jest.fn(); |
| 12 | +nodeClient.status = 'ready'; |
| 13 | +nodeClient.disconnect = jest.fn(); |
| 14 | + |
| 15 | +describe('RedisClient', () => { |
| 16 | + let redisClient: RedisClient; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + jest.resetAllMocks(); |
| 20 | + redisClient = new RedisClient('databaseId', getRedisClientFn); |
| 21 | + getRedisClientFn.mockResolvedValue(nodeClient); |
| 22 | + nodeClient.subscribe.mockResolvedValue('OK'); |
| 23 | + nodeClient.psubscribe.mockResolvedValue('OK'); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('getClient', () => { |
| 27 | + let connectSpy; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + connectSpy = jest.spyOn(redisClient as any, 'connect'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should connect and return client by default', async () => { |
| 34 | + expect(await redisClient.getClient()).toEqual(nodeClient); |
| 35 | + expect(connectSpy).toHaveBeenCalledTimes(1); |
| 36 | + expect(redisClient['status']).toEqual(RedisClientStatus.Connected); |
| 37 | + }); |
| 38 | + it('should wait until first attempt of connection finish with success', async () => { |
| 39 | + redisClient.getClient().then().catch(); |
| 40 | + expect(redisClient['status']).toEqual(RedisClientStatus.Connecting); |
| 41 | + expect(await redisClient.getClient()).toEqual(nodeClient); |
| 42 | + expect(connectSpy).toHaveBeenCalledTimes(1); |
| 43 | + expect(redisClient['status']).toEqual(RedisClientStatus.Connected); |
| 44 | + }); |
| 45 | + it('should wait until first attempt of connection finish with error', async () => { |
| 46 | + try { |
| 47 | + getRedisClientFn.mockRejectedValueOnce(new Error('Connection error')); |
| 48 | + redisClient.getClient().then().catch(); |
| 49 | + expect(redisClient['status']).toEqual(RedisClientStatus.Connecting); |
| 50 | + expect(await redisClient.getClient()).toEqual(nodeClient); |
| 51 | + fail(); |
| 52 | + } catch (e) { |
| 53 | + expect(connectSpy).toHaveBeenCalledTimes(1); |
| 54 | + expect(redisClient['status']).toEqual(RedisClientStatus.Error); |
| 55 | + } |
| 56 | + }); |
| 57 | + it('should return existing connection when status connected', async () => { |
| 58 | + expect(await redisClient.getClient()).toEqual(nodeClient); |
| 59 | + expect(connectSpy).toHaveBeenCalledTimes(1); |
| 60 | + expect(redisClient['status']).toEqual(RedisClientStatus.Connected); |
| 61 | + expect(await redisClient.getClient()).toEqual(nodeClient); |
| 62 | + expect(connectSpy).toHaveBeenCalledTimes(1); |
| 63 | + }); |
| 64 | + it('should return create new connection when status end or error', async () => { |
| 65 | + expect(await redisClient.getClient()).toEqual(nodeClient); |
| 66 | + expect(connectSpy).toHaveBeenCalledTimes(1); |
| 67 | + expect(redisClient['status']).toEqual(RedisClientStatus.Connected); |
| 68 | + redisClient['status'] = RedisClientStatus.Error; |
| 69 | + expect(await redisClient.getClient()).toEqual(nodeClient); |
| 70 | + expect(connectSpy).toHaveBeenCalledTimes(2); |
| 71 | + expect(redisClient['status']).toEqual(RedisClientStatus.Connected); |
| 72 | + redisClient['status'] = RedisClientStatus.End; |
| 73 | + expect(await redisClient.getClient()).toEqual(nodeClient); |
| 74 | + expect(connectSpy).toHaveBeenCalledTimes(3); |
| 75 | + expect(redisClient['status']).toEqual(RedisClientStatus.Connected); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('connect', () => { |
| 80 | + it('should connect and emit connected event', async () => { |
| 81 | + expect(await new Promise((res) => { |
| 82 | + redisClient['connect'](); |
| 83 | + redisClient.on(RedisClientEvents.Connected, res); |
| 84 | + })).toEqual(nodeClient); |
| 85 | + }); |
| 86 | + it('should emit message event (message source)', async () => { |
| 87 | + await redisClient['connect'](); |
| 88 | + const [id, message] = await new Promise((res) => { |
| 89 | + redisClient.on('message', (i, m) => res([i, m])); |
| 90 | + nodeClient.emit('message', 'channel-a', 'message-a'); |
| 91 | + }); |
| 92 | + |
| 93 | + expect(id).toEqual('s:channel-a'); |
| 94 | + expect(message.channel).toEqual('channel-a'); |
| 95 | + expect(message.message).toEqual('message-a'); |
| 96 | + }); |
| 97 | + it('should emit message event (pmessage source)', async () => { |
| 98 | + await redisClient['connect'](); |
| 99 | + const [id, message] = await new Promise((res) => { |
| 100 | + redisClient.on('message', (i, m) => res([i, m])); |
| 101 | + nodeClient.emit('pmessage', '*', 'channel-aa', 'message-aa'); |
| 102 | + }); |
| 103 | + expect(id).toEqual('p:*'); |
| 104 | + expect(message.channel).toEqual('channel-aa'); |
| 105 | + expect(message.message).toEqual('message-aa'); |
| 106 | + }); |
| 107 | + it('should emit end event', async () => { |
| 108 | + await redisClient['connect'](); |
| 109 | + await new Promise((res) => { |
| 110 | + redisClient.on('end', () => { |
| 111 | + res(null); |
| 112 | + }); |
| 113 | + |
| 114 | + nodeClient.emit('end'); |
| 115 | + }); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe('destroy', () => { |
| 120 | + it('should remove all listeners, disconnect, set client to null and emit end event', async () => { |
| 121 | + const removeAllListenersSpy = jest.spyOn(nodeClient, 'removeAllListeners'); |
| 122 | + |
| 123 | + await redisClient['connect'](); |
| 124 | + redisClient.destroy(); |
| 125 | + |
| 126 | + expect(redisClient['client']).toEqual(null); |
| 127 | + expect(redisClient['status']).toEqual(RedisClientStatus.End); |
| 128 | + expect(removeAllListenersSpy).toHaveBeenCalled(); |
| 129 | + expect(nodeClient.disconnect).toHaveBeenCalled(); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments