-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathclient.test.js
More file actions
299 lines (258 loc) · 9.24 KB
/
client.test.js
File metadata and controls
299 lines (258 loc) · 9.24 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2014 Joyent, Inc. All rights reserved.
var fast = require('../lib');
var test = require('tape').test;
///--- Globals
var HOST = process.env.TEST_HOST || '127.0.0.1';
var PORT = process.env.TEST_PORT || 12345;
// a bogus loopback address should work for testing connect timeout
var TIMEOUT_HOST = process.env.TEST_TIMEOUT_HOST || '127.1.1.1';
var TIMEOUT_MS = 1000;
var client;
var server;
///--- Tests
test('connect timeout', function (t) {
function done() {
client.removeAllListeners();
client.close();
t.end();
}
client = fast.createClient({
host: TIMEOUT_HOST,
port: PORT,
connectTimeout: TIMEOUT_MS
});
var failTimer = setTimeout(function () {
t.ok(false, 'timeout failed');
done();
}, TIMEOUT_MS * 2);
client.once('connectError', function (err) {
t.equal(err.name, 'ConnectionTimeoutError', 'timeout error');
clearTimeout(failTimer);
done();
});
});
test('close suppress connectErrors', function (t) {
client = fast.createClient({
host: TIMEOUT_HOST,
port: PORT,
connectTimeout: TIMEOUT_MS
});
client.on('connectError', function (err) {
t.fail('error not suppressed');
});
setImmediate(function () {
client.close();
setTimeout(function () {
t.ok(true);
t.end();
}, TIMEOUT_MS);
});
});
test('connect retry limit', function (t) {
var targetCount = 3;
var realCount = 0;
client = fast.createClient({
host: HOST,
port: PORT,
retry: {
retries: targetCount
}
});
client.on('connectError', function (err) {
realCount++;
});
client.once('error', function (err) {
// The first failure is not a retry
t.equal(realCount, targetCount+1, 'retry count');
client.close();
t.end();
});
});
test('countPending', function (t) {
server = fast.createServer();
server.rpc('sleep', function (timeout, res) {
setTimeout(function () {
res.end(null);
}, parseInt(timeout, 10));
});
server.listen(PORT, function () {
client = fast.createClient({
host: 'localhost',
port: PORT
});
client.once('connect', function () {
client.rpc('sleep', 900);
client.rpc('sleep', 1900);
client.rpc('sleep', 2900);
var expected = 3;
function check() {
t.equal(expected, client.countPending);
if (expected === 0) {
client.close();
server.close();
t.end();
} else {
expected--;
setTimeout(check, 1000);
}
}
check();
});
});
//test
});
test('RPC error on close', function (t) {
server = fast.createServer();
server.rpc('slow', function (res) {
// Don't respond to simulate indefinite hang
});
server.listen(PORT, function () {
client = fast.createClient({
host: HOST,
port: PORT
});
client.once('connect', function () {
t.pass('connected');
var res = client.rpc('slow');
res.on('error', function (err) {
t.equal(err.name, 'ConnectionClosedError');
server.close();
t.end();
});
setImmediate(function () {
t.pass('closing');
client.close();
});
});
});
});
test('RPC error when not connected', function (t) {
server = fast.createServer();
server.rpc('pass', function (res) {
res.end(null);
});
server.listen(PORT, function () {
client = fast.createClient({
host: HOST,
port: PORT,
reconnect: false
});
client.once('connect', function () {
// Simulate server close
client.fast_conn.destroy();
});
client.once('close', function () {
var res = client.rpc('pass');
res.once('error', function (err) {
t.ok(err);
t.equal(err.name, 'NoConnectionError');
server.close();
client.close();
t.end();
});
res.on('end', t.fail.bind(t, 'end called'));
});
});
});
// Regression test for https://smartos.org/bugview/MORAY-324.
test('socket properly hangs up via close', function (t) {
var client1EmittedClose = false;
server = fast.createServer();
server.listen(PORT, function () {
// Create a first client that we'll close right away.
// The goal is to reproduce the issue described by MORAY-324 where
// a client that would be immediately closed before establishing
// a connection would _not_ be closed, and would still connect
// to the server.
client = fast.createClient({
host: HOST,
port: PORT
});
client.close();
client.on('connect', function onClosedClientConnect() {
t.ok(false, 'closed client should not emit connect event');
});
client.on('close', function onClient1Close() {
client1EmittedClose = true;
});
// Create a second client, only for the purpose of making
// sure that the first one has the time to connect if the bug
// described in MORAY-324 is still present.
var client2 = fast.createClient({
host: HOST,
port: PORT
});
// Close the second client as soon as it connects so that it
// doesn't hold the libuv event loop open and allows the server
// to close (and thus the test to end) if the first client
// manages to close its connection.
client2.on('connect', function onClient2Connected() {
client2.close();
});
client2.on('close', function onClient2Closed() {
t.equal(client1EmittedClose, true, 'first client should ' +
'have emitted close');
// Use a timeout to check for the number of current connections
// on the server, as when client2 closed its connection, the
// other end of the connection may not have closed yet.
// Delay of 1000ms is arbitrary, but should be enough to let
// the server side of the connection to close, and the
// net.Server's .connections property to be updated.
setTimeout(function waitForClientsClose() {
server.srv.getConnections(function (err, nbConnections) {
t.ifError(err, 'getConnections should not result in ' +
'an error');
t.equal(nbConnections, 0,
'after second client closed, server should have ' +
'no remaining client connected');
});
// When the second client closes, if the bug described by
// MORAY-324 is still present, the first client will have
// established a connection, and the server won't be able to
// close since the first client will never close.
// If MORAY-324 is fixed, the first client will have closed
// its connection before it's established, and thus as soon
// as the second client closes its connection, the server
// can close and the test can end.
server.close();
server.on('close', function onServerClosed() {
t.end();
});
}, 1000);
});
});
});
test('client timeout should be cleared on close', function (t) {
server = fast.createServer();
server.listen(PORT, function () {
server.srv.unref();
client = fast.createClient({
host: HOST,
port: PORT
});
client.close();
client.on('close', function onClient1Close() {
t.equal(process._getActiveHandles().length, 0,
'there should be no active handle left after client ' +
'closed its connection');
server.close();
server.on('close', function onServerClose() {
t.end();
});
});
});
});
// This is essentially a regression test for
// https://smartos.org/bugview/CNAPI-648.
test('close does not assert', function (t) {
// Arguments passed to createClient below are set just to that the client
// creation does not assert. The port and connectTimeout arguments do not
// have a specific purpose, since the test closes the client before it had a
// chance to establish any connection anyway.
client = fast.createClient({
port: PORT,
connectTimeout: TIMEOUT_MS
});
client.close();
t.end();
});