-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(package): add message queue test
- Loading branch information
1 parent
4cdcc71
commit 8cc198b
Showing
3 changed files
with
56 additions
and
32 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 |
---|---|---|
@@ -1,41 +1,33 @@ | ||
import { expect, describe, it, beforeAll } from 'vitest'; | ||
import { expect, describe, it, beforeAll, afterAll } from 'vitest'; | ||
import { MessageQueue } from '../src'; | ||
|
||
describe('#MessageQueue', () => { | ||
let messageQueue; | ||
describe('MessageQueue', () => { | ||
let messageQueue: MessageQueue; | ||
|
||
beforeAll(() => { | ||
messageQueue = new MessageQueue('amqp://localhost'); | ||
messageQueue.bindExchangeWithQueue('Booking-Exchange', 'Booking-Queue'); | ||
}); | ||
|
||
describe('send message', async () => { | ||
it('should send message', async () => { | ||
const res = await messageQueue.sendMessage({ | ||
bookingId: '123', | ||
payment: 123.1, | ||
}); | ||
expect(res).toBe(true); | ||
}); | ||
afterAll(() => { | ||
messageQueue.closeConnection(); | ||
}); | ||
|
||
it('should bind exchange with queue', async () => { | ||
await messageQueue.bindExchangeWithQueue('Booking-Ticket', 'Booking'); | ||
expect(messageQueue.queue).toBeDefined(); | ||
expect(messageQueue.exchange).toBeDefined(); | ||
}); | ||
|
||
describe('send message with 3 retries', async () => { | ||
it('should send message 3 times', async () => { | ||
const MAX_RETRIES = 3; | ||
const res = await messageQueue.sendMessageWithRetry( | ||
{ | ||
bookingId: '123', | ||
payment: 123.1, | ||
}, | ||
MAX_RETRIES | ||
); | ||
expect(res).toBe('DONE'); | ||
}); | ||
it('should send and receive message', async () => { | ||
const message = { data: 'hello world' }; | ||
await messageQueue.sendMessage(message); | ||
const receivedMessage = await messageQueue.getMessage(); | ||
expect(receivedMessage).toEqual(message); | ||
}); | ||
|
||
describe('get message', async () => { | ||
it('should get message', async () => { | ||
const message = await messageQueue.getMessage(); | ||
expect(message).toEqual({ bookingId: '123', payment: 123.1 }); | ||
}); | ||
}); | ||
it('should send message with retry', async () => { | ||
const message = { data: 'hello world' }; | ||
const result = await messageQueue.sendMessageWithRetry(message, 3); | ||
expect(result).toEqual('DONE'); | ||
}); | ||
}); |
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,32 @@ | ||
import MessageQueue from './message.queue'; | ||
|
||
describe('MessageQueue', () => { | ||
let messageQueue: MessageQueue; | ||
|
||
beforeAll(() => { | ||
messageQueue = new MessageQueue('amqp://localhost'); | ||
}); | ||
|
||
afterAll(() => { | ||
messageQueue.closeConnection(); | ||
}); | ||
|
||
it('should bind exchange with queue', async () => { | ||
await messageQueue.bindExchangeWithQueue('Booking-Ticket', 'Booking'); | ||
expect(messageQueue.queue).toBeDefined(); | ||
expect(messageQueue.exchange).toBeDefined(); | ||
}); | ||
|
||
it('should send and receive message', async () => { | ||
const message = { data: 'hello world' }; | ||
await messageQueue.sendMessage(message); | ||
const receivedMessage = await messageQueue.getMessage(); | ||
expect(receivedMessage).toEqual(message); | ||
}); | ||
|
||
it('should send message with retry', async () => { | ||
const message = { data: 'hello world' }; | ||
const result = await messageQueue.sendMessageWithRetry(message, 3); | ||
expect(result).toEqual('DONE'); | ||
}); | ||
}); |
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