Skip to content

Commit

Permalink
test: RedisClient
Browse files Browse the repository at this point in the history
  • Loading branch information
morgsmccauley committed Aug 17, 2023
1 parent 213e252 commit e5a6b31
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions runner/src/redis-client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './redis-client';
114 changes: 114 additions & 0 deletions runner/src/redis-client/redis-client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import RedisClient from './redis-client';

describe('RedisClient', () => {
describe('getNextStreamMessage', () => {
it('returns the first message when none processed', async () => {
const mockClient = {
on: jest.fn(),
connect: jest.fn().mockResolvedValue(null),
get: jest.fn().mockResolvedValue(null),
xRead: jest.fn().mockResolvedValue(null),
} as any;

const client = new RedisClient(mockClient);

const message = await client.getNextStreamMessage('streamKey');

expect(mockClient.get).toHaveBeenCalledWith('streamKey:lastId');
expect(mockClient.xRead).toHaveBeenCalledWith(
{ key: 'streamKey', id: '0' },
{ COUNT: 1 }
);
expect(message).toBeUndefined();
});

it('returns the message after the last processed', async () => {
const mockClient = {
on: jest.fn(),
connect: jest.fn().mockResolvedValue(null),
get: jest.fn().mockResolvedValue('0-0'),
xRead: jest.fn().mockResolvedValue([
{
messages: ['data']
}
]),
} as any;

const client = new RedisClient(mockClient);

const message = await client.getNextStreamMessage('streamKey');

expect(mockClient.get).toHaveBeenCalledWith('streamKey:lastId');
expect(mockClient.xRead).toHaveBeenCalledWith(
{ key: 'streamKey', id: '0-0' },
{ COUNT: 1 }
);
expect(message).toStrictEqual(['data']);
});
});

it('acknowledges the stream message', async () => {
const mockClient = {
on: jest.fn(),
connect: jest.fn().mockResolvedValue(null),
set: jest.fn().mockResolvedValue(null),
} as any;

const client = new RedisClient(mockClient);

await client.acknowledgeStreamMessage('streamKey', '1-1');

expect(mockClient.set).toHaveBeenCalledWith('streamKey:lastId', '1-1');
});

it('returns the range of messages after the passed id', async () => {
const mockClient = {
on: jest.fn(),
connect: jest.fn().mockResolvedValue(null),
xRange: jest.fn().mockResolvedValue([
'data'
]),
get: jest.fn().mockResolvedValue('1-0'),
} as any;

const client = new RedisClient(mockClient);

const unprocessedMessages = await client.getUnprocessedStreamMessages('streamKey');

expect(mockClient.get).toHaveBeenCalledWith('streamKey:lastId');
expect(mockClient.xRange).toHaveBeenCalledWith('streamKey', '1-1', '+');
expect(unprocessedMessages).toEqual([
'data'
]);
});

it('returns stream storage data', async () => {
const mockClient = {
on: jest.fn(),
connect: jest.fn().mockResolvedValue(null),
get: jest.fn().mockResolvedValue(JSON.stringify({ account_id: '123', function_name: 'testFunc' })),
} as any;

const client = new RedisClient(mockClient);

const storageData = await client.getStreamStorage('streamKey');

expect(mockClient.get).toHaveBeenCalledWith('streamKey:storage');
expect(storageData).toEqual({ account_id: '123', function_name: 'testFunc' });
});

it('returns the list of streams', async () => {
const mockClient = {
on: jest.fn(),
connect: jest.fn().mockResolvedValue(null),
sMembers: jest.fn().mockResolvedValue(['streamKey1', 'streamKey2']),
} as any;

const client = new RedisClient(mockClient);

const streams = await client.getStreams();

expect(mockClient.sMembers).toHaveBeenCalledWith('streams');
expect(streams).toEqual(['streamKey1', 'streamKey2']);
});
});
File renamed without changes.

0 comments on commit e5a6b31

Please sign in to comment.