From 716d0c1135cb3358ef3a77ee8bbab487862bceb4 Mon Sep 17 00:00:00 2001 From: zobalogh Date: Fri, 1 Aug 2014 14:17:32 +0100 Subject: [PATCH] Adding tail node tests --- test/nodes/core/storage/28-tail_spec.js | 93 +++++++++++++++++++++++++ test/nodes/helper.js | 10 ++- 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 test/nodes/core/storage/28-tail_spec.js diff --git a/test/nodes/core/storage/28-tail_spec.js b/test/nodes/core/storage/28-tail_spec.js new file mode 100644 index 000000000..b31353f41 --- /dev/null +++ b/test/nodes/core/storage/28-tail_spec.js @@ -0,0 +1,93 @@ +/** + * Copyright 2014 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +var should = require("should"); +var path = require('path'); +var fs = require('fs-extra'); + +var tailNode = require("../../../../nodes/core/storage/28-tail.js"); +var helper = require("../../helper.js"); + +describe('TailNode', function() { + + var tempDir = path.join(__dirname, ".tmp/"); + var fileToTail = path.join(tempDir, "tailMe.txt"); + + beforeEach(function(done) { + fs.exists(fileToTail, function(exists) { + if(exists) { + fs.unlinkSync(fileToTail); + } + }); + fs.exists(tempDir, function(exists) { + if(!exists) { + fs.mkdirSync(tempDir); + } + fs.writeFileSync(fileToTail, "Tail message line1\nTail message line2\n"); + helper.startServer(done); + }); + }); + + afterEach(function(done) { + + fs.exists(fileToTail, function(exists) { + if(exists) { + fs.unlinkSync(fileToTail); + } + // have to call stop server otherwise tail node litters test output + // with warnings when tailed file gets deleted + fs.remove(tempDir, helper.stopServer(done)); + }); + }); + + it('should be loaded', function(done) { + var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail}]; + helper.load(tailNode, flow, function() { + var tailNode1 = helper.getNode("tailNode1"); + tailNode1.should.have.property('name', 'tailNode'); + done(); + }); + }); + + it('should tail a file', function(done) { + 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"); + var inputCounter = 0; + helperNode1.on("input", function(msg) { + msg.should.have.property('topic', fileToTail); + msg.payload.should.equal("Tail message line" + (++inputCounter)); + }); + done(); + }); + }); + + it('work in non-split mode', function(done) { + var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":false, "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 line1\nTail message line2\n"); + }); + done(); + }); + }); +}); diff --git a/test/nodes/helper.js b/test/nodes/helper.js index d38e27d82..a60bcd1e7 100644 --- a/test/nodes/helper.js +++ b/test/nodes/helper.js @@ -31,6 +31,8 @@ var listenPort = 0; // use ephemeral port var port; var url; +var server; + function helperNode(n) { RED.nodes.createNode(this, n); } @@ -71,7 +73,7 @@ module.exports = { }, startServer: function(done) { - var server = http.createServer(function(req,res){app(req,res);}); + server = http.createServer(function(req,res){app(req,res);}); RED.init(server, {}); server.listen(listenPort, address); server.on('listening', function() { @@ -81,6 +83,12 @@ module.exports = { done(); }); }, + //TODO consider saving TCP handshake/server reinit on start/stop/start sequences + stopServer : function(done) { + if(server) { + server.close(done); + } + }, url: function() { return url; },