improve tests

This commit is contained in:
nora 2022-03-26 13:02:53 +01:00
parent a1fa68396f
commit a31c0a555f
8 changed files with 80 additions and 25 deletions

View file

@ -0,0 +1,25 @@
/*
This test binds a queue to a fanout exchange and sends a message to that exchange.
It expects the message to arrive at the queue.
*/
import { connectAmqp, waitForMessage } from './utils/utils.js';
const connection = await connectAmqp();
const channel = await connection.createChannel();
const QUEUE = 'exchange-bind-queue-2352';
const EXCHANGE = 'exchange-bind-3152';
await channel.assertQueue(QUEUE);
await channel.assertExchange(EXCHANGE, 'fanout');
await channel.bindQueue(QUEUE, EXCHANGE, '');
await channel.publish(EXCHANGE, '', Buffer.from('STOP'));
console.log('Sent STOP message to queue');
await waitForMessage(channel, QUEUE, 'STOP');
await channel.close();
await connection.close();

View file

@ -1,28 +1,23 @@
import { connectAmqp } from './utils/utils.js';
/*
This test consumes from a queue and then sends a message to it, expecting it to arrive.
*/
import { connectAmqp, waitForMessage } from './utils/utils.js';
const connection = await connectAmqp();
const channel = await connection.createChannel();
await channel.assertQueue('consume-queue-1415');
const QUEUE = 'consume-queue-1415';
const consumePromise = new Promise((resolve) => {
channel
.consume('consume-queue-1415', (msg) => {
if (msg.content.toString() === 'STOP') {
resolve();
}
})
.then((response) =>
console.log(`Registered consumer, consumerTag: "${response.consumerTag}"`)
);
});
await channel.assertQueue(QUEUE);
const consumer = waitForMessage(channel, QUEUE, 'STOP');
await channel.sendToQueue(QUEUE, Buffer.from('STOP'));
await channel.sendToQueue('consume-queue-1415', Buffer.from('STOP'));
console.log('Sent STOP message to queue');
await consumePromise;
console.log('Received STOP!');
await consumer;
await channel.close();
await connection.close();

View file

@ -1,18 +1,22 @@
/*
This test declares a new queue and expects it to be empty.
*/
import { assert, connectAmqp } from './utils/utils.js';
const queueName = 'test-queue-124';
const QUEUE = 'test-queue-124';
const connection = await connectAmqp();
const channel = await connection.createChannel();
const reply = await channel.assertQueue(queueName);
const reply = await channel.assertQueue(QUEUE);
assert(reply.messageCount === 0, 'Message found in queue');
assert(reply.consumerCount === 0, 'Consumer listening on queue');
assert(reply.queue === queueName, 'Wrong queue name returned');
assert(reply.queue === QUEUE, 'Wrong queue name returned');
console.log(`created queue '${queueName}'`);
console.log(`created queue '${QUEUE}'`);
await channel.close();
await connection.close();

View file

@ -1,3 +1,8 @@
/*
This test creates a queue with an empty name and asserts that the generated
queue name is not empty.
*/
import { assert, connectAmqp } from './utils/utils.js';
const connection = await connectAmqp();

View file

@ -1 +1,5 @@
/*
This is a dummy test that makes sure the test system works.
*/
console.log('Passed :)');

View file

@ -1,3 +1,6 @@
/*
This queue opens a channel and closes it again.
*/
import { connectAmqp } from './utils/utils.js';
const connection = await connectAmqp();

View file

@ -1,11 +1,16 @@
/*
This test just sends a message to a new queue.
*/
import { connectAmqp } from './utils/utils.js';
const connection = await connectAmqp();
const channel = await connection.createChannel();
await channel.assertQueue('send-queue-352');
const QUEUE = 'send-queue-352';
channel.publish('', 'send-queue-352', Buffer.from('hello'));
await channel.assertQueue(QUEUE);
channel.publish('', QUEUE, Buffer.from('hello'));
console.log('Published message');

View file

@ -1,7 +1,5 @@
import { connect } from 'amqplib';
export const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
export const connectAmqp = async () => {
return connect(
{
@ -22,3 +20,19 @@ export const assert = (cond, msg) => {
throw new Error(`Assertion failed: ${msg}`);
}
};
export const waitForMessage = (channel, queue, message) =>
new Promise((resolve) => {
channel
.consume(queue, (msg) => {
if (msg.content.toString() === message) {
console.log(`Received '${message}'!`);
resolve();
}
})
.then((response) =>
console.log(
`Registered consumer, consumerTag: "${response.consumerTag}"`
)
);
});