From ae439c252210e3cfe85971c5ffabb06b8ffd1aa3 Mon Sep 17 00:00:00 2001 From: Eric Duminil Date: Sun, 1 Dec 2024 21:29:30 +0100 Subject: [PATCH] Allow different topics for monitoring and publishing in MQTT tests --- test/backend-test/test-mqtt.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/backend-test/test-mqtt.js b/test/backend-test/test-mqtt.js index 450310298..8eecfdb40 100644 --- a/test/backend-test/test-mqtt.js +++ b/test/backend-test/test-mqtt.js @@ -9,17 +9,19 @@ const { UP, PENDING } = require("../../src/util"); * Runs an MQTT test with the * @param {string} mqttSuccessMessage the message that the monitor expects * @param {null|"keyword"|"json-query"} mqttCheckType the type of check we perform - * @param {string} receivedMessage what message is recieved from the mqtt channel + * @param {string} receivedMessage what message is received from the mqtt channel + * @param {string} monitorTopic which MQTT topic is monitored (wildcards are allowed) + * @param {string} publishTopic to which MQTT topic the message is sent * @returns {Promise} the heartbeat produced by the check */ -async function testMqtt(mqttSuccessMessage, mqttCheckType, receivedMessage) { +async function testMqtt(mqttSuccessMessage, mqttCheckType, receivedMessage, monitorTopic = "test", publishTopic = "test") { const hiveMQContainer = await new HiveMQContainer().start(); const connectionString = hiveMQContainer.getConnectionString(); const mqttMonitorType = new MqttMonitorType(); const monitor = { jsonPath: "firstProp", // always return firstProp for the json-query monitor hostname: connectionString.split(":", 2).join(":"), - mqttTopic: "test", + mqttTopic: monitorTopic, port: connectionString.split(":")[2], mqttUsername: null, mqttPassword: null, @@ -35,9 +37,9 @@ async function testMqtt(mqttSuccessMessage, mqttCheckType, receivedMessage) { const testMqttClient = mqtt.connect(hiveMQContainer.getConnectionString()); testMqttClient.on("connect", () => { - testMqttClient.subscribe("test", (error) => { + testMqttClient.subscribe(monitorTopic, (error) => { if (!error) { - testMqttClient.publish("test", receivedMessage); + testMqttClient.publish(publishTopic, receivedMessage); } }); });