|
| 1 | +import { afterEach, beforeEach, describe, test } from "vitest" |
| 2 | +import { use, expect } from "chai" |
| 3 | +import chaiAsPromised from "chai-as-promised" |
| 4 | +import { host, port, username, password, numberOfConnections, eventually } from "../../support/util.js" |
| 5 | +import { |
| 6 | + Connection, |
| 7 | + ConnectionEvents, |
| 8 | + ConnectionOptions, |
| 9 | + Container, |
| 10 | + create_container, |
| 11 | + Receiver, |
| 12 | + ReceiverEvents, |
| 13 | + ReceiverOptions, |
| 14 | + Sender, |
| 15 | + SenderEvents, |
| 16 | + SenderOptions, |
| 17 | +} from "rhea" |
| 18 | + |
| 19 | +use(chaiAsPromised) |
| 20 | + |
| 21 | +describe("Rhea tests", () => { |
| 22 | + let container: Container |
| 23 | + let connection: Connection |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + container = create_container() |
| 27 | + }) |
| 28 | + |
| 29 | + afterEach(async () => { |
| 30 | + await close(connection) |
| 31 | + }) |
| 32 | + |
| 33 | + test("create a connection", async () => { |
| 34 | + connection = await open(container, { |
| 35 | + host, |
| 36 | + port, |
| 37 | + username, |
| 38 | + password, |
| 39 | + }) |
| 40 | + |
| 41 | + await eventually(async () => { |
| 42 | + expect(await numberOfConnections()).to.eql(1) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + test("connect to the management", async () => { |
| 47 | + connection = await open(container, { |
| 48 | + host, |
| 49 | + port, |
| 50 | + username, |
| 51 | + password, |
| 52 | + }) |
| 53 | + |
| 54 | + await eventually(async () => { |
| 55 | + await openSender(connection) |
| 56 | + await openReceiver(connection) |
| 57 | + }, 4000) |
| 58 | + }) |
| 59 | +}) |
| 60 | + |
| 61 | +async function open(container: Container, params: ConnectionOptions): Promise<Connection> { |
| 62 | + return new Promise((res, rej) => { |
| 63 | + container.once(ConnectionEvents.connectionOpen, (context) => { |
| 64 | + return res(context.connection) |
| 65 | + }) |
| 66 | + container.once(ConnectionEvents.error, (context) => { |
| 67 | + return rej(context.connection.error) |
| 68 | + }) |
| 69 | + container.connect(params) |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +async function close(connection: Connection): Promise<void> { |
| 74 | + return new Promise((res, rej) => { |
| 75 | + connection.once(ConnectionEvents.connectionClose, () => { |
| 76 | + res() |
| 77 | + }) |
| 78 | + connection.once(ConnectionEvents.connectionError, (context) => { |
| 79 | + rej(new Error("Connection error: " + context.connection.error)) |
| 80 | + }) |
| 81 | + connection.close() |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +const MANAGEMENT_NODE_CONFIGURATION: SenderOptions | ReceiverOptions = { |
| 86 | + snd_settle_mode: 1, |
| 87 | + rcv_settle_mode: 0, |
| 88 | + name: "management-link-pair", |
| 89 | + target: { address: "/management", expiry_policy: "LINK_DETACH", timeout: 0, dynamic: false }, |
| 90 | + source: { address: "/management", expiry_policy: "LINK_DETACH", timeout: 0, dynamic: false, durable: 0 }, |
| 91 | + properties: { paired: true }, |
| 92 | +} |
| 93 | + |
| 94 | +async function openReceiver(connection: Connection) { |
| 95 | + return openLink( |
| 96 | + connection, |
| 97 | + ReceiverEvents.receiverOpen, |
| 98 | + ReceiverEvents.receiverError, |
| 99 | + connection.open_receiver.bind(connection), |
| 100 | + MANAGEMENT_NODE_CONFIGURATION |
| 101 | + ) |
| 102 | +} |
| 103 | + |
| 104 | +async function openSender(connection: Connection) { |
| 105 | + return openLink( |
| 106 | + connection, |
| 107 | + SenderEvents.senderOpen, |
| 108 | + SenderEvents.senderError, |
| 109 | + connection.open_sender.bind(connection), |
| 110 | + MANAGEMENT_NODE_CONFIGURATION |
| 111 | + ) |
| 112 | +} |
| 113 | + |
| 114 | +type LinkOpenEvents = SenderEvents.senderOpen | ReceiverEvents.receiverOpen |
| 115 | +type LinkErrorEvents = SenderEvents.senderError | ReceiverEvents.receiverError |
| 116 | +type OpenLinkMethods = |
| 117 | + | ((options?: SenderOptions | string) => Sender) |
| 118 | + | ((options?: ReceiverOptions | string) => Receiver) |
| 119 | + |
| 120 | +async function openLink( |
| 121 | + connection: Connection, |
| 122 | + successEvent: LinkOpenEvents, |
| 123 | + errorEvent: LinkErrorEvents, |
| 124 | + openMethod: OpenLinkMethods, |
| 125 | + config?: SenderOptions | ReceiverOptions | string |
| 126 | +): Promise<Sender | Receiver> { |
| 127 | + return new Promise((res, rej) => { |
| 128 | + connection.once(successEvent, (context) => { |
| 129 | + return res(context.receiver || context.sender) |
| 130 | + }) |
| 131 | + connection.once(errorEvent, (context) => { |
| 132 | + return rej(context.connection.error) |
| 133 | + }) |
| 134 | + openMethod(config) |
| 135 | + }) |
| 136 | +} |
0 commit comments