summaryrefslogtreecommitdiffstats
path: root/dgbuilder/test/nodes/core/storage/28-tail_spec.js
blob: 06124576122e6f1b70cbc37b76983838cc3508f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
 * 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 mkdirp = require('mkdirp');

var tailNode = require("../../../../nodes/core/storage/28-tail.js");
var helper = require("../../helper.js");

describe('TailNode', function() {

    var resourcesDir = path.join(__dirname,"..","..","..","resources");
    var fileToTail = path.join(resourcesDir,"28-tail-test-file.txt");

    beforeEach(function(done) {
        fs.writeFileSync(fileToTail, "Tail message line 1\nTail message line 2\n");
        helper.startServer(done);
    });

    afterEach(function(done) {
        helper.unload().then(function() {
            fs.unlinkSync(fileToTail);
            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('tail 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) {
                //console.log(msg);
                msg.should.have.property('topic', fileToTail);
                msg.payload.should.equal("Tail message line " + (++inputCounter + 2));
                if (inputCounter === 2) {
                    done();
                }
            });
            setTimeout( function() {
                fs.appendFileSync(fileToTail, "Tail message line 3\n");
                fs.appendFileSync(fileToTail, "Tail message line 4\n");
            },100);
        });
    });

    it('tail should 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) {
                //console.log(msg);
                msg.should.have.property('topic', fileToTail);
                msg.payload.should.equal("Tail message line 5\nTail message line 6\n");
                done();
            });
            setTimeout( function() {
                fs.appendFileSync(fileToTail, "Tail message line 5\nTail message line 6\n");
            },150);
        });
    });

    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:[]}];
        helper.load(tailNode, flow, function() {
            var tailNode1 = helper.getNode("tailNode1");
            var helperNode1 = helper.getNode("helperNode1");
            var inputCounter = 0;
            var warned = false;
            tailNode1.on("log", function(msg) {
                if (msg.level == "warn") { warned = true; }
            });
            helperNode1.on("input", function(msg) {
                console.log("inputCounter =",inputCounter);
                console.log(msg);
                msg.should.have.property('topic', fileToTail);
                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);
                }
            });
            var actions = [
                function() { fs.appendFileSync(fileToTail, "Tail message line append\n");},
                function() { fs.writeFileSync(fileToTail, "Tail message line truncate\n");},
                function() { fs.appendFileSync(fileToTail, "Tail message line append 3\n");},
                function() { fs.appendFileSync(fileToTail, "Tail message line append 4\n");},
                function() { fs.appendFileSync(fileToTail, "Tail message line append 5\n");}
            ];
            
            function processAction() {
                var action = actions.shift();
                action();
                if (actions.length > 0) {
                    setTimeout(function() {
                        processAction();
                    },250);
                }
            }
            setTimeout( function() {
                processAction();
            },150);
        });
    });
    */

});