index.js
3.28 KB
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
require('./setup');
var index = require('../index');
var Promise = require('bluebird');
describe('Main index.js', function() {
var validData = {}
before(function(done) {
validData = {
dirname: './log_test/',
log: {},
stat: {
interval: 2,
data: [
{key: 'a', threshold: 2, threshold_inv: 1},
{key: 'b', threshold: 2 }
]
},
alarm: {}
}
done();
});
it('init function should return true on cb properly', function(done) {
var object = index.init(validData);
expect(object).to.be.an('object');
expect(object).to.have.property('log');
expect(object).to.have.property('stat');
done();
});
it('init function should return err on cb properly (case 1)', function(done) {
try {
var object = index.init({log: 'hi'});
}
catch (err) {
var my_err = err.message.split(',');
expect(my_err).to.include.members(['log is invalid format', 'dirname is required', 'alarm is required']);
done();
}
});
it('init function should return err on cb properly (case 2)', function(done) {
try {
var object = index.init({pupu: 'hi'});
}
catch (err) {
var my_err = err.message.split(',');
expect(my_err).to.include.members(['pupu is not allowed']);
done();
}
});
it('init function should return err on cb properly (case 3)', function(done) {
try {
var object = index.init({pupu: 'hi'});
}
catch (err) {
var my_err = err.message.split(',');
expect(my_err).to.include.members(['log is required', 'stat is required']);
done();
}
});
it('should fail to create stat object if validdata object contain no key with threshold', function(done) {
var testData = {
dirname: './log_test/',
log: {},
stat: {
interval: 2,
data: [
{key: 'a', threshold: 2, threshold_inv: 1},
{key: 'b', threshold: 2},
{key: 'c', threshold_inv: 1},
{key: 'd', threshold: 2},
{threshold: 2},
]
}
}
try {
var object = index.init(testData);
}
catch (err) {
var my_err = err.message.split(',');
expect(my_err).to.include.members(['stat.data.4.key is required']);
done();
}
});
it('should run the whole flow correctly', function(done) {
var initData = {
dirname: './whole_flow/',
log: {
rotation: 500,
maxsize: 500
},
stat: {
rotation: 500,
maxsize: 500,
interval: 5,
data: [
{key: 'testIncrement1', threshold: 3, threshold_inv: 1},
{key: 'testIncrement2', threshold: 2 }
]
},
alarm: {
rotation: 500,
maxsize: 500
}
};
var logObj = index.init(initData);
Promise.try(function() {})
.then(function() {
logObj.stat.start();
logObj.log.append({hi: 'test'});
logObj.stat.increment('testIncrement1');
return Promise.delay(100);
})
.then(function() {
logObj.log.append({hi: 'test2'});
logObj.stat.increment('testIncrement2');
logObj.log.append({hi: 'test2'});
logObj.stat.increment('testIncrement2');
})
.then(function() {
logObj.stat.stop();
done();
})
});
});