1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

fix new test and fix bug found in previous PR

This commit is contained in:
Steve-Mcl 2022-09-29 19:05:53 +01:00
parent f11b9c1e18
commit 81b4874a7c
2 changed files with 28 additions and 11 deletions

View File

@ -994,7 +994,7 @@ module.exports = function(RED) {
node.client.publish(msg.topic, msg.payload, options, function (err) { node.client.publish(msg.topic, msg.payload, options, function (err) {
if (done) { if (done) {
done(err) done(err)
} else { } else if(err) {
node.error(err, msg) node.error(err, msg)
} }
}) })

View File

@ -430,24 +430,38 @@ describe('MQTT Nodes', function () {
}; };
testSendRecv(brokerOptions, { topic: brokerOptions.birthTopic }, {}, options, hooks); testSendRecv(brokerOptions, { topic: brokerOptions.birthTopic }, {}, options, hooks);
}); });
itConditional('should fail with bad birth topic', function (done) { itConditional('should safely discard bad birth topic', function (done) {
if (skipTests) { return this.skip() } if (skipTests) { return this.skip() }
this.timeout = 2000; this.timeout = 2000;
const baseTopic = nextTopic(); const baseTopic = nextTopic();
const brokerOptions = { const brokerOptions = {
protocolVersion: 4, protocolVersion: 4,
birthTopic: baseTopic + "#", birthTopic: baseTopic + "#", // a publish topic should never have a wildcard
birthPayload: "broker connected", birthPayload: "broker connected",
birthQos: 2, birthQos: 2,
} }
const options = {}; const options = {};
const hooks = { done: done, beforeLoad: null, afterLoad: null, afterConnect: null }; const hooks = { done: null, beforeLoad: null, afterLoad: null, afterConnect: null };
options.expectMsg = { hooks.afterLoad = (helperNode, mqttBroker, mqttIn, mqttOut) => {
topic: brokerOptions.birthTopic, helperNode.on("input", function (msg) {
payload: brokerOptions.birthPayload, try {
qos: brokerOptions.birthQos msg.should.have.a.property("error").type("object");
}; msg.error.should.have.a.property("source").type("object");
testSendRecv(brokerOptions, { topic: brokerOptions.birthTopic }, {}, options, hooks); msg.error.source.should.have.a.property("id", mqttIn.id);
done();
} catch (err) {
done(err)
}
});
return true; //handled
}
options.expectMsg = null;
try {
testSendRecv(brokerOptions, { topic: brokerOptions.birthTopic }, {}, options, hooks);
done()
} catch(err) {
done(e)
}
}); });
itConditional('should publish close message', function (done) { itConditional('should publish close message', function (done) {
if (skipTests) { return this.skip() } if (skipTests) { return this.skip() }
@ -606,12 +620,13 @@ function testSendRecv(brokerOptions, inNodeOptions, outNodeOptions, options, hoo
const mqttBroker = helper.getNode(brokerOptions.id); const mqttBroker = helper.getNode(brokerOptions.id);
const mqttIn = helper.getNode(nodes.mqtt_in.id); const mqttIn = helper.getNode(nodes.mqtt_in.id);
const mqttOut = helper.getNode(nodes.mqtt_out.id); const mqttOut = helper.getNode(nodes.mqtt_out.id);
let afterLoadHandled = false; let afterLoadHandled = false, finished = false;
if (hooks.afterLoad) { if (hooks.afterLoad) {
afterLoadHandled = hooks.afterLoad(helperNode, mqttBroker, mqttIn, mqttOut) afterLoadHandled = hooks.afterLoad(helperNode, mqttBroker, mqttIn, mqttOut)
} }
if (!afterLoadHandled) { if (!afterLoadHandled) {
helperNode.on("input", function (msg) { helperNode.on("input", function (msg) {
finished = true
try { try {
compareMsgToExpected(msg, expectMsg); compareMsgToExpected(msg, expectMsg);
if (hooks.done) { hooks.done(); } if (hooks.done) { hooks.done(); }
@ -636,10 +651,12 @@ function testSendRecv(brokerOptions, inNodeOptions, outNodeOptions, options, hoo
} }
}) })
.catch((e) => { .catch((e) => {
if(finished) { return }
if (hooks.done) { hooks.done(e); } if (hooks.done) { hooks.done(e); }
else { throw e; } else { throw e; }
}); });
} catch (err) { } catch (err) {
if(finished) { return }
if (hooks.done) { hooks.done(err); } if (hooks.done) { hooks.done(err); }
else { throw err; } else { throw err; }
} }