-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some unit tests for QueueSubjectListener
- Loading branch information
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
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)); | ||
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)); | ||
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)); | ||
expect(handler1).toHaveBeenCalledTimes(1); | ||
expect(handler2).toHaveBeenCalledTimes(1); | ||
expect(queueMock.deleteMessage).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |