fix odl patches
[ccsdk/distribution.git] / dgbuilder / test / nodes / core / storage / 28-tail_spec.js
1 /**
2  * Copyright 2014 IBM Corp.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  **/
16
17 var should = require("should");
18 var path = require('path');
19 var fs = require('fs-extra');
20 var mkdirp = require('mkdirp');
21
22 var tailNode = require("../../../../nodes/core/storage/28-tail.js");
23 var helper = require("../../helper.js");
24
25 describe('TailNode', function() {
26
27     var resourcesDir = path.join(__dirname,"..","..","..","resources");
28     var fileToTail = path.join(resourcesDir,"28-tail-test-file.txt");
29
30     beforeEach(function(done) {
31         fs.writeFileSync(fileToTail, "Tail message line 1\nTail message line 2\n");
32         helper.startServer(done);
33     });
34
35     afterEach(function(done) {
36         helper.unload().then(function() {
37             fs.unlinkSync(fileToTail);
38             helper.stopServer(done);
39         });
40     });
41
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');
47             done();
48         });
49     });
50
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");
57             var inputCounter = 0;
58             helperNode1.on("input", function(msg) {
59                 //console.log(msg);
60                 msg.should.have.property('topic', fileToTail);
61                 msg.payload.should.equal("Tail message line " + (++inputCounter + 2));
62                 if (inputCounter === 2) {
63                     done();
64                 }
65             });
66             setTimeout( function() {
67                 fs.appendFileSync(fileToTail, "Tail message line 3\n");
68                 fs.appendFileSync(fileToTail, "Tail message line 4\n");
69             },100);
70         });
71     });
72
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) {
80                 //console.log(msg);
81                 msg.should.have.property('topic', fileToTail);
82                 msg.payload.should.equal("Tail message line 5\nTail message line 6\n");
83                 done();
84             });
85             setTimeout( function() {
86                 fs.appendFileSync(fileToTail, "Tail message line 5\nTail message line 6\n");
87             },150);
88         });
89     });
90
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");
101                 done();
102             });
103             setTimeout( function() {
104                 fs.writeFileSync(fileToTail, "Tail message line\n");
105             },150);
106         });
107     });
108     /*
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;
116             var warned = false;
117             tailNode1.on("log", function(msg) {
118                 if (msg.level == "warn") { warned = true; }
119             });
120             helperNode1.on("input", function(msg) {
121                 console.log("inputCounter =",inputCounter);
122                 console.log(msg);
123                 msg.should.have.property('topic', fileToTail);
124                 inputCounter++;
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");
130                 } else {
131                     msg.payload.should.equal("Tail message line append "+inputCounter);
132                 }
133                 
134                 if (inputCounter === 5) {
135                     setTimeout(function() {
136                         warned.should.be.true;
137                         done();
138                     },100);
139                 }
140             });
141             var actions = [
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");}
147             ];
148             
149             function processAction() {
150                 var action = actions.shift();
151                 action();
152                 if (actions.length > 0) {
153                     setTimeout(function() {
154                         processAction();
155                     },250);
156                 }
157             }
158             setTimeout( function() {
159                 processAction();
160             },150);
161         });
162     });
163     */
164
165 });