1// 2// Copyright (c) ZeroC, Inc. All rights reserved. 3// 4 5import {Ice} from "ice"; 6import {Test} from "./generated" 7import {TestHelper} from "../../../Common/TestHelper" 8const test = TestHelper.test; 9 10export class Client extends TestHelper 11{ 12 async allTests() 13 { 14 const communicator = this.communicator(); 15 const out = this.getWriter(); 16 17 let sref = "test:" + this.getTestEndpoint(); 18 let obj = communicator.stringToProxy(sref); 19 test(obj !== null); 20 const p = Test.TestIntfPrx.uncheckedCast(obj); 21 22 sref = "testController:" + this.getTestEndpoint(1); 23 obj = communicator.stringToProxy(sref); 24 test(obj !== null); 25 const testController = Test.TestIntfControllerPrx.uncheckedCast(obj); 26 27 out.write("testing batch requests with proxy... "); 28 { 29 const count = await p.opBatchCount(); 30 test(count === 0); 31 const b1 = p.ice_batchOneway(); 32 const bf = b1.opBatch(); 33 test(bf.isCompleted()); 34 test(!bf.isSent()); 35 b1.opBatch(); 36 await b1.ice_flushBatchRequests(); 37 await p.waitForBatch(2); 38 await b1.ice_flushBatchRequests(); 39 } 40 out.writeLine("ok"); 41 42 out.write("testing batch requests with connection... "); 43 { 44 test(await p.opBatchCount() === 0); 45 const b1 = p.ice_batchOneway(); 46 await b1.opBatch(); 47 const bf = b1.opBatch(); 48 test(bf.isCompleted()); 49 await b1.ice_flushBatchRequests(); 50 test(await p.waitForBatch(2)); 51 } 52 53 if(await p.ice_getConnection() !== null) 54 { 55 test(await p.opBatchCount() == 0); 56 const b1 = p.ice_batchOneway(); 57 await b1.opBatch(); 58 await b1.ice_getConnection().then(conn => conn.close(Ice.ConnectionClose.GracefullyWithWait)); 59 await b1.ice_flushBatchRequests(); 60 test(await p.waitForBatch(1)); 61 } 62 out.writeLine("ok"); 63 64 out.write("testing batch requests with communicator... "); 65 { 66 // 67 // Async task - 1 connection. 68 // 69 test(await p.opBatchCount() === 0); 70 const b1 = Test.TestIntfPrx.uncheckedCast( 71 await p.ice_getConnection().then(c => c.createProxy(p.ice_getIdentity()).ice_batchOneway())); 72 await b1.opBatch(); 73 await b1.opBatch(); 74 75 await communicator.flushBatchRequests(Ice.CompressBatch.No); 76 test(await p.waitForBatch(2)); 77 } 78 79 { 80 // 81 // Async task exception - 1 connection. 82 // 83 test(await p.opBatchCount() === 0); 84 const b1 = Test.TestIntfPrx.uncheckedCast( 85 await p.ice_getConnection().then(c => c.createProxy(p.ice_getIdentity()).ice_batchOneway())); 86 await b1.opBatch(); 87 await b1.ice_getConnection().then(c => c.close(Ice.ConnectionClose.GracefullyWithWait)); 88 89 await communicator.flushBatchRequests(Ice.CompressBatch.No); 90 test(await p.opBatchCount() == 0); 91 } 92 93 { 94 // 95 // Async task - 2 connections. 96 // 97 test(await p.opBatchCount() === 0); 98 const b1 = Test.TestIntfPrx.uncheckedCast( 99 await p.ice_getConnection().then(c => c.createProxy(p.ice_getIdentity()).ice_batchOneway())); 100 const b2 = Test.TestIntfPrx.uncheckedCast( 101 await p.ice_connectionId("2").ice_getConnection().then( 102 c => c.createProxy(p.ice_getIdentity()).ice_batchOneway())); 103 104 await b2.ice_getConnection(); // Ensure connection is established. 105 await b1.opBatch(); 106 await b1.opBatch(); 107 await b2.opBatch(); 108 await b2.opBatch(); 109 110 await communicator.flushBatchRequests(Ice.CompressBatch.No); 111 test(await p.waitForBatch(4)); 112 } 113 114 { 115 // 116 // AsyncResult exception - 2 connections - 1 failure. 117 // 118 // All connections should be flushed even if there are failures on some connections. 119 // Exceptions should not be reported. 120 // 121 test(await p.opBatchCount() === 0); 122 const b1 = Test.TestIntfPrx.uncheckedCast( 123 await p.ice_getConnection().then(c => c.createProxy(p.ice_getIdentity()).ice_batchOneway())); 124 const b2 = Test.TestIntfPrx.uncheckedCast( 125 await p.ice_connectionId("2").ice_getConnection().then( 126 c => c.createProxy(p.ice_getIdentity()).ice_batchOneway())); 127 await b2.ice_getConnection(); // Ensure connection is established. 128 await b1.opBatch(); 129 await b2.opBatch(); 130 await b1.ice_getConnection().then(c => c.close(Ice.ConnectionClose.GracefullyWithWait)); 131 132 await communicator.flushBatchRequests(Ice.CompressBatch.No); 133 test(await p.waitForBatch(1)); 134 } 135 136 { 137 // 138 // Async task exception - 2 connections - 2 failures. 139 // 140 // The sent callback should be invoked even if all connections fail. 141 // 142 test(await p.opBatchCount() == 0); 143 const b1 = Test.TestIntfPrx.uncheckedCast( 144 await p.ice_getConnection().then(c => c.createProxy(p.ice_getIdentity()).ice_batchOneway())); 145 const b2 = Test.TestIntfPrx.uncheckedCast( 146 await p.ice_connectionId("2").ice_getConnection().then( 147 c => c.createProxy(p.ice_getIdentity()).ice_batchOneway())); 148 await b2.ice_getConnection(); // Ensure connection is established. 149 await b1.opBatch(); 150 await b2.opBatch(); 151 await b1.ice_getConnection().then(c => c.close(Ice.ConnectionClose.GracefullyWithWait)); 152 await b2.ice_getConnection().then(c => c.close(Ice.ConnectionClose.GracefullyWithWait)); 153 154 await communicator.flushBatchRequests(Ice.CompressBatch.No); 155 test(await p.opBatchCount() === 0); 156 } 157 out.writeLine("ok"); 158 159 out.write("testing AsyncResult operations... "); 160 { 161 await testController.holdAdapter(); 162 let r1:Ice.AsyncResult<void>; 163 let r2:Ice.AsyncResult<void>; 164 try 165 { 166 r1 = p.op(); 167 const seq = new Uint8Array(100000); 168 while(true) 169 { 170 r2 = p.opWithPayload(seq); 171 if(r2.sentSynchronously()) 172 { 173 await Ice.Promise.delay(0); 174 } 175 else 176 { 177 break; 178 } 179 } 180 181 if(await p.ice_getConnection() !== null) 182 { 183 test(r1.sentSynchronously() && r1.isSent() && !r1.isCompleted() || 184 !r1.sentSynchronously() && !r1.isCompleted()); 185 186 test(!r2.sentSynchronously() && !r2.isCompleted()); 187 188 test(!r1.isCompleted()); 189 test(!r2.isCompleted()); 190 } 191 } 192 finally 193 { 194 await testController.resumeAdapter(); 195 } 196 197 await r1; 198 test(r1.isSent()); 199 test(r1.isCompleted()); 200 201 await r2; 202 test(r2.isSent()); 203 test(r2.isCompleted()); 204 205 test(r1.operation == "op"); 206 test(r2.operation == "opWithPayload"); 207 208 let r = p.ice_ping(); 209 test(r.operation === "ice_ping"); 210 test(r.connection === null); // Expected 211 test(r.communicator == communicator); 212 test(r.proxy == p); 213 214 // 215 // Oneway 216 // 217 let p2 = p.ice_oneway(); 218 r = p2.ice_ping(); 219 test(r.operation === "ice_ping"); 220 test(r.connection === null); // Expected 221 test(r.communicator == communicator); 222 test(r.proxy == p2); 223 224 // 225 // Batch request via proxy 226 // 227 p2 = p.ice_batchOneway(); 228 p2.ice_ping(); 229 { 230 let r2 = p2.ice_flushBatchRequests(); 231 test(r2.operation === "ice_flushBatchRequests"); 232 test(r2.connection === null); // Expected 233 test(r2.communicator == communicator); 234 test(r2.proxy == p2); 235 } 236 237 const con = p.ice_getCachedConnection(); 238 p2 = p.ice_batchOneway(); 239 p2.ice_ping(); 240 { 241 let r2 = con.flushBatchRequests(Ice.CompressBatch.No); 242 test(r2.operation === "flushBatchRequests"); 243 test(r2.connection == con); 244 test(r2.communicator == communicator); 245 test(r2.proxy === null); 246 } 247 248 p2 = p.ice_batchOneway(); 249 p2.ice_ping(); 250 { 251 let r2 = communicator.flushBatchRequests(Ice.CompressBatch.No); 252 test(r2.operation === "flushBatchRequests"); 253 test(r2.connection === null); 254 test(r2.communicator == communicator); 255 test(r2.proxy === null); 256 } 257 } 258 259 { 260 await testController.holdAdapter(); 261 const seq = new Uint8Array(new Array(100000)); 262 let r; 263 while(true) 264 { 265 r = p.opWithPayload(seq); 266 if(r.sentSynchronously()) 267 { 268 await Ice.Promise.delay(0); 269 } 270 else 271 { 272 break; 273 } 274 } 275 276 test(!r.isSent()); 277 278 const r1 = p.ice_ping(); 279 r1.then( 280 () => test(false), 281 ex => test(ex instanceof Ice.InvocationCanceledException)); 282 283 const r2 = p.ice_id(); 284 r2.then( 285 () => test(false), 286 ex => test(ex instanceof Ice.InvocationCanceledException)); 287 288 r1.cancel(); 289 r2.cancel(); 290 291 await testController.resumeAdapter(); 292 await p.ice_ping(); 293 294 test(!r1.isSent() && r1.isCompleted()); 295 test(!r2.isSent() && r2.isCompleted()); 296 } 297 298 { 299 await testController.holdAdapter(); 300 301 const r1 = p.op(); 302 const r2 = p.ice_id(); 303 304 await p.ice_oneway().ice_ping(); 305 306 r1.cancel(); 307 r1.then( 308 () => test(false), 309 ex => test(ex instanceof Ice.InvocationCanceledException)); 310 311 r2.cancel(); 312 r2.then( 313 () => test(false), 314 ex => test(ex instanceof Ice.InvocationCanceledException)); 315 316 await testController.resumeAdapter(); 317 } 318 out.writeLine("ok"); 319 320 await p.shutdown(); 321 } 322 323 async run(args:string[]) 324 { 325 let communicator:Ice.Communicator; 326 try 327 { 328 [communicator, args] = this.initialize(args); 329 await this.allTests(); 330 } 331 finally 332 { 333 if(communicator) 334 { 335 await communicator.destroy(); 336 } 337 } 338 } 339} 340