-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmessage_queue.test.js
90 lines (67 loc) · 1.92 KB
/
message_queue.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
'use strict';
const assert = require('assert');
describe('MessageQueue', () => {
let agent;
beforeEach(() => {
agent = global.agent;
});
describe('flush()', () => {
it('should send messages and empty the queue', (done) => {
let lastPayload;
agent.apiRequest = {
post: function(endpoint, payload, callback) {
lastPayload = payload;
setTimeout(() => {
callback(null);
assert.equal(agent.messageQueue.queue.length, 0);
done();
}, 1);
}
};
let m = {
'm0': 1
};
agent.messageQueue.add('t0', m);
agent.messageQueue.queue[0]['added_at'] = Date.now() - 20 * 60 * 1000;
m = {
'm1': 1
};
agent.messageQueue.add('t1', m)
m = {
'm2': 2
};
agent.messageQueue.add('t1', m)
agent.messageQueue.flush(false, () => {
assert.equal(lastPayload['messages'][0]['content']['m1'], 1);
assert.equal(lastPayload['messages'][1]['content']['m2'], 2);
});
});
it('should fail to send messages and restore the queue', (done) => {
agent.apiRequest = {
post: function(endpoint, payload, callback) {
setTimeout(() => {
callback(new Error('some error'));
assert.equal(agent.messageQueue.queue[0]['content']['m1'], 1);
assert.equal(agent.messageQueue.queue[1]['content']['m2'], 2);
assert.equal(agent.messageQueue.queue[2]['content']['m3'], 3);
assert.equal(agent.messageQueue.queue.length, 3);
done();
}, 1);
}
};
let m = {
'm1': 1
};
agent.messageQueue.add('t1', m)
m = {
'm2': 2
};
agent.messageQueue.add('t1', m)
agent.messageQueue.flush(false, (err) => {});
m = {
'm3': 3
};
agent.messageQueue.add('t1', m)
});
});
});