2 * Copyright 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var should = require("should");
18 var path = require('path');
19 var fs = require('fs-extra');
20 var mkdirp = require('mkdirp');
22 var tailNode = require("../../../../nodes/core/storage/28-tail.js");
23 var helper = require("../../helper.js");
25 describe('TailNode', function() {
27 var resourcesDir = path.join(__dirname,"..","..","..","resources");
28 var fileToTail = path.join(resourcesDir,"28-tail-test-file.txt");
30 beforeEach(function(done) {
31 fs.writeFileSync(fileToTail, "Tail message line 1\nTail message line 2\n");
32 helper.startServer(done);
35 afterEach(function(done) {
36 helper.unload().then(function() {
37 fs.unlinkSync(fileToTail);
38 helper.stopServer(done);
42 it('should be loaded', function(done) {
43 var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail}];
44 helper.load(tailNode, flow, function() {
45 var tailNode1 = helper.getNode("tailNode1");
46 tailNode1.should.have.property('name', 'tailNode');
51 it('tail should tail a file', function(done) {
52 var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
53 {id:"helperNode1", type:"helper", wires:[]}];
54 helper.load(tailNode, flow, function() {
55 var tailNode1 = helper.getNode("tailNode1");
56 var helperNode1 = helper.getNode("helperNode1");
58 helperNode1.on("input", function(msg) {
60 msg.should.have.property('topic', fileToTail);
61 msg.payload.should.equal("Tail message line " + (++inputCounter + 2));
62 if (inputCounter === 2) {
66 setTimeout( function() {
67 fs.appendFileSync(fileToTail, "Tail message line 3\n");
68 fs.appendFileSync(fileToTail, "Tail message line 4\n");
73 it('tail should work in non-split mode', function(done) {
74 var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":false, "filename":fileToTail, "wires":[["helperNode1"]]},
75 {id:"helperNode1", type:"helper", wires:[]}];
76 helper.load(tailNode, flow, function() {
77 var tailNode1 = helper.getNode("tailNode1");
78 var helperNode1 = helper.getNode("helperNode1");
79 helperNode1.on("input", function(msg) {
81 msg.should.have.property('topic', fileToTail);
82 msg.payload.should.equal("Tail message line 5\nTail message line 6\n");
85 setTimeout( function() {
86 fs.appendFileSync(fileToTail, "Tail message line 5\nTail message line 6\n");
91 it('tail should handle a non-existent file', function(done) {
92 fs.unlinkSync(fileToTail);
93 var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
94 {id:"helperNode1", type:"helper", wires:[]}];
95 helper.load(tailNode, flow, function() {
96 var tailNode1 = helper.getNode("tailNode1");
97 var helperNode1 = helper.getNode("helperNode1");
98 helperNode1.on("input", function(msg) {
99 msg.should.have.property('topic', fileToTail);
100 msg.payload.should.equal("Tail message line");
103 setTimeout( function() {
104 fs.writeFileSync(fileToTail, "Tail message line\n");
109 it('tail should handle file truncation', function(done) {
110 var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
111 {id:"helperNode1", type:"helper", wires:[]}];
112 helper.load(tailNode, flow, function() {
113 var tailNode1 = helper.getNode("tailNode1");
114 var helperNode1 = helper.getNode("helperNode1");
115 var inputCounter = 0;
117 tailNode1.on("log", function(msg) {
118 if (msg.level == "warn") { warned = true; }
120 helperNode1.on("input", function(msg) {
121 console.log("inputCounter =",inputCounter);
123 msg.should.have.property('topic', fileToTail);
125 if (inputCounter === 1) {
126 warned.should.be.false;
127 msg.payload.should.equal("Tail message line append");
128 } else if (inputCounter === 2) {
129 msg.payload.should.equal("Tail message line truncate");
131 msg.payload.should.equal("Tail message line append "+inputCounter);
134 if (inputCounter === 5) {
135 setTimeout(function() {
136 warned.should.be.true;
142 function() { fs.appendFileSync(fileToTail, "Tail message line append\n");},
143 function() { fs.writeFileSync(fileToTail, "Tail message line truncate\n");},
144 function() { fs.appendFileSync(fileToTail, "Tail message line append 3\n");},
145 function() { fs.appendFileSync(fileToTail, "Tail message line append 4\n");},
146 function() { fs.appendFileSync(fileToTail, "Tail message line append 5\n");}
149 function processAction() {
150 var action = actions.shift();
152 if (actions.length > 0) {
153 setTimeout(function() {
158 setTimeout( function() {