mirror of
https://github.com/Noratrieb/haesli.git
synced 2026-01-14 11:45:02 +01:00
improve tests
This commit is contained in:
parent
a1fa68396f
commit
a31c0a555f
8 changed files with 80 additions and 25 deletions
25
test-js/src/bind-exchange-message.js
Normal file
25
test-js/src/bind-exchange-message.js
Normal 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();
|
||||||
|
|
@ -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 connection = await connectAmqp();
|
||||||
const channel = await connection.createChannel();
|
const channel = await connection.createChannel();
|
||||||
|
|
||||||
await channel.assertQueue('consume-queue-1415');
|
const QUEUE = 'consume-queue-1415';
|
||||||
|
|
||||||
const consumePromise = new Promise((resolve) => {
|
await channel.assertQueue(QUEUE);
|
||||||
channel
|
|
||||||
.consume('consume-queue-1415', (msg) => {
|
const consumer = waitForMessage(channel, QUEUE, 'STOP');
|
||||||
if (msg.content.toString() === 'STOP') {
|
|
||||||
resolve();
|
await channel.sendToQueue(QUEUE, Buffer.from('STOP'));
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((response) =>
|
|
||||||
console.log(`Registered consumer, consumerTag: "${response.consumerTag}"`)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
await channel.sendToQueue('consume-queue-1415', Buffer.from('STOP'));
|
|
||||||
console.log('Sent STOP message to queue');
|
console.log('Sent STOP message to queue');
|
||||||
|
|
||||||
await consumePromise;
|
await consumer;
|
||||||
|
|
||||||
console.log('Received STOP!');
|
|
||||||
|
|
||||||
await channel.close();
|
await channel.close();
|
||||||
await connection.close();
|
await connection.close();
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,22 @@
|
||||||
|
/*
|
||||||
|
This test declares a new queue and expects it to be empty.
|
||||||
|
*/
|
||||||
|
|
||||||
import { assert, connectAmqp } from './utils/utils.js';
|
import { assert, connectAmqp } from './utils/utils.js';
|
||||||
|
|
||||||
const queueName = 'test-queue-124';
|
const QUEUE = 'test-queue-124';
|
||||||
|
|
||||||
const connection = await connectAmqp();
|
const connection = await connectAmqp();
|
||||||
|
|
||||||
const channel = await connection.createChannel();
|
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.messageCount === 0, 'Message found in queue');
|
||||||
assert(reply.consumerCount === 0, 'Consumer listening on 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 channel.close();
|
||||||
await connection.close();
|
await connection.close();
|
||||||
|
|
|
||||||
|
|
@ -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';
|
import { assert, connectAmqp } from './utils/utils.js';
|
||||||
|
|
||||||
const connection = await connectAmqp();
|
const connection = await connectAmqp();
|
||||||
|
|
|
||||||
|
|
@ -1 +1,5 @@
|
||||||
|
/*
|
||||||
|
This is a dummy test that makes sure the test system works.
|
||||||
|
*/
|
||||||
|
|
||||||
console.log('Passed :)');
|
console.log('Passed :)');
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
/*
|
||||||
|
This queue opens a channel and closes it again.
|
||||||
|
*/
|
||||||
import { connectAmqp } from './utils/utils.js';
|
import { connectAmqp } from './utils/utils.js';
|
||||||
|
|
||||||
const connection = await connectAmqp();
|
const connection = await connectAmqp();
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,16 @@
|
||||||
|
/*
|
||||||
|
This test just sends a message to a new queue.
|
||||||
|
*/
|
||||||
import { connectAmqp } from './utils/utils.js';
|
import { connectAmqp } from './utils/utils.js';
|
||||||
|
|
||||||
const connection = await connectAmqp();
|
const connection = await connectAmqp();
|
||||||
const channel = await connection.createChannel();
|
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');
|
console.log('Published message');
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
import { connect } from 'amqplib';
|
import { connect } from 'amqplib';
|
||||||
|
|
||||||
export const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
|
|
||||||
|
|
||||||
export const connectAmqp = async () => {
|
export const connectAmqp = async () => {
|
||||||
return connect(
|
return connect(
|
||||||
{
|
{
|
||||||
|
|
@ -22,3 +20,19 @@ export const assert = (cond, msg) => {
|
||||||
throw new Error(`Assertion failed: ${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}"`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue