Skip to content

Commit

Permalink
Add some unit tests for QueueSubjectListener
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrjoh committed Aug 26, 2024
1 parent c7153b0 commit 64a3b50
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions test/QueueSubjectListener.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {Queue} from '../src';
import {QueueSubjectListener} from '../src/queue/QueueSubjectListener';

const messages = [
{
Body: JSON.stringify({
Subject: 'test',
Message: JSON.stringify({id: '123', test: 'test'}),
}),
ReceiptHandle: 'test',
},
];

describe('QueueSubjectListener', () => {
describe('listen', () => {
it('should be able to listen to queue and call handler', async () => {
const queueMock = {
receiveMessage: jest.fn().mockResolvedValueOnce({
Messages: messages,
}),
deleteMessage: jest.fn(Promise.resolve),
} as unknown as Queue;

const sut = new QueueSubjectListener(queueMock, null, {
maxConcurrentMessage: 1,
waitTimeSeconds: 0,
visibilityTimeout: 0,
receiveTimeout: () => 0,
});

const handler = jest.fn(() => Promise.resolve());

sut.onSubject('test', handler);
sut.listen();

await new Promise(resolve => setTimeout(resolve, 10));
sut.stop();

expect(handler).toHaveBeenCalledTimes(1);
expect(queueMock.deleteMessage).toHaveBeenCalledTimes(1);
});

it('should be able to listen to queue and call handler with retry', async () => {
const queueMock = {
receiveMessage: jest.fn().mockResolvedValueOnce({
Messages: messages,
}),
deleteMessage: jest.fn(Promise.resolve),
changeMessageVisibility: jest.fn(Promise.resolve),
} as unknown as Queue;

const sut = new QueueSubjectListener(queueMock, null, {
maxConcurrentMessage: 1,
waitTimeSeconds: 0,
visibilityTimeout: 0,
receiveTimeout: () => 0,
});

const handler = jest.fn(() => Promise.reject('error'));

sut.onSubject('test', handler, {maxAttempts: 2, backoffDelay: 1});
sut.listen();

await new Promise(resolve => setTimeout(resolve, 10));
sut.stop();

expect(handler).toHaveBeenCalledTimes(1);
expect(queueMock.deleteMessage).not.toHaveBeenCalled();
expect(queueMock.changeMessageVisibility).toHaveBeenCalledTimes(1);
});

it('should not retry when multiple handlers are registered', async () => {
const queueMock = {
receiveMessage: jest.fn().mockResolvedValueOnce({
Messages: messages,
}),
deleteMessage: jest.fn(Promise.resolve),
} as unknown as Queue;

const sut = new QueueSubjectListener(queueMock, null, {
maxConcurrentMessage: 1,
waitTimeSeconds: 0,
visibilityTimeout: 0,
receiveTimeout: () => 0,
});

const handler1 = jest.fn(() => Promise.reject('error'));
const handler2 = jest.fn(() => Promise.resolve());

sut.onSubject('test', handler1, {maxAttempts: 2, backoffDelay: 1});
sut.onSubject('test', handler2);
sut.listen();

await new Promise(resolve => setTimeout(resolve, 10));
sut.stop();

expect(handler1).toHaveBeenCalledTimes(1);
expect(handler2).toHaveBeenCalledTimes(1);
expect(queueMock.deleteMessage).toHaveBeenCalledTimes(1);
});
});
});

0 comments on commit 64a3b50

Please sign in to comment.