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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
// **********************************************************************
//
// Copyright (c) 2003-present ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
/* global
isSafari : false,
isWorker : false
*/
(function(module, require, exports)
{
const Ice = require("ice").Ice;
const Test = require("Test").Test;
const TestHelper = require("TestHelper").TestHelper;
const ArrayUtil = Ice.ArrayUtil;
const test = TestHelper.test;
const isBrowser = (typeof window !== 'undefined' || typeof WorkerGlobalScope !== 'undefined');
const isConnectionFailed = ex =>
(!isBrowser && ex instanceof Ice.ConnectionRefusedException) ||
(isBrowser && ex instanceof Ice.ConnectFailedException) ||
(ex instanceof Ice.ConnectTimeoutException);
class Client extends TestHelper
{
async allTests()
{
async function createTestIntfPrx(adapters)
{
let endpoints = [];
let obj = null;
for(const adapter of adapters)
{
obj = await adapter.getTestIntf();
endpoints = endpoints.concat(obj.ice_getEndpoints());
}
return Test.TestIntfPrx.uncheckedCast(obj.ice_endpoints(endpoints));
}
async function deactivate(communicator, adapters)
{
for(const adapter of adapters)
{
await communicator.deactivateObjectAdapter(adapter);
}
}
const out = this.getWriter();
let communicator = this.communicator();
const properties = communicator.getProperties().clone();
out.write("testing stringToProxy... ");
const ref = "communicator:" + this.getTestEndpoint();
let com = Test.RemoteCommunicatorPrx.uncheckedCast(communicator.stringToProxy(ref));
test(com !== null);
out.writeLine("ok");
out.write("testing binding with single endpoint... ");
{
const adapter = await com.createObjectAdapter("Adapter", "default");
const test1 = await adapter.getTestIntf();
const test2 = await adapter.getTestIntf();
test(await test1.ice_getConnection() === await test2.ice_getConnection());
await test1.ice_ping();
await test2.ice_ping();
await com.deactivateObjectAdapter(adapter);
const test3 = Test.TestIntfPrx.uncheckedCast(test1);
test(await test3.ice_getConnection() === await test1.ice_getConnection());
test(await test3.ice_getConnection() === await test2.ice_getConnection());
try
{
await test3.ice_ping();
test(false);
}
catch(ex)
{
test(isConnectionFailed(ex), ex);
}
}
out.writeLine("ok");
await communicator.destroy();
[communicator] = this.initialize(properties);
com = Test.RemoteCommunicatorPrx.uncheckedCast(communicator.stringToProxy(ref));
out.write("testing binding with multiple endpoints... ");
{
const adapters = await Promise.all(
[
com.createObjectAdapter("Adapter11", "default"),
com.createObjectAdapter("Adapter12", "default"),
com.createObjectAdapter("Adapter13", "default")
]);
//
// Ensure that when a connection is opened it's reused for new
// proxies and that all endpoints are eventually tried.
//
let names = ["Adapter11", "Adapter12", "Adapter13"];
while(names.length > 0)
{
const adpts = ArrayUtil.clone(adapters);
const test1 = await createTestIntfPrx(adpts);
ArrayUtil.shuffle(adpts);
const test2 = await createTestIntfPrx(adpts);
ArrayUtil.shuffle(adpts);
const test3 = await createTestIntfPrx(adpts);
await test1.ice_ping();
test(await test1.ice_getConnection() == await test2.ice_getConnection());
test(await test2.ice_getConnection() == await test3.ice_getConnection());
const name = await test1.getAdapterName();
const index = names.indexOf(name);
if(index !== -1)
{
names.splice(index, 1);
}
const conn = await test1.ice_getConnection();
await conn.close(Ice.ConnectionClose.GracefullyWithWait);
}
//
// Ensure that the proxy correctly caches the connection (we
// always send the request over the same connection.)
//
{
for(const adpt of adapters)
{
const prx = await adpt.getTestIntf();
await prx.ice_ping();
}
const t = await createTestIntfPrx(adapters);
const name = await t.getAdapterName();
const nRetry = 10;
let i;
/* eslint-disable curly */
for(i = 0; i < nRetry && await t.getAdapterName() == name; i++);
/* eslint-enable curly */
test(i == nRetry);
for(const adpt of adapters)
{
const prx = await adpt.getTestIntf();
const conn = await prx.ice_getConnection();
await conn.close(Ice.ConnectionClose.GracefullyWithWait);
}
}
//
// Deactivate an adapter and ensure that we can still
// establish the connection to the remaining adapters.
//
await com.deactivateObjectAdapter(adapters[0]);
names = ["Adapter12", "Adapter13"];
while(names.length > 0)
{
const adpts = ArrayUtil.clone(adapters);
const test1 = await createTestIntfPrx(adpts);
ArrayUtil.shuffle(adpts);
const test2 = await createTestIntfPrx(adpts);
ArrayUtil.shuffle(adpts);
const test3 = await createTestIntfPrx(adpts);
test(await test1.ice_getConnection() == await test2.ice_getConnection());
test(await test2.ice_getConnection() == await test3.ice_getConnection());
const name = await test1.getAdapterName();
const index = names.indexOf(name);
if(index !== -1)
{
names.splice(index, 1);
}
const conn = await test1.ice_getConnection();
await conn.close(Ice.ConnectionClose.GracefullyWithWait);
}
//
// Deactivate an adapter and ensure that we can still
// establish the connection to the remaining adapter.
//
await com.deactivateObjectAdapter(adapters[2]);
const obj = await createTestIntfPrx(adapters);
test(await obj.getAdapterName() == "Adapter12");
await deactivate(com, adapters);
}
out.writeLine("ok");
await communicator.destroy();
[communicator] = this.initialize(properties);
com = Test.RemoteCommunicatorPrx.uncheckedCast(communicator.stringToProxy(ref));
//
// Skip this test with IE it open too many connections IE doesn't allow more
// than 6 connections. Firefox has a configuration that causes failed connections
// to be delayed on retry (network.websocket.delay-failed-reconnects=true by
// default), so we prefer to also disable this test with Firefox.
//
if(typeof navigator === "undefined" ||
["MSIE", "Trident/7.0", "Firefox"].every(value => navigator.userAgent.indexOf(value) === -1))
{
out.write("testing binding with multiple random endpoints... ");
const adapters = await Promise.all(
[
com.createObjectAdapter("AdapterRandom11", "default"),
com.createObjectAdapter("AdapterRandom12", "default"),
com.createObjectAdapter("AdapterRandom13", "default"),
com.createObjectAdapter("AdapterRandom14", "default"),
com.createObjectAdapter("AdapterRandom15", "default")
]);
let count = 20;
let adapterCount = adapters.length;
while(--count > 0)
{
const proxies = [];
if(count == 1)
{
await com.deactivateObjectAdapter(adapters[4]);
--adapterCount;
}
for(let i = 0; i < proxies.length; ++i)
{
let adpts = new Array(Math.floor(Math.random() * adapters.length));
if(adpts.length == 0)
{
adpts = new Array(1);
}
for(let j = 0; j < adpts.length; ++j)
{
adpts[j] = adapters[Math.floor(Math.random() * adapters.length)];
}
proxies[i] = await createTestIntfPrx(ArrayUtil.clone(adpts));
}
for(let i = 0; i < proxies.length; i++)
{
proxies[i].getAdapterName().catch(
ex =>
{
// ignore exception
});
}
for(let i = 0; i < proxies.length; i++)
{
try
{
await proxies[i].ice_ping();
}
catch(ex)
{
test(ex instanceof Ice.LocalException, ex);
}
}
const connections = [];
for(let i = 0; i < proxies.length; i++)
{
if(proxies[i].ice_getCachedConnection() !== null)
{
if(connections.indexOf(proxies[i].ice_getCachedConnection()) === -1)
{
connections.push(proxies[i].ice_getCachedConnection());
}
}
}
test(connections.length <= adapterCount);
for(const a of adapters)
{
try
{
const prx = await a.getTestIntf();
const conn = await prx.ice_getConnection();
await conn.close(Ice.ConnectionClose.GracefullyWithWait);
}
catch(ex)
{
// Expected if adapter is down.
test(ex instanceof Ice.LocalException, ex);
}
}
}
out.writeLine("ok");
}
await communicator.destroy();
[communicator] = this.initialize(properties);
com = Test.RemoteCommunicatorPrx.uncheckedCast(communicator.stringToProxy(ref));
out.write("testing random endpoint selection... ");
{
const adapters = await Promise.all(
[
com.createObjectAdapter("Adapter21", "default"),
com.createObjectAdapter("Adapter22", "default"),
com.createObjectAdapter("Adapter23", "default")
]);
let obj = await createTestIntfPrx(adapters);
test(obj.ice_getEndpointSelection() == Ice.EndpointSelectionType.Random);
let names = ["Adapter21", "Adapter22", "Adapter23"];
while(names.length > 0)
{
const index = names.indexOf(await obj.getAdapterName());
if(index !== -1)
{
names.splice(index, 1);
}
const conn = await obj.ice_getConnection();
await conn.close(Ice.ConnectionClose.GracefullyWithWait);
}
obj = Test.TestIntfPrx.uncheckedCast(obj.ice_endpointSelection(Ice.EndpointSelectionType.Random));
test(obj.ice_getEndpointSelection() == Ice.EndpointSelectionType.Random);
names = ["Adapter21", "Adapter22", "Adapter23"];
while(names.length > 0)
{
const index = names.indexOf(await obj.getAdapterName());
if(index !== -1)
{
names.splice(index, 1);
}
const conn = await obj.ice_getConnection();
await conn.close(Ice.ConnectionClose.GracefullyWithWait);
}
await deactivate(com, adapters);
}
out.writeLine("ok");
await communicator.destroy();
[communicator] = this.initialize(properties);
com = Test.RemoteCommunicatorPrx.uncheckedCast(communicator.stringToProxy(ref));
out.write("testing ordered endpoint selection... ");
{
let adapters = await Promise.all(
[
com.createObjectAdapter("Adapter31", "default"),
com.createObjectAdapter("Adapter32", "default"),
com.createObjectAdapter("Adapter33", "default")
]);
let obj = await createTestIntfPrx(adapters);
obj = Test.TestIntfPrx.uncheckedCast(obj.ice_endpointSelection(Ice.EndpointSelectionType.Ordered));
test(obj.ice_getEndpointSelection() == Ice.EndpointSelectionType.Ordered);
const nRetry = 3;
//
// Ensure that endpoints are tried in order by deactiving the adapters
// one after the other.
//
/* eslint-disable curly */
let i;
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter31"; i++);
test(i == nRetry);
await com.deactivateObjectAdapter(adapters[0]);
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter32"; i++);
test(i == nRetry);
await com.deactivateObjectAdapter(adapters[1]);
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter33"; i++);
test(i == nRetry);
await com.deactivateObjectAdapter(adapters[2]);
/* eslint-enable curly */
try
{
await obj.getAdapterName();
}
catch(ex)
{
test(isConnectionFailed(ex), ex);
}
const endpoints = obj.ice_getEndpoints();
adapters = [];
//
// Now, re-activate the adapters with the same endpoints in the opposite
// order.
//
/* eslint-disable curly */
adapters.push(await com.createObjectAdapter("Adapter36", endpoints[2].toString()));
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter36"; i++);
test(i == nRetry);
let conn = await obj.ice_getConnection();
await conn.close(Ice.ConnectionClose.GracefullyWithWait);
adapters.push(await com.createObjectAdapter("Adapter35", endpoints[1].toString()));
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter35"; i++);
test(i == nRetry);
conn = await obj.ice_getConnection();
await conn.close(Ice.ConnectionClose.GracefullyWithWait);
adapters.push(await com.createObjectAdapter("Adapter34", endpoints[0].toString()));
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter34"; i++);
test(i == nRetry);
/* eslint-enable curly */
await deactivate(com, adapters);
}
out.writeLine("ok");
await communicator.destroy();
[communicator] = this.initialize(properties);
com = Test.RemoteCommunicatorPrx.uncheckedCast(communicator.stringToProxy(ref));
out.write("testing per request binding with single endpoint... ");
{
const adapter = await com.createObjectAdapter("Adapter41", "default");
const test1 = Test.TestIntfPrx.uncheckedCast((await adapter.getTestIntf()).ice_connectionCached(false));
const test2 = Test.TestIntfPrx.uncheckedCast((await adapter.getTestIntf()).ice_connectionCached(false));
test(!test1.ice_isConnectionCached());
test(!test2.ice_isConnectionCached());
test(await test1.ice_getConnection() !== null && await test2.ice_getConnection() !== null);
test(await test1.ice_getConnection() == await test2.ice_getConnection());
await test1.ice_ping();
await com.deactivateObjectAdapter(adapter);
const test3 = Test.TestIntfPrx.uncheckedCast(test1);
try
{
test(await test3.ice_getConnection() == await test1.ice_getConnection());
test(false);
}
catch(ex)
{
test(isConnectionFailed(ex), ex);
}
}
out.writeLine("ok");
await communicator.destroy();
[communicator] = this.initialize(properties);
com = Test.RemoteCommunicatorPrx.uncheckedCast(communicator.stringToProxy(ref));
out.write("testing per request binding with multiple endpoints... ");
{
const adapters = await Promise.all(
[
com.createObjectAdapter("Adapter51", "default"),
com.createObjectAdapter("Adapter52", "default"),
com.createObjectAdapter("Adapter53", "default")
]);
const obj = Test.TestIntfPrx.uncheckedCast(
(await createTestIntfPrx(adapters)).ice_connectionCached(false));
test(!obj.ice_isConnectionCached());
let names = ["Adapter51", "Adapter52", "Adapter53"];
while(names.length > 0)
{
const name = await obj.getAdapterName();
const index = names.indexOf(name);
if(index !== -1)
{
names.splice(index, 1);
}
}
await com.deactivateObjectAdapter(adapters[0]);
names = ["Adapter52", "Adapter53"];
while(names.length > 0)
{
const name = await obj.getAdapterName();
const index = names.indexOf(name);
if(index !== -1)
{
names.splice(index, 1);
}
}
await com.deactivateObjectAdapter(adapters[2]);
test(await obj.getAdapterName() == "Adapter52");
await deactivate(com, adapters);
}
out.writeLine("ok");
if(typeof navigator === "undefined" ||
["Firefox", "MSIE", "Trident/7.0"].every(value => navigator.userAgent.indexOf(value) === -1))
{
//
// Firefox adds a delay on websocket failed reconnects that causes this test to take too
// much time to complete.
//
// You can set network.websocket.delay-failed-reconnects to false in Firefox about:config
// to disable this behaviour
//
await communicator.destroy();
[communicator] = this.initialize(properties);
com = Test.RemoteCommunicatorPrx.uncheckedCast(communicator.stringToProxy(ref));
out.write("testing per request binding and ordered endpoint selection... ");
let adapters = await Promise.all(
[
com.createObjectAdapter("Adapter61", "default"),
com.createObjectAdapter("Adapter62", "default"),
com.createObjectAdapter("Adapter63", "default")
]);
let obj = await createTestIntfPrx(adapters);
obj = Test.TestIntfPrx.uncheckedCast(obj.ice_endpointSelection(Ice.EndpointSelectionType.Ordered));
test(obj.ice_getEndpointSelection() == Ice.EndpointSelectionType.Ordered);
obj = Test.TestIntfPrx.uncheckedCast(obj.ice_connectionCached(false));
test(!obj.ice_isConnectionCached());
const nRetry = 3;
let i;
//
// Ensure that endpoints are tried in order by deactiving the adapters
// one after the other.
//
/* eslint-disable curly */
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter61"; i++);
test(i == nRetry);
await com.deactivateObjectAdapter(adapters[0]);
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter62"; i++);
test(i == nRetry);
await com.deactivateObjectAdapter(adapters[1]);
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter63"; i++);
test(i == nRetry);
com.deactivateObjectAdapter(adapters[2]);
/* eslint-enable curly */
try
{
await obj.getAdapterName();
}
catch(ex)
{
test(isConnectionFailed(ex), ex);
}
const endpoints = obj.ice_getEndpoints();
adapters = [];
//
// Now, re-activate the adapters with the same endpoints in the opposite
// order.
//
/* eslint-disable curly */
adapters.push(await com.createObjectAdapter("Adapter66", endpoints[2].toString()));
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter66"; i++);
test(i == nRetry);
adapters.push(await com.createObjectAdapter("Adapter65", endpoints[1].toString()));
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter65"; i++);
test(i == nRetry);
adapters.push(await com.createObjectAdapter("Adapter64", endpoints[0].toString()));
for(i = 0; i < nRetry && await obj.getAdapterName() == "Adapter64"; i++);
test(i == nRetry);
/* eslint-enable curly */
await deactivate(com, adapters);
out.writeLine("ok");
}
await com.shutdown();
}
async run(args)
{
let communicator;
try
{
const out = this.getWriter();
[communicator, args] = this.initialize(args);
if(typeof navigator !== 'undefined' && isSafari() && isWorker())
{
//
// BUGFIX:
//
// With Safari 9.1 and WebWorkers, this test hangs in communicator destruction. The
// web socket send() method never returns for the sending of close connection message.
//
out.writeLine("Test not supported with Safari web workers.");
const obj = communicator.stringToProxy("communicator:" + this.getTestEndpoint());
const prx = await Test.RemoteCommunicatorPrx.uncheckedCast(obj);
await prx.shutdown();
}
else
{
await this.allTests();
}
}
finally
{
if(communicator)
{
await communicator.destroy();
}
}
}
}
exports.Client = Client;
}(typeof global !== "undefined" && typeof global.process !== "undefined" ? module : undefined,
typeof global !== "undefined" && typeof global.process !== "undefined" ? require :
(typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope) ? self.Ice._require : window.Ice._require,
typeof global !== "undefined" && typeof global.process !== "undefined" ? exports :
(typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope) ? self : window));
|