-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
160 lines (114 loc) · 3.34 KB
/
index.test.js
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
'use strict';
var expect = require('chai').expect;
var sinon = require('sinon');
var Promise = require('bluebird');
describe(__filename, function() {
var mod;
beforeEach(function() {
delete require.cache[require.resolve('./index')];
mod = require('./index');
});
it('Should flush according to the flush interval', function(done) {
var flush = sinon.stub();
var x51 = mod({
flush: flush,
flushInterval: 200
});
x51.push({});
x51.push({});
setTimeout(function() {
expect(flush.callCount).to.eql(1);
var items = flush.getCall(0).args[0];
expect(items.length).to.eql(2);
done();
}, 250);
});
it('Should flush continuously', function(done) {
var flush = sinon.stub();
var x51 = mod({
flush: flush,
flushInterval: 50
});
x51.push({});
setTimeout(function() {
expect(flush.callCount).to.eql(1);
var items = flush.getCall(0).args[0];
expect(items.length).to.eql(1);
x51.push({});
x51.push({});
// Should flush a second time
setTimeout(function() {
expect(flush.callCount).to.eql(2);
var items = flush.getCall(1).args[0];
expect(items.length).to.eql(2);
done();
}, 60);
}, 70);
});
it('Should not lose records if flush throws an error', function() {
var flush = sinon.stub();
flush.onCall(0).throws(new Error());
flush.onCall(1).returns(undefined);
var x51 = mod({
flush: flush
});
x51.push({});
// This one will throw an error
x51.flush();
expect(flush.callCount).to.eql(1);
var items1 = flush.getCall(0).args[0];
expect(items1.length).to.eql(1);
// Send another record
x51.push({});
// This one should succeed
x51.flush();
expect(flush.callCount).to.eql(2);
var items2 = flush.getCall(1).args[0];
expect(items2.length).to.eql(2); // Should have both records this time
});
it('Should not lose records if flush throws an error and is a promise', function(done) {
var flush = sinon.stub();
flush.onCall(0).returns(Promise.reject(new Error()));
flush.onCall(1).returns(Promise.resolve());
var x51 = mod({
flush: flush
});
x51.push({});
// This one will throw an error
x51.flush();
expect(flush.callCount).to.eql(1);
var items1 = flush.getCall(0).args[0];
expect(items1.length).to.eql(1);
// Send another record
x51.push({});
setTimeout(function() {
// This one should succeed
x51.flush();
expect(flush.callCount).to.eql(2);
var items2 = flush.getCall(1).args[0];
expect(items2.length).to.eql(2); // Should have both records this time
done();
});
});
it('Should not flush automatically if the number of records is below the set threshold', function() {
var flush = sinon.stub();
var x51 = mod({
flush: flush,
maxRecords: 2
});
x51.push({});
expect(flush.callCount).to.eql(0);
});
it('Should proactively flush if the number of records passes the set threshold', function() {
var flush = sinon.stub();
var x51 = mod({
flush: flush,
maxRecords: 2
});
x51.push({});
x51.push({});
expect(flush.callCount).to.eql(1);
var items = flush.getCall(0).args[0];
expect(items.length).to.eql(2);
});
});