summaryrefslogtreecommitdiff
path: root/js/test/Ice/ami/Client.js
blob: 1b77396b72ee4b051752a4501f2556d9f1b6aaed (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
// **********************************************************************
//
// Copyright (c) 2003-2017 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)
{
    var Ice = require("ice").Ice;
    var Test = require("Test").Test;
    var Promise = Ice.Promise;

    function allTests(communicator, out)
    {
        var promise = new Ice.Promise();
        var test = function(b)
        {
            if(!b)
            {
                try
                {
                    throw new Error("test failed");
                }
                catch(err)
                {
                    promise.reject(err);
                    throw err;
                }
            }
        };

        var getConnectionBatchProxy = function(proxy, connectionId)
        {
            if(!connectionId)
            {
                connectionId = "";
            }
            var p = proxy;
            return p.ice_connectionId(connectionId).ice_getConnection().then(c =>
                {
                    p = p.constructor.uncheckedCast(c.createProxy(proxy.ice_getIdentity())).ice_batchOneway();
                    return p.ice_getConnection();
                }
            ).then(c =>
                {
                    test(p.ice_getCachedConnection() === c);
                    return p;
                });
        };

        var result = null;
        var p = Test.TestIntfPrx.uncheckedCast(communicator.stringToProxy("test:default -p 12010"));
        var testController =
            Test.TestIntfControllerPrx.uncheckedCast(communicator.stringToProxy("testController:default -p 12011"));
        var r = null;
        var b1 = null;
        var b2 = null;

        Promise.try(() =>
            {
                out.write("testing batch requests with proxy... ");
                return p.opBatchCount().then(count =>
                    {
                        test(count === 0);
                        b1 = p.ice_batchOneway();
                        test(b1.opBatch());
                        test(b1.opBatch());
                        return b1.ice_flushBatchRequests();
                    }
                ).then(() => p.waitForBatch(2)
                ).then(() => b1.ice_flushBatchRequests()
                ).then(() => out.writeLine("ok"));
            }
        ).then(() =>
            {
                out.write("testing batch requests with connection... ");
                return p.opBatchCount().then(count =>
                    {
                        test(count === 0);
                        return getConnectionBatchProxy(p).then(prx =>
                            {
                                b1 = prx;
                                var connection = b1.ice_getCachedConnection();
                                test(b1.opBatch());
                                test(b1.opBatch());
                                return connection.flushBatchRequests();
                            });
                    }
                ).then(() => p.waitForBatch(2)
                ).then(() => b1.ice_getConnection().then(connection => connection.flushBatchRequests())
                ).then(() => out.writeLine("ok"));
            }
        ).then(() =>
            {
                out.write("testing batch requests with communicator... ");
                return p.opBatchCount().then(count =>
                    {
                        test(count === 0);
                        test(b1.opBatch());
                        test(b1.opBatch());
                        return communicator.flushBatchRequests().then(() => p.waitForBatch(2))
                                                                .then(() => p.opBatchCount());
                    }
                ).then(batchCount =>
                    {
                        //
                        // AsyncResult exception - 1 connection.
                        //
                        test(batchCount === 0);
                        b1.opBatch();
                        b1.ice_getCachedConnection().close(Ice.ConnectionClose.GracefullyWithWait);
                        return communicator.flushBatchRequests().then(() => p.opBatchCount());
                    }
                ).then(batchCount =>
                    {
                        //
                        // AsyncResult exception - 2 connections
                        //
                        test(batchCount === 0);
                        return getConnectionBatchProxy(p).then(prx =>
                            {
                                b1 = prx;
                                return getConnectionBatchProxy(p, "2");
                            }
                        ).then(prx => // Ensure connection is established.
                            {
                                b2 = prx;
                                b1.opBatch();
                                b1.opBatch();
                                b2.opBatch();
                                b2.opBatch();
                                return communicator.flushBatchRequests();
                            }
                        ).then(() => p.waitForBatch(4))
                         .then(() => p.opBatchCount());
                    }
                ).then(batchCount =>
                    {
                        //
                        // AsyncResult exception - 2 connections - 1 failure.
                        //
                        // All connections should be flushed even if there are failures on some connections.
                        // Exceptions should not be reported.
                        //
                        test(batchCount === 0);
                        return getConnectionBatchProxy(p).then(prx =>
                            {
                                b1 = prx;
                                return getConnectionBatchProxy(p, "2");
                            }
                        ).then(prx => // Ensure connection is established.
                            {
                                b2 = prx;
                                b1.opBatch();
                                b2.opBatch();
                                b1.ice_getCachedConnection().close(Ice.ConnectionClose.GracefullyWithWait);
                                return communicator.flushBatchRequests();
                            }
                        ).then(() => p.waitForBatch(1)
                        ).then(() => p.opBatchCount());
                    }
                ).then(batchCount =>
                    {
                        //
                        // AsyncResult exception - 2 connections - 2 failures.
                        //
                        // All connections should be flushed even if there are failures on some connections.
                        // Exceptions should not be reported.
                        //
                        test(batchCount === 0);
                        return getConnectionBatchProxy(p).then(prx =>
                            {
                                b1 = prx;
                                return getConnectionBatchProxy(p, "2");
                            }
                        ).then(prx => // Ensure connection is established.
                            {
                                b2 = prx;
                                b1.opBatch();
                                b2.opBatch();
                                b1.ice_getCachedConnection().close(Ice.ConnectionClose.GracefullyWithWait);
                                b2.ice_getCachedConnection().close(Ice.ConnectionClose.GracefullyWithWait);
                                return communicator.flushBatchRequests();
                            }
                        ).then(() => p.opBatchCount());
                    }
                ).then(batchCount =>
                    {
                        test(batchCount === 0);
                        out.writeLine("ok");
                    });
            }
        ).then(() =>
            {
                out.write("testing AsyncResult operations... ");

                var indirect = Test.TestIntfPrx.uncheckedCast(p.ice_adapterId("dummy"));

                return indirect.op().catch(ex => test(ex instanceof Ice.NoEndpointException)
                ).then(() => testController.holdAdapter()
                ).then(() =>
                    {
                        var r1 = p.op();
                        var r2 = null;
                        var seq = Ice.Buffer.createNative(new Array(100000));

                        while((r2 = p.opWithPayload(seq)).sentSynchronously());
                        test(r1.sentSynchronously() && r1.isSent() && !r1.isCompleted() ||
                             !r1.sentSynchronously() && !r1.isCompleted());

                        test(!r2.sentSynchronously() && !r2.isCompleted());

                        testController.resumeAdapter();

                        test(r1.operation === "op");
                        test(r2.operation === "opWithPayload");

                        return r1.then(() =>
                            {
                                test(r1.isSent());
                                test(r1.isCompleted());
                                return r2;
                            }
                        ).then(() =>
                            {
                                test(r2.isSent());
                                test(r2.isCompleted());
                            });
                    }
                ).then(() =>
                    {
                        r = p.ice_ping();
                        test(r.operation === "ice_ping");
                        test(r.connection === null); // Expected
                        test(r.communicator == communicator);
                        test(r.proxy == p);

                        //
                        // Oneway
                        //
                        var p2 = p.ice_oneway();
                        r = p2.ice_ping();
                        test(r.operation === "ice_ping");
                        test(r.connection === null); // Expected
                        test(r.communicator == communicator);
                        test(r.proxy == p2);

                        //
                        // Batch request via proxy
                        //
                        p2 = p.ice_batchOneway();
                        p2.ice_ping();
                        r = p2.ice_flushBatchRequests();
                        test(r.operation === "ice_flushBatchRequests");
                        test(r.connection === null); // Expected
                        test(r.communicator == communicator);
                        test(r.proxy == p2);

                        var con = p.ice_getCachedConnection();
                        p2 = p.ice_batchOneway();
                        p2.ice_ping();
                        r = con.flushBatchRequests();
                        test(r.operation === "flushBatchRequests");
                        test(r.connection == con);
                        test(r.communicator == communicator);
                        test(r.proxy === null);

                        p2 = p.ice_batchOneway();
                        p2.ice_ping();
                        r = communicator.flushBatchRequests();
                        test(r.operation === "flushBatchRequests");
                        test(r.connection === null);
                        test(r.communicator == communicator);
                        test(r.proxy === null);
                    }
                ).then(() => testController.holdAdapter()
                ).then(() =>
                    {
                        var seq = Ice.Buffer.createNative(new Array(100000));
                        while((r = p.opWithPayload(seq)).sentSynchronously());
                        test(!r.isSent());

                        var r1 = p.ice_ping();
                        r1.then(
                            () => test(false),
                            (ex) => test(ex instanceof Ice.InvocationCanceledException));

                        var r2 = p.ice_id();
                        r2.then(
                            () => test(false),
                            (ex) => test(ex instanceof Ice.InvocationCanceledException));

                        r1.cancel();
                        r2.cancel();

                        return testController.resumeAdapter()
                            .then(() => p.ice_ping())
                            .then(() =>
                                {
                                    test(!r1.isSent() && r1.isCompleted());
                                    test(!r2.isSent() && r2.isCompleted());
                                });
                    }
                ).then(() => testController.holdAdapter()
                ).then(() =>
                    {
                        var r1 = p.op();
                        var r2 = p.ice_id();
                        return p.ice_oneway().ice_ping().then(() =>
                            {
                                r1.cancel();
                                r1.then(
                                    () => test(false),
                                    (ex) => test(ex instanceof Ice.InvocationCanceledException));

                                r2.cancel();
                                r2.then(
                                    () => test(false),
                                    (ex) => test(ex instanceof Ice.InvocationCanceledException));

                                return testController.resumeAdapter();
                            });
                    }
                ).then(() => out.writeLine("ok"));
            }
        ).then(
            () => p.shutdown(),
            ex =>
            {
                console.log("unexpected exception:\n" + ex);
                test(false);
            }
        ).then(promise.resolve, promise.reject);
        return promise;
    }

    exports._test = function(out, id)
    {
        var communicator = Ice.initialize(id);
        return Promise.try(() =>
            {
                if(typeof(navigator) !== 'undefined' && isSafari() && isWorker())
                {
                    out.writeLine("Test not supported with Safari web workers.");
                    return Test.TestIntfPrx.uncheckedCast(
                        communicator.stringToProxy("test:default -p 12010")).shutdown();
                }
                else
                {
                    return allTests(communicator, out);
                }
            }).finally(() => communicator.destroy());
    };
    exports._runServer = true;
}
(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));