From 691ef12e04d3bdb2c2e62dd0ed9e3c636634d1b3 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Wed, 27 Aug 2014 23:29:26 +0100 Subject: [PATCH] Add more tail node tests - add 'file doesn't exist initially' text - remove resource file as it is programmatically modified by the test - tweak test checking in truncated file test --- test/nodes/core/storage/28-tail_spec.js | 55 ++++++++++++++++++++----- test/nodes/helper.js | 4 +- test/resources/28-tail-test-file.txt | 3 -- 3 files changed, 47 insertions(+), 15 deletions(-) delete mode 100644 test/resources/28-tail-test-file.txt diff --git a/test/nodes/core/storage/28-tail_spec.js b/test/nodes/core/storage/28-tail_spec.js index c60bf6095..f4fc88442 100644 --- a/test/nodes/core/storage/28-tail_spec.js +++ b/test/nodes/core/storage/28-tail_spec.js @@ -26,15 +26,17 @@ describe('TailNode', function() { var resourcesDir = path.join(__dirname,"..","..","..","resources"); var fileToTail = path.join(resourcesDir,"28-tail-test-file.txt"); - fs.writeFileSync(fileToTail, "Tail message line 1\nTail message line 2\n"); beforeEach(function(done) { + fs.writeFileSync(fileToTail, "Tail message line 1\nTail message line 2\n"); helper.startServer(done); }); afterEach(function(done) { - helper.unload(); - helper.stopServer(done); + helper.unload().then(function() { + fs.unlinkSync(fileToTail); + helper.stopServer(done); + }); }); it('should be loaded', function(done) { @@ -86,6 +88,24 @@ describe('TailNode', function() { }); }); + it('tail should handle a non-existent file', function(done) { + fs.unlinkSync(fileToTail); + var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]}, + {id:"helperNode1", type:"helper", wires:[]}]; + helper.load(tailNode, flow, function() { + var tailNode1 = helper.getNode("tailNode1"); + var helperNode1 = helper.getNode("helperNode1"); + helperNode1.on("input", function(msg) { + msg.should.have.property('topic', fileToTail); + msg.payload.should.equal("Tail message line"); + done(); + }); + setTimeout( function() { + fs.writeFileSync(fileToTail, "Tail message line\n"); + },150); + }); + }); + it('tail should handle file truncation', function(done) { var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]}, {id:"helperNode1", type:"helper", wires:[]}]; @@ -99,15 +119,30 @@ describe('TailNode', function() { }); helperNode1.on("input", function(msg) { msg.should.have.property('topic', fileToTail); - msg.payload.should.equal("Tail message line A"); - if ((++inputCounter === 3) && (warned === true)) { done(); } + inputCounter++; + if (inputCounter === 1) { + warned.should.be.false; + msg.payload.should.equal("Tail message line append"); + } else if (inputCounter === 2) { + msg.payload.should.equal("Tail message line truncate"); + } else { + msg.payload.should.equal("Tail message line append "+inputCounter); + } + + if (inputCounter === 5) { + setTimeout(function() { + warned.should.be.true; + done(); + },100); + } }); setTimeout( function() { - fs.writeFileSync(fileToTail, "Tail message line A\n"); - fs.appendFileSync(fileToTail, "Tail message line A\n"); - fs.appendFileSync(fileToTail, "Tail message line A\n"); - fs.appendFileSync(fileToTail, "Tail message line A\n"); - },200); + fs.appendFileSync(fileToTail, "Tail message line append\n"); + fs.writeFileSync(fileToTail, "Tail message line truncate\n"); + fs.appendFileSync(fileToTail, "Tail message line append 3\n"); + fs.appendFileSync(fileToTail, "Tail message line append 4\n"); + fs.appendFileSync(fileToTail, "Tail message line append 5\n"); + },150); }); }); diff --git a/test/nodes/helper.js b/test/nodes/helper.js index 12c8b60fc..6019815c8 100644 --- a/test/nodes/helper.js +++ b/test/nodes/helper.js @@ -62,7 +62,7 @@ module.exports = { unload: function() { // TODO: any other state to remove between tests? redNodes.clearRegistry(); - flows.stopFlows(); + return flows.stopFlows(); }, getNode: function(id) { @@ -89,7 +89,7 @@ module.exports = { }); }, //TODO consider saving TCP handshake/server reinit on start/stop/start sequences - stopServer : function(done) { + stopServer: function(done) { if(server) { server.close(done); } diff --git a/test/resources/28-tail-test-file.txt b/test/resources/28-tail-test-file.txt deleted file mode 100644 index 875426e7a..000000000 --- a/test/resources/28-tail-test-file.txt +++ /dev/null @@ -1,3 +0,0 @@ -Tail message line A -Tail message line A -Tail message line A