|
| 1 | +import { Management } from "../../src/index.js" |
| 2 | +import { afterEach, beforeEach, describe, expect, test } from "vitest" |
| 3 | +import { eventually, host, password, port, username, cleanRabbit } from "../support/util.js" |
| 4 | +import { createEnvironment, Environment } from "../../src/environment.js" |
| 5 | +import { Connection } from "../../src/connection.js" |
| 6 | +import { Queue } from "../../src/queue.js" |
| 7 | +import { Exchange } from "../../src/exchange.js" |
| 8 | +import { createAmqpMessage } from "../../src/message.js" |
| 9 | + |
| 10 | +describe("Consumer", () => { |
| 11 | + let environment: Environment |
| 12 | + let connection: Connection |
| 13 | + let management: Management |
| 14 | + let queue: Queue |
| 15 | + let exchange: Exchange |
| 16 | + |
| 17 | + const exchangeName = "test-exchange" |
| 18 | + const queueName = "test-queue" |
| 19 | + const bindingKey = "test-binding" |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + environment = createEnvironment({ |
| 23 | + host, |
| 24 | + port, |
| 25 | + username, |
| 26 | + password, |
| 27 | + }) |
| 28 | + connection = await environment.createConnection() |
| 29 | + management = connection.management() |
| 30 | + queue = await management.declareQueue(queueName) |
| 31 | + exchange = await management.declareExchange(exchangeName) |
| 32 | + await management.bind(bindingKey, { source: exchange, destination: queue }) |
| 33 | + }) |
| 34 | + |
| 35 | + afterEach(async () => { |
| 36 | + try { |
| 37 | + await cleanRabbit({ match: /test-/ }) |
| 38 | + await connection.close() |
| 39 | + await environment.close() |
| 40 | + } catch (error) { |
| 41 | + console.error(error) |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + test("consumer can handle message on exchange", async () => { |
| 46 | + const publisher = await connection.createPublisher({ exchange: { name: exchangeName, routingKey: bindingKey } }) |
| 47 | + const expectedBody = "ciao" |
| 48 | + await publisher.publish(createAmqpMessage({ body: expectedBody })) |
| 49 | + let received: string = "" |
| 50 | + |
| 51 | + const consumer = await connection.createConsumer(queueName, { |
| 52 | + messageHandler: (message) => { |
| 53 | + received = message.body |
| 54 | + }, |
| 55 | + }) |
| 56 | + consumer.start() |
| 57 | + |
| 58 | + await eventually(() => { |
| 59 | + expect(received).to.be.eql(expectedBody) |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + test.skip("consumer can handle message on exchange, destination on message", async () => { |
| 64 | + const publisher = await connection.createPublisher() |
| 65 | + const expectedBody = "ciao" |
| 66 | + await publisher.publish( |
| 67 | + createAmqpMessage({ |
| 68 | + body: expectedBody, |
| 69 | + destination: { exchange: { name: exchangeName, routingKey: bindingKey } }, |
| 70 | + }) |
| 71 | + ) |
| 72 | + let received: string = "" |
| 73 | + |
| 74 | + const consumer = await connection.createConsumer(queueName, { |
| 75 | + messageHandler: (message) => { |
| 76 | + received = message.body |
| 77 | + }, |
| 78 | + }) |
| 79 | + consumer.start() |
| 80 | + |
| 81 | + await eventually(() => { |
| 82 | + expect(received).to.be.eql(expectedBody) |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + test("consumer can handle message on queue", async () => { |
| 87 | + const publisher = await connection.createPublisher({ queue: { name: queueName } }) |
| 88 | + const expectedBody = "ciao" |
| 89 | + await publisher.publish( |
| 90 | + createAmqpMessage({ |
| 91 | + body: expectedBody, |
| 92 | + }) |
| 93 | + ) |
| 94 | + let received: string = "" |
| 95 | + |
| 96 | + const consumer = await connection.createConsumer(queueName, { |
| 97 | + messageHandler: (message) => { |
| 98 | + received = message.body |
| 99 | + }, |
| 100 | + }) |
| 101 | + consumer.start() |
| 102 | + |
| 103 | + await eventually(() => { |
| 104 | + expect(received).to.be.eql(expectedBody) |
| 105 | + }) |
| 106 | + }) |
| 107 | +}) |
0 commit comments