|
| 1 | +// This test in only for documenting Redis client usage |
| 2 | + |
| 3 | +import { createClient } from 'redis'; |
| 4 | + |
| 5 | +const redisClient = createClient({ url: process.env.REDIS_URL || 'redis://localhost:6379' }); |
| 6 | + |
| 7 | +interface RedisStreamMessage { |
| 8 | + id: string; |
| 9 | + message: Record<string, string>; |
| 10 | +} |
| 11 | +interface RedisStreamResult { |
| 12 | + name: string; |
| 13 | + messages: RedisStreamMessage[]; |
| 14 | +} |
| 15 | + |
| 16 | +test('Redis client connects successfully', async () => { |
| 17 | + await redisClient.connect(); |
| 18 | + expect(redisClient.isOpen).toBe(true); |
| 19 | + await redisClient.quit(); |
| 20 | +}); |
| 21 | + |
| 22 | +test('calls connect after quit', async () => { |
| 23 | + await redisClient.connect(); |
| 24 | + expect(redisClient.isOpen).toBe(true); |
| 25 | + await redisClient.quit(); |
| 26 | + |
| 27 | + await redisClient.connect(); |
| 28 | + expect(redisClient.isOpen).toBe(true); |
| 29 | + await redisClient.quit(); |
| 30 | +}); |
| 31 | + |
| 32 | +test('calls quit before connect is resolved', async () => { |
| 33 | + const client = createClient({ url: process.env.REDIS_URL || 'redis://localhost:6379' }); |
| 34 | + const connectPromise = client.connect(); |
| 35 | + await client.quit(); |
| 36 | + await connectPromise; |
| 37 | + expect(client.isOpen).toBe(false); |
| 38 | +}); |
| 39 | + |
| 40 | +test('multiple connect calls', async () => { |
| 41 | + const client = createClient({ url: process.env.REDIS_URL || 'redis://localhost:6379' }); |
| 42 | + const connectPromise1 = client.connect(); |
| 43 | + const connectPromise2 = client.connect(); |
| 44 | + await expect(connectPromise2).rejects.toThrow('Socket already opened'); |
| 45 | + await expect(connectPromise1).resolves.toMatchObject({}); |
| 46 | + expect(client.isOpen).toBe(true); |
| 47 | + await client.quit(); |
| 48 | +}); |
| 49 | + |
| 50 | +test('write to stream and read back', async () => { |
| 51 | + const client = createClient({ url: process.env.REDIS_URL || 'redis://localhost:6379' }); |
| 52 | + await client.connect(); |
| 53 | + |
| 54 | + const streamKey = 'test-stream'; |
| 55 | + await client.del(streamKey); |
| 56 | + const messageId = await client.xAdd(streamKey, '*', { field1: 'value1' }); |
| 57 | + |
| 58 | + const result = (await client.xRead({ key: streamKey, id: '0-0' }, { COUNT: 1, BLOCK: 2000 })) as |
| 59 | + | RedisStreamResult[] |
| 60 | + | null; |
| 61 | + expect(result).not.toBeNull(); |
| 62 | + expect(result).toBeDefined(); |
| 63 | + |
| 64 | + const [stream] = result!; |
| 65 | + expect(stream).toBeDefined(); |
| 66 | + expect(stream?.messages.length).toBe(1); |
| 67 | + const [message] = stream!.messages; |
| 68 | + expect(message!.id).toBe(messageId); |
| 69 | + expect(message!.message).toEqual({ field1: 'value1' }); |
| 70 | + |
| 71 | + await client.quit(); |
| 72 | +}); |
| 73 | + |
| 74 | +test('quit while reading from stream', async () => { |
| 75 | + const client = createClient({ url: process.env.REDIS_URL || 'redis://localhost:6379' }); |
| 76 | + await client.connect(); |
| 77 | + |
| 78 | + const streamKey = 'test-stream-quit'; |
| 79 | + |
| 80 | + const readPromise = client.xRead({ key: streamKey, id: '$' }, { BLOCK: 0 }); |
| 81 | + |
| 82 | + // Wait a moment to ensure xRead is blocking |
| 83 | + await new Promise((resolve) => { |
| 84 | + setTimeout(resolve, 500); |
| 85 | + }); |
| 86 | + |
| 87 | + client.destroy(); |
| 88 | + |
| 89 | + await expect(readPromise).rejects.toThrow(); |
| 90 | +}); |
| 91 | + |
| 92 | +it('expire sets TTL on stream', async () => { |
| 93 | + const client = createClient({ url: process.env.REDIS_URL || 'redis://localhost:6379' }); |
| 94 | + await client.connect(); |
| 95 | + |
| 96 | + const streamKey = 'test-stream-expire'; |
| 97 | + await client.del(streamKey); |
| 98 | + await client.xAdd(streamKey, '*', { field1: 'value1' }); |
| 99 | + |
| 100 | + const expireResult = await client.expire(streamKey, 1); // 1 second |
| 101 | + expect(expireResult).toBe(1); // 1 means the key existed and TTL was set |
| 102 | + |
| 103 | + const ttl1 = await client.ttl(streamKey); |
| 104 | + expect(ttl1).toBeLessThanOrEqual(1); |
| 105 | + expect(ttl1).toBeGreaterThan(0); |
| 106 | + |
| 107 | + const existsBeforeTimeout = await client.exists(streamKey); |
| 108 | + expect(existsBeforeTimeout).toBe(1); // Key should exist before timeout |
| 109 | + |
| 110 | + // Wait for 1.1 seconds |
| 111 | + await new Promise((resolve) => { |
| 112 | + setTimeout(resolve, 1100); |
| 113 | + }); |
| 114 | + |
| 115 | + const existsAfterTimeout = await client.exists(streamKey); |
| 116 | + expect(existsAfterTimeout).toBe(0); // Key should have expired |
| 117 | + |
| 118 | + await client.quit(); |
| 119 | +}); |
0 commit comments