Skip to content

Commit

Permalink
chore(package): add message queue test
Browse files Browse the repository at this point in the history
  • Loading branch information
belovetech committed Sep 26, 2023
1 parent 4cdcc71 commit 8cc198b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 32 deletions.
52 changes: 22 additions & 30 deletions packages/shared-libs/__tests__/message.queue.spec.ts
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');
});
});
32 changes: 32 additions & 0 deletions packages/shared-libs/src/message.queue.test.ts
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');
});
});
4 changes: 2 additions & 2 deletions packages/shared-libs/src/message.queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { logger } from './logger';
export default class MessageQueue {
private url: string;
public readonly connection: Amqp.Connection;
private queue: Amqp.Queue;
private exchange: Amqp.Exchange;
public queue: Amqp.Queue;
public exchange: Amqp.Exchange;
private MAX_RETRIES = 5;

constructor(url: string) {
Expand Down

0 comments on commit 8cc198b

Please sign in to comment.