-
Notifications
You must be signed in to change notification settings - Fork 58
/
HeartbeatFromServerTest.js
147 lines (135 loc) · 5.8 KB
/
HeartbeatFromServerTest.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
/*
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
*
* 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 RC = require('../RC');
var HazelcastClient = require('../../').Client;
var Config = require('../../').Config;
var Util = require('../Util');
var DeferredPromise = require('../../lib/Util').DeferredPromise;
var Address = require('../../').Address;
describe('Heartbeat', function () {
this.timeout(50000);
var cluster;
beforeEach(function () {
return RC.createCluster(null, null).then(function (resp) {
cluster = resp;
});
});
afterEach(function () {
return RC.shutdownCluster(cluster.id);
});
it('Heartbeat stopped fired when second member stops heartbeating', function (done) {
var client;
var memberAddedPromise = new DeferredPromise();
RC.startMember(cluster.id).then(function () {
var cfg = new Config.ClientConfig();
cfg.properties['hazelcast.client.heartbeat.interval'] = 500;
cfg.properties['hazelcast.client.heartbeat.timeout'] = 2000;
return HazelcastClient.newHazelcastClient(cfg);
}).then(function (resp) {
client = resp;
}).then(function () {
var membershipListener = {
memberAdded: function (membershipEvent) {
var address = new Address(membershipEvent.member.address.host, membershipEvent.member.address.port);
warmUpConnectionToAddressWithRetry(client, address);
memberAddedPromise.resolve();
}
};
client.clusterService.addMembershipListener(membershipListener);
client.heartbeat.addListener({
onHeartbeatStopped: function (connection) {
client.shutdown();
done();
}
})
}).then(function () {
return RC.startMember(cluster.id);
}).then(function (member2) {
return memberAddedPromise.promise.then(function () {
simulateHeartbeatLost(client, member2.host + ':' + member2.port, 2000);
})
}).catch(done);
});
it('Heartbeat restored fired when second member comes back', function (done) {
var client;
var member1;
var member2;
RC.startMember(cluster.id).then(function (m) {
member1 = m;
var cfg = new Config.ClientConfig();
cfg.properties['hazelcast.client.heartbeat.interval'] = 500;
cfg.properties['hazelcast.client.heartbeat.timeout'] = 2000;
return HazelcastClient.newHazelcastClient(cfg);
}).then(function (resp) {
client = resp;
var membershipListener = {
memberAdded: function (membershipEvent) {
warmUpConnectionToAddressWithRetry(client, membershipEvent.member.address, 3).then(function () {
simulateHeartbeatLost(client, membershipEvent.member.address, 2000);
}).catch(done);
}
};
client.clusterService.addMembershipListener(membershipListener);
client.heartbeat.addListener({
onHeartbeatRestored: function (connection) {
client.shutdown();
done();
}
});
return RC.startMember(cluster.id);
}).then(function (resp) {
member2 = resp;
}).catch(done);
});
it('emits shutdown when lost connection to cluster', function (done) {
var member;
RC.startMember(cluster.id).then(function (m) {
member = m;
var cfg = new Config.ClientConfig();
cfg.properties['hazelcast.client.heartbeat.interval'] = 500;
cfg.properties['hazelcast.client.heartbeat.timeout'] = 2000;
return HazelcastClient.newHazelcastClient(cfg);
}).then(function (client) {
var expected = 'shuttingDown';
client.lifecycleService.on('lifecycleEvent', function (state) {
if (state === 'shuttingDown' && expected === 'shuttingDown') {
expected = 'shutdown';
} else if (state === 'shutdown' && expected === 'shutdown') {
done();
} else {
done('Expected ' + expected + '. Got ' + state);
}
});
RC.terminateMember(cluster.id, member.uuid);
});
});
function simulateHeartbeatLost(client, address, timeout) {
client.connectionManager.establishedConnections[address].lastReadTimeMillis = client.connectionManager.establishedConnections[address].getLastReadTimeMillis() - timeout;
}
function warmUpConnectionToAddressWithRetry(client, address, retryCount) {
return client.connectionManager.getOrConnect(address).then(function (conn) {
if (conn != null) {
return conn;
} else if (conn == null && retryCount > 0) {
return Util.promiseWaitMilliseconds(300).then(function () {
return warmUpConnectionToAddressWithRetry(client, address, retryCount - 1);
});
} else {
throw new Error('Could not warm up connection to ' + address);
}
});
}
});