added default timeout to function node (#1)

* added default timeout to function node

* added unit test to support defaultFunctionTimeout
This commit is contained in:
Vasu Vanka
2024-12-10 01:41:54 +05:30
committed by GitHub
parent bb01f26f06
commit 477e81256e
3 changed files with 98 additions and 5 deletions

View File

@@ -1437,7 +1437,7 @@ describe('function node', function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "function";
});
logEvents.should.have.length(1);
logEvents.should.have.length(2);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
@@ -1451,7 +1451,7 @@ describe('function node', function() {
});
});
it('check if default function timeout settings are recognized', function (done) {
it('check if function timeout settings are recognized', function (done) {
RED.settings.functionTimeout = 0.01;
var flow = [{id: "n1",type: "function",timeout: RED.settings.functionTimeout,wires: [["n2"]],func: "while(1==1){};\nreturn msg;"}];
helper.load(functionNode, flow, function () {
@@ -1463,7 +1463,7 @@ describe('function node', function() {
var logEvents = helper.log().args.filter(function (evt) {
return evt[0].type == "function";
});
logEvents.should.have.length(1);
logEvents.should.have.length(2);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
@@ -1479,6 +1479,65 @@ describe('function node', function() {
});
});
it('check if default function timeout settings are recognized', function (done) {
RED.settings.defaultFunctionTimeout = 0.01;
var flow = [{id: "n1",type: "function",wires: [["n2"]],func: "while(1==1){};\nreturn msg;"}];
helper.load(functionNode, flow, function () {
var n1 = helper.getNode("n1");
n1.receive({ payload: "foo", topic: "bar" });
setTimeout(function () {
try {
helper.log().called.should.be.true();
var logEvents = helper.log().args.filter(function (evt) {
return evt[0].type == "function";
});
logEvents.should.have.length(2);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
msg.should.have.property('type', 'function');
should.equal(RED.settings.defaultFunctionTimeout, 0.01);
should.equal(msg.msg.message, 'Script execution timed out after 10ms');
delete RED.settings.defaultFunctionTimeout;
done();
} catch (err) {
done(err);
}
}, 500);
});
});
it('check if functionTimeout has higher precedence over default function timeout setting', function (done) {
RED.settings.defaultFunctionTimeout = 0.02;
RED.settings.functionTimeout = 0.01;
var flow = [{id: "n1",type: "function",timeout: RED.settings.functionTimeout,wires: [["n2"]],func: "while(1==1){};\nreturn msg;"}];
helper.load(functionNode, flow, function () {
var n1 = helper.getNode("n1");
n1.receive({ payload: "foo", topic: "bar" });
setTimeout(function () {
try {
helper.log().called.should.be.true();
var logEvents = helper.log().args.filter(function (evt) {
return evt[0].type == "function";
});
logEvents.should.have.length(2);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
msg.should.have.property('type', 'function');
should.equal(RED.settings.functionTimeout, 0.01);
should.equal(RED.settings.defaultFunctionTimeout, 0.02);
should.equal(msg.msg.message, 'Script execution timed out after 10ms');
delete RED.settings.functionTimeout;
delete RED.settings.defaultFunctionTimeout;
done();
} catch (err) {
done(err);
}
}, 500);
});
});
describe("finalize function", function() {
it('should execute', function(done) {