forked from stomp-js/stompjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heart-beat.spec.js
116 lines (97 loc) · 3.06 KB
/
heart-beat.spec.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
/*
These tests wrap a web socket and force introduce errors.
In this case the wrapper eats away pings.
Typically either side, when they are expecting pings, will wait for 2*heartbeat interval before closing.
RabbitMQ does not support heartbeat intervals of less than 1000ms.
So, altogether, these tests will each take slightly more than 2000ms each.
*/
describe('Ping', function () {
let client;
beforeEach(function () {
client = stompClient();
});
afterEach(async function () {
await disconnectStomp(client);
});
// Find length -
const length = data => {
return typeof data === 'string' ? data.length : data.byteLength;
};
// See https://github.com/stomp-js/stompjs/issues/188
it('Should allow server to not send heartbeat header', function (done) {
client.webSocketFactory = () => {
const wrapperWS = new WrapperWS(new WebSocket(client.brokerURL));
let inComingFrame;
const onFrame = frame => {
inComingFrame = frame;
};
const onIncomingPing = () => {};
const parser = new StompJs.Parser(onFrame, onIncomingPing);
wrapperWS.ws.onmessage = ev => {
parser.parseChunk(ev.data);
if (inComingFrame.command === 'CONNECTED') {
const frame = StompJs.FrameImpl.fromRawFrame(
inComingFrame,
this._escapeHeaderValues
);
delete frame.headers['heart-beat'];
ev = { data: frame.serialize() };
}
wrapperWS.onmessage(ev);
};
return wrapperWS;
};
client.onConnect = () => {
done();
};
client.activate();
});
const incomingPingTest = function (done) {
client.heartbeatIncoming = 1000;
client.heartbeatOutgoing = 0;
client.webSocketFactory = () => {
const wrapperWS = new WrapperWS(new WebSocket(client.brokerURL));
wrapperWS.ws.onmessage = ev => {
// Eat away incoming ping
if (length(ev.data) === 1) {
console.log('Eating incoming ping');
return;
}
wrapperWS.onmessage(ev);
};
return wrapperWS;
};
client.onWebSocketClose = ev => {
console.log('here');
done();
};
client.activate();
};
it('Should close connection when no incoming ping', incomingPingTest);
describe('With discardWebsocketOnCommFailure', function () {
beforeEach(function () {
client.discardWebsocketOnCommFailure = true;
});
it('Should close connection when no incoming ping', incomingPingTest);
});
it('Should close connection when no outgoing ping', function (done) {
client.heartbeatIncoming = 0;
client.heartbeatOutgoing = 1000;
client.webSocketFactory = () => {
const wrapperWS = new WrapperWS(new WebSocket(client.brokerURL));
wrapperWS.send = data => {
// Eat away outgoing ping
if (length(data) === 1) {
console.log('Eating outgoing ping');
return;
}
wrapperWS.ws.send(data);
};
return wrapperWS;
};
client.onWebSocketClose = ev => {
done();
};
client.activate();
});
});