summaryrefslogtreecommitdiff
path: root/js/test/Ice/acm/Client.js
blob: 03186ac81ad6461782e0404d8de85d4bd49a8131 (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2018 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.
//
// **********************************************************************

(function(module, require, exports)
{
    const Ice = require("ice").Ice;
    const TestHelper = require("TestHelper").TestHelper;
    const Test = require("Test").Test;
    const test = TestHelper.test;

    class LoggerI
    {
        constructor(out)
        {
            this._messages = [];
            this._out = out;
        }

        print(msg)
        {
            this._messages.push(msg);
        }

        trace(category, message)
        {
            this._messages.push("[" + category + "] " + message);
        }

        warning(message)
        {
            this._messages.push("warning: " + message);
        }

        error(message)
        {
            this._messages.push("error: " + message);
        }

        cloneWithPrefix(prefix)
        {
            return this;
        }

        dump()
        {
            this._messages.forEach(message => this._out.writeLine(message));
            this._messages = [];
        }
    }

    class TestCase
    {
        constructor(name, com, out)
        {
            this._name = name;
            this._com = com;
            this._logger = new LoggerI(out);
            this._msg = null;

            this._clientACMTimeout = -1;
            this._clientACMClose = -1;
            this._clientACMHeartbeat = -1;

            this._serverACMTimeout = -1;
            this._serverACMClose = -1;
            this._serverACMHeartbeat = -1;

            this._heartbeat = 0;
            this._closed = false;
        }

        async init()
        {
            const initData = new Ice.InitializationData();
            initData.properties = this._com.ice_getCommunicator().getProperties().clone();
            initData.logger = this._logger;
            initData.properties.setProperty("Ice.ACM.Timeout", "2");
            if(this._clientACMTimeout >= 0)
            {
                initData.properties.setProperty("Ice.ACM.Client.Timeout", String(this._clientACMTimeout));
            }
            if(this._clientACMClose >= 0)
            {
                initData.properties.setProperty("Ice.ACM.Client.Close", String(this._clientACMClose));
            }
            if(this._clientACMHeartbeat >= 0)
            {
                initData.properties.setProperty("Ice.ACM.Client.Heartbeat", String(this._clientACMHeartbeat));
            }
            this._communicator = Ice.initialize(initData);

            this._adapter = await this._com.createObjectAdapter(this._serverACMTimeout,
                                                                this._serverACMClose,
                                                                this._serverACMHeartbeat);
        }

        async destroy()
        {
            await this._adapter.deactivate();
            await this._communicator.destroy();
        }

        join(out)
        {
            this._logger.dump();
            out.write("testing " + this._name + "... ");
            if(this._msg === null)
            {
                out.writeLine("ok");
            }
            else
            {
                out.writeLine("failed! " + this._msg);
                test(false);
            }
        }

        async start()
        {
            try
            {
                let prx = await this._adapter.getTestIntf();
                prx = Test.TestIntfPrx.uncheckedCast(this._communicator.stringToProxy(prx.toString()));
                const con = await prx.ice_getConnection();
                con.setCloseCallback(connection =>
                                     {
                                         this._closed = true;
                                     });
                con.setHeartbeatCallback(connection => ++this._heartbeat);
                await this.runTestCase(this._adapter, prx);
            }
            catch(ex)
            {
                this._msg = "unexpected exception:\n" + ex.toString() + "\n" + ex.stack;
            }
        }

        async waitForClosed()
        {
            const now = Date.now();
            while(!this._closed)
            {
                await Ice.Promise.delay(100);
                if(Date.now() - now >= 2000)
                {
                    test(false); // Waited for more than 2s for close, something's wrong.
                }
            }
        }

        runTestCase(adapter, proxy)
        {
            test(false); // Abstract
        }

        setClientACM(timeout, close, heartbeat)
        {
            this._clientACMTimeout = timeout;
            this._clientACMClose = close;
            this._clientACMHeartbeat = heartbeat;
        }

        setServerACM(timeout, close, heartbeat)
        {
            this._serverACMTimeout = timeout;
            this._serverACMClose = close;
            this._serverACMHeartbeat = heartbeat;
        }
    }

    class InvocationHeartbeatTest extends TestCase
    {
        constructor(com, out)
        {
            super("invocation heartbeat", com, out);
            this.setServerACM(1, -1, -1); // Faster ACM to make sure we receive enough ACM heartbeats
        }

        async runTestCase(adapter, proxy)
        {
            await proxy.sleep(4);
            test(this._heartbeat >= 4);
        }
    }

    class InvocationHeartbeatOnHoldTest extends TestCase
    {
        constructor(com, out)
        {
            super("invocation with heartbeat on hold", com, out);
            // Use default ACM configuration.
        }

        async runTestCase(adapter, proxy)
        {
            // When the OA is put on hold, connections shouldn't
            // send heartbeats, the invocation should therefore
            // fail.
            try
            {
                await proxy.sleepAndHold(10);
                test(false);
            }
            catch(ex)
            {
                await adapter.activate();
                await proxy.interruptSleep();
                await this.waitForClosed();
            }
        }
    }

    class InvocationNoHeartbeatTest extends TestCase
    {
        constructor(com, out)
        {
            super("invocation with no heartbeat", com, out);
            this.setServerACM(2, 2, 0); // Disable heartbeat on invocations
        }

        async runTestCase(adapter, proxy)
        {
            // Heartbeats are disabled on the server, the
            // invocation should fail since heartbeats are
            // expected.
            try
            {
                await proxy.sleep(10);
                test(false);
            }
            catch(ex)
            {
                await proxy.interruptSleep();
                await this.waitForClosed();
                test(this._heartbeat === 0);
            }
        }
    }

    class InvocationHeartbeatCloseOnIdleTest extends TestCase
    {
        constructor(com, out)
        {
            super("invocation with no heartbeat and close on idle", com, out);
            this.setClientACM(1, 1, 0); // Only close on idle.
            this.setServerACM(1, 2, 0); // Disable heartbeat on invocations
        }

        async runTestCase(adapter, proxy)
        {
            // No close on invocation, the call should succeed this
            // time.
            await proxy.sleep(3);
            test(this._heartbeat === 0);
            test(!this._closed);
        }
    }

    class CloseOnIdleTest extends TestCase
    {
        constructor(com, out)
        {
            super("close on idle", com, out);
            this.setClientACM(1, 1, 0); // Only close on idle
        }

        async runTestCase(adapter, proxy)
        {
            await Ice.Promise.delay(3000);
            await this.waitForClosed();
            test(this._heartbeat === 0);
        }
    }

    class CloseOnInvocationTest extends TestCase
    {
        constructor(com, out)
        {
            super("close on invocation", com, out);
            this.setClientACM(1, 2, 0); // Only close on invocation
        }

        async runTestCase(adapter, proxy)
        {
            await Ice.Promise.delay(3000);
            test(this._heartbeat === 0);
            test(!this._closed);
        }
    }

    class CloseOnIdleAndInvocationTest extends TestCase
    {
        constructor(com, out)
        {
            super("close on idle and invocation", com, out);
            this.setClientACM(1, 3, 0); // Only close on idle and invocation
        }

        async runTestCase(adapter, proxy)
        {
            //
            // Put the adapter on hold. The server will not respond to
            // the graceful close. This allows to test whether or not
            // the close is graceful or forceful.
            //
            await adapter.hold();
            await Ice.Promise.delay(3000);
            test(this._heartbeat === 0);
            test(!this._closed); // Not closed yet because of graceful close.
            await adapter.activate();
            await Ice.Promise.delay(1000);
            await this.waitForClosed(); // Connection should be closed this time.
        }
    }

    class ForcefullCloseOnIdleAndInvocationTest extends TestCase
    {
        constructor(com, out)
        {
            super("forcefull close on idle and invocation", com, out);
            this.setClientACM(1, 4, 0); // Only close on idle and invocation
        }

        async runTestCase(adapter, proxy)
        {
            await adapter.hold();
            await Ice.Promise.delay(3000);
            await this.waitForClosed();

            test(this._heartbeat === 0);
        }
    }

    class HeartbeatOnIdleTest extends TestCase
    {
        constructor(com, out)
        {
            super("heartbeat on idle", com, out);
            this.setServerACM(1, -1, 2); // Enable server heartbeats.
        }

        async runTestCase(adapter, proxy)
        {
            await Ice.Promise.delay(3000);
            return this._heartbeat >= 3;
        }
    }

    class HeartbeatAlwaysTest extends TestCase
    {
        constructor(com, out)
        {
            super("heartbeat always", com, out);
            this.setServerACM(1, -1, 3); // Enable server heartbeats.
        }

        async runTestCase(adapter, proxy)
        {
            for(let i = 0; i < 10; i++)
            {
                await proxy.ice_ping();
                await Ice.Promise.delay(300);
            }

            test(this._heartbeat >= 3);
        }
    }

    class HeartbeatManualTest extends TestCase
    {
        constructor(com, out)
        {
            super("manual heartbeats", com, out);
            //
            // Disable heartbeats.
            //
            this.setClientACM(10, -1, 0);
            this.setServerACM(10, -1, 0);
        }

        async runTestCase(adapter, proxy)
        {
            await proxy.startHeartbeatCount();
            const con = await proxy.ice_getConnection();
            await con.heartbeat();
            await con.heartbeat();
            await con.heartbeat();
            await con.heartbeat();
            await con.heartbeat();
            await proxy.waitForHeartbeatCount(5);
        }
    }

    class SetACMTest extends TestCase
    {
        constructor(com, out)
        {
            super("setACM/getACM", com, out);
            this.setClientACM(15, 4, 0);
        }

        async runTestCase(adapter, proxy)
        {
            const con = proxy.ice_getCachedConnection();

            try
            {
                con.setACM(-19, undefined, undefined);
                test(false);
            }
            catch(ex)
            {
            }

            let acm = con.getACM();
            test(acm.timeout === 15);
            test(acm.close === Ice.ACMClose.CloseOnIdleForceful);
            test(acm.heartbeat === Ice.ACMHeartbeat.HeartbeatOff);

            con.setACM(undefined, undefined, undefined);
            acm = con.getACM();
            test(acm.timeout === 15);
            test(acm.close === Ice.ACMClose.CloseOnIdleForceful);
            test(acm.heartbeat === Ice.ACMHeartbeat.HeartbeatOff);

            con.setACM(1, Ice.ACMClose.CloseOnInvocationAndIdle, Ice.ACMHeartbeat.HeartbeatAlways);
            acm = con.getACM();
            test(acm.timeout === 1);
            test(acm.close === Ice.ACMClose.CloseOnInvocationAndIdle);
            test(acm.heartbeat === Ice.ACMHeartbeat.HeartbeatAlways);

            await proxy.startHeartbeatCount();
            await proxy.waitForHeartbeatCount(2);

            await new Promise(
                (resolve, reject) =>
                    {
                        con.setCloseCallback(() => resolve());
                        con.close(Ice.ConnectionClose.Gracefully);
                    });

            await new Promise(
                (resolve, reject) =>
                    {
                        con.setCloseCallback(() => resolve());
                    });
            con.setHeartbeatCallback(c => test(false));
        }
    }

    class Client extends TestHelper
    {
        async allTests()
        {
            const communicator = this.communicator();
            const out = this.getWriter();
            const ref = "communicator:" + this.getTestEndpoint();
            const com = Test.RemoteCommunicatorPrx.uncheckedCast(communicator.stringToProxy(ref));

            const tests = [];
            //
            // Skip some tests with IE it opens too many connections and
            // IE doesn't allow more than 6 connections.
            //
            if(typeof navigator !== "undefined" &&
               ["MSIE", "Trident/7.0", "Edge/12", "Edge/13"].some(value => navigator.userAgent.indexOf(value) !== -1))
            {
                tests.push(new HeartbeatOnIdleTest(com, out));
                tests.push(new SetACMTest(com, out));
            }
            else
            {
                tests.push(new InvocationHeartbeatTest(com, out));
                tests.push(new InvocationHeartbeatOnHoldTest(com, out));
                tests.push(new InvocationNoHeartbeatTest(com, out));
                tests.push(new InvocationHeartbeatCloseOnIdleTest(com, out));

                tests.push(new CloseOnIdleTest(com, out));
                tests.push(new CloseOnInvocationTest(com, out));
                tests.push(new CloseOnIdleAndInvocationTest(com, out));
                tests.push(new ForcefullCloseOnIdleAndInvocationTest(com, out));

                tests.push(new HeartbeatOnIdleTest(com, out));
                tests.push(new HeartbeatAlwaysTest(com, out));
                tests.push(new HeartbeatManualTest(com, out));
                tests.push(new SetACMTest(com, out));
            }

            await Promise.all(tests.map(test => test.init()));
            await Promise.all(tests.map(test => test.start()));
            for(const test of tests)
            {
                test.join(out);
            }
            await Promise.all(tests.map(test => test.destroy()));

            out.write("shutting down... ");
            await com.shutdown();
            out.writeLine("ok");
        }

        async run(args)
        {
            let communicator;
            try
            {
                [communicator, args] = this.initialize(args);
                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 : this.Ice._require,
  typeof global !== "undefined" && typeof global.process !== "undefined" ? exports : this));