public static void allTests(global::Test.TestHelper helper, bool collocated) { Communicator communicator = helper.communicator(); var p = TestIntfPrx.Parse($"test:{helper.getTestEndpoint(0)}", communicator); var testController = TestIntfControllerPrx.Parse($"testController:{helper.getTestEndpoint(1)}", communicator); var output = helper.getWriter(); output.Write("testing async invocation..."); output.Flush(); { Dictionary <string, string> ctx = new Dictionary <string, string>(); test(p.IceIsAAsync("::Test::TestIntf").Result); test(p.IceIsAAsync("::Test::TestIntf", ctx).Result); p.IcePingAsync().Wait(); p.IcePingAsync(ctx).Wait(); test(p.IceIdAsync().Result.Equals("::Test::TestIntf")); test(p.IceIdAsync(ctx).Result.Equals("::Test::TestIntf")); test(p.IceIdsAsync().Result.Length == 2); test(p.IceIdsAsync(ctx).Result.Length == 2); if (!collocated) { test(p.GetConnectionAsync().Result != null); } p.opAsync().Wait(); p.opAsync(ctx).Wait(); test(p.opWithResultAsync().Result == 15); test(p.opWithResultAsync(ctx).Result == 15); try { p.opWithUEAsync().Wait(); test(false); } catch (AggregateException ae) { ae.Handle(ex => ex is Test.TestIntfException); } try { p.opWithUEAsync(ctx).Wait(); test(false); } catch (AggregateException ae) { ae.Handle(ex => ex is Test.TestIntfException); } } output.WriteLine("ok"); output.Write("testing async/await..."); output.Flush(); { Task.Run(async() => { Dictionary <string, string> ctx = new Dictionary <string, string>(); test(await p.IceIsAAsync("::Test::TestIntf")); test(await p.IceIsAAsync("::Test::TestIntf", ctx)); await p.IcePingAsync(); await p.IcePingAsync(ctx); var id = await p.IceIdAsync(); test(id.Equals("::Test::TestIntf")); id = await p.IceIdAsync(ctx); test(id.Equals("::Test::TestIntf")); var ids = await p.IceIdsAsync(); test(ids.Length == 2); ids = await p.IceIdsAsync(ctx); test(ids.Length == 2); if (!collocated) { var conn = await p.GetConnectionAsync(); test(conn != null); } await p.opAsync(); await p.opAsync(ctx); var result = await p.opWithResultAsync(); test(result == 15); result = await p.opWithResultAsync(ctx); test(result == 15); try { await p.opWithUEAsync(); test(false); } catch (System.Exception ex) { test(ex is Test.TestIntfException); } try { await p.opWithUEAsync(ctx); test(false); } catch (System.Exception ex) { test(ex is Test.TestIntfException); } }).Wait(); } output.WriteLine("ok"); output.Write("testing async continuations..."); output.Flush(); { Dictionary <string, string> ctx = new Dictionary <string, string>(); p.IceIsAAsync("::Test::TestIntf").ContinueWith(previous => { test(previous.Result); }).Wait(); p.IceIsAAsync("::Test::TestIntf", ctx).ContinueWith(previous => { test(previous.Result); }).Wait(); p.IcePingAsync().ContinueWith(previous => { previous.Wait(); }).Wait(); p.IcePingAsync(ctx).ContinueWith(previous => { previous.Wait(); }).Wait(); p.IceIdAsync().ContinueWith(previous => { test(previous.Result.Equals("::Test::TestIntf")); }).Wait(); p.IceIdAsync(ctx).ContinueWith(previous => { test(previous.Result.Equals("::Test::TestIntf")); }).Wait(); p.IceIdsAsync().ContinueWith(previous => { test(previous.Result.Length == 2); }).Wait(); p.IceIdsAsync(ctx).ContinueWith(previous => { test(previous.Result.Length == 2); }).Wait(); if (!collocated) { p.GetConnectionAsync().ContinueWith(previous => { test(previous.Result != null); }).Wait(); } p.opAsync().ContinueWith(previous => previous.Wait()).Wait(); p.opAsync(ctx).ContinueWith(previous => previous.Wait()).Wait(); p.opWithResultAsync().ContinueWith(previous => { test(previous.Result == 15); }).Wait(); p.opWithResultAsync(ctx).ContinueWith(previous => { test(previous.Result == 15); }).Wait(); p.opWithUEAsync().ContinueWith(previous => { try { previous.Wait(); } catch (AggregateException ae) { ae.Handle(ex => ex is Test.TestIntfException); } }).Wait(); p.opWithUEAsync(ctx).ContinueWith(previous => { try { previous.Wait(); } catch (AggregateException ae) { ae.Handle(ex => ex is Test.TestIntfException); } }).Wait(); } output.WriteLine("ok"); output.Write("testing local exceptions with async tasks... "); output.Flush(); { TestIntfPrx indirect = p.Clone(adapterId: "dummy"); try { indirect.opAsync().Wait(); test(false); } catch (AggregateException ae) { ae.Handle((ex) => { return(ex is NoEndpointException); }); } try { p.Clone(oneway: true).opWithResultAsync(); test(false); } catch (TwowayOnlyException) { } // // Check that CommunicatorDestroyedException is raised directly. // if (p.GetConnection() != null) { Communicator ic = helper.initialize(communicator.GetProperties()); IObjectPrx o = IObjectPrx.Parse(p.ToString(), ic); TestIntfPrx p2 = Test.TestIntfPrx.CheckedCast(o); ic.destroy(); try { p2.opAsync(); test(false); } catch (CommunicatorDestroyedException) { // Expected. } } } output.WriteLine("ok"); output.Write("testing exception with async task... "); output.Flush(); { TestIntfPrx i = p.Clone(adapterId: "dummy"); try { i.IceIsAAsync("::Test::TestIntf").Wait(); test(false); } catch (AggregateException) { } try { i.opAsync().Wait(); test(false); } catch (AggregateException) { } try { i.opWithResultAsync().Wait(); test(false); } catch (AggregateException) { } try { i.opWithUEAsync().Wait(); test(false); } catch (AggregateException) { } // Ensures no exception is called when response is received test(p.IceIsAAsync("::Test::TestIntf").Result); p.opAsync().Wait(); p.opWithResultAsync().Wait(); // If response is a user exception, it should be received. try { p.opWithUEAsync().Wait(); test(false); } catch (AggregateException ae) { ae.Handle((ex) => { return(ex is Test.TestIntfException); }); } } output.WriteLine("ok"); output.Write("testing progress callback... "); output.Flush(); { { SentCallback cb = new SentCallback(); Task t = p.IceIsAAsync("", progress: new Progress(sentSynchronously => { cb.sent(sentSynchronously); })); cb.check(); t.Wait(); t = p.IcePingAsync( progress: new Progress(sentSynchronously => { cb.sent(sentSynchronously); })); cb.check(); t.Wait(); t = p.IceIdAsync( progress: new Progress(sentSynchronously => { cb.sent(sentSynchronously); })); cb.check(); t.Wait(); t = p.IceIdsAsync( progress: new Progress(sentSynchronously => { cb.sent(sentSynchronously); })); cb.check(); t.Wait(); t = p.opAsync( progress: new Progress(sentSynchronously => { cb.sent(sentSynchronously); })); cb.check(); t.Wait(); } List <Task> tasks = new List <Task>(); byte[] seq = new byte[10024]; (new Random()).NextBytes(seq); testController.holdAdapter(); try { Task t = null; ProgresCallback cb; do { cb = new ProgresCallback(); t = p.opWithPayloadAsync(seq, progress: cb); tasks.Add(t); }while (cb.SentSynchronously); } finally { testController.resumeAdapter(); } foreach (Task t in tasks) { t.Wait(); } } output.WriteLine("ok"); output.Write("testing async/await... "); output.Flush(); Func <Task> task = async() => { try { await p.opAsync(); var r = await p.opWithResultAsync(); test(r == 15); try { await p.opWithUEAsync(); } catch (Test.TestIntfException) { } // Operations implemented with amd and async. await p.opAsyncDispatchAsync(); r = await p.opWithResultAsyncDispatchAsync(); test(r == 15); try { await p.opWithUEAsyncDispatchAsync(); test(false); } catch (Test.TestIntfException) { } } catch (OperationNotExistException) { // Expected with cross testing, this opXxxAsyncDispatch methods are C# only. } }; task().Wait(); output.WriteLine("ok"); if (p.GetConnection() != null) { output.Write("testing async Task cancellation... "); output.Flush(); { var cs1 = new CancellationTokenSource(); var cs2 = new CancellationTokenSource(); var cs3 = new CancellationTokenSource(); Task t1; Task t2; Task t3; try { testController.holdAdapter(); ProgresCallback cb = null; byte[] seq = new byte[10024]; for (int i = 0; i < 200; ++i) // 2MB { cb = new ProgresCallback(); p.opWithPayloadAsync(seq, progress: cb); } test(!cb.Sent); t1 = p.IcePingAsync(cancel: cs1.Token); t2 = p.IcePingAsync(cancel: cs2.Token); cs3.Cancel(); t3 = p.IcePingAsync(cancel: cs3.Token); cs1.Cancel(); cs2.Cancel(); try { t1.Wait(); test(false); } catch (AggregateException ae) { ae.Handle(ex => { return(ex is InvocationCanceledException); }); } try { t2.Wait(); test(false); } catch (AggregateException ae) { ae.Handle(ex => { return(ex is InvocationCanceledException); }); } try { t3.Wait(); test(false); } catch (AggregateException ae) { ae.Handle(ex => { return(ex is InvocationCanceledException); }); } } finally { testController.resumeAdapter(); p.IcePing(); } } output.WriteLine("ok"); } if (p.GetConnection() != null && p.supportsAMD()) { output.Write("testing graceful close connection with wait... "); output.Flush(); { // // Local case: begin a request, close the connection gracefully, and make sure it waits // for the request to complete. // Connection con = p.GetConnection(); CallbackBase cb = new CallbackBase(); con.setCloseCallback(_ => { cb.called(); }); Task t = p.sleepAsync(100); con.close(ConnectionClose.GracefullyWithWait); t.Wait(); // Should complete successfully. cb.check(); } { // // Remote case. // byte[] seq = new byte[1024 * 10]; // // Send multiple opWithPayload, followed by a close and followed by multiple opWithPaylod. // The goal is to make sure that none of the opWithPayload fail even if the server closes // the connection gracefully in between. // int maxQueue = 2; bool done = false; while (!done && maxQueue < 50) { done = true; p.IcePing(); List <Task> results = new List <Task>(); for (int i = 0; i < maxQueue; ++i) { results.Add(p.opWithPayloadAsync(seq)); } ProgresCallback cb = new ProgresCallback(); p.closeAsync(Test.CloseMode.GracefullyWithWait, progress: cb); if (!cb.SentSynchronously) { for (int i = 0; i < maxQueue; i++) { cb = new ProgresCallback(); Task t = p.opWithPayloadAsync(seq, progress: cb); results.Add(t); if (cb.SentSynchronously) { done = false; maxQueue *= 2; break; } } } else { maxQueue *= 2; done = false; } foreach (Task q in results) { q.Wait(); } } } output.WriteLine("ok"); output.Write("testing graceful close connection without wait... "); output.Flush(); { // // Local case: start an operation and then close the connection gracefully on the client side // without waiting for the pending invocation to complete. There will be no retry and we expect the // invocation to fail with ConnectionManuallyClosedException. // p = p.Clone(connectionId: "CloseGracefully"); // Start with a new connection. Connection con = p.GetConnection(); CallbackBase cb = new CallbackBase(); Task t = p.startDispatchAsync( progress: new Progress(sentSynchronously => { cb.called(); })); cb.check(); // Ensure the request was sent before we close the connection. con.close(ConnectionClose.Gracefully); try { t.Wait(); test(false); } catch (System.AggregateException ex) { test(ex.InnerException is ConnectionManuallyClosedException); test((ex.InnerException as ConnectionManuallyClosedException).graceful); } p.finishDispatch(); // // Remote case: the server closes the connection gracefully, which means the connection // will not be closed until all pending dispatched requests have completed. // con = p.GetConnection(); cb = new CallbackBase(); con.setCloseCallback(_ => { cb.called(); }); t = p.sleepAsync(100); p.close(Test.CloseMode.Gracefully); // Close is delayed until sleep completes. cb.check(); t.Wait(); } output.WriteLine("ok"); output.Write("testing forceful close connection... "); output.Flush(); { // // Local case: start an operation and then close the connection forcefully on the client side. // There will be no retry and we expect the invocation to fail with ConnectionManuallyClosedException. // p.IcePing(); Connection con = p.GetConnection(); CallbackBase cb = new CallbackBase(); Task t = p.startDispatchAsync( progress: new Progress(sentSynchronously => { cb.called(); })); cb.check(); // Ensure the request was sent before we close the connection. con.close(ConnectionClose.Forcefully); try { t.Wait(); test(false); } catch (AggregateException ex) { test(ex.InnerException is ConnectionManuallyClosedException); test(!(ex.InnerException as ConnectionManuallyClosedException).graceful); } p.finishDispatch(); // // Remote case: the server closes the connection forcefully. This causes the request to fail // with a ConnectionLostException. Since the close() operation is not idempotent, the client // will not retry. // try { p.close(Test.CloseMode.Forcefully); test(false); } catch (ConnectionLostException) { // Expected. } } output.WriteLine("ok"); } output.Write("testing ice_scheduler... "); output.Flush(); { p.IcePingAsync().ContinueWith( (t) => { test(Thread.CurrentThread.Name == null || !Thread.CurrentThread.Name.Contains("ThreadPool.Client")); }).Wait(); p.IcePingAsync().ContinueWith( (t) => { test(Thread.CurrentThread.Name.Contains("ThreadPool.Client")); }, p.Scheduler).Wait(); { TaskCompletionSource <int> s1 = new TaskCompletionSource <int>(); TaskCompletionSource <int> s2 = new TaskCompletionSource <int>(); Task t1 = s1.Task; Task t2 = s2.Task; Task t3 = null; Task t4 = null; p.IcePingAsync().ContinueWith( (t) => { test(Thread.CurrentThread.Name.Contains("ThreadPool.Client")); // // t1 Continuation run in the thread that completes it. // var id = Thread.CurrentThread.ManagedThreadId; t3 = t1.ContinueWith(prev => { test(id == Thread.CurrentThread.ManagedThreadId); }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, p.Scheduler); s1.SetResult(1); // // t2 completed from the main thread // t4 = t2.ContinueWith(prev => { test(id != Thread.CurrentThread.ManagedThreadId); test(Thread.CurrentThread.Name == null || !Thread.CurrentThread.Name.Contains("ThreadPool.Client")); }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, p.Scheduler); }, p.Scheduler).Wait(); s2.SetResult(1); Task.WaitAll(t1, t2, t3, t4); } if (!collocated) { ObjectAdapter adapter = communicator.createObjectAdapter(""); PingReplyI replyI = new PingReplyI(); var reply = adapter.Add(replyI); adapter.Activate(); p.GetConnection().setAdapter(adapter); p.pingBiDir(reply); test(replyI.checkReceived()); adapter.Destroy(); } } output.WriteLine("ok"); output.Write("testing result struct... "); output.Flush(); { var q = Test.Outer.Inner.TestIntfPrx.Parse($"test2:{helper.getTestEndpoint(0)}", communicator); q.opAsync(1).ContinueWith(t => { var r = t.Result; test(r.returnValue == 1); test(r.j == 1); }).Wait(); } output.WriteLine("ok"); p.shutdown(); }
public static void allTests(Test.TestHelper helper) { var output = helper.getWriter(); Communicator communicator = helper.communicator(); string sref = "test:" + helper.getTestEndpoint(0); var obj = IObjectPrx.Parse(sref, communicator); test(obj != null); TestIntfPrx p = TestIntfPrx.UncheckedCast(obj); sref = "testController:" + helper.getTestEndpoint(1); obj = IObjectPrx.Parse(sref, communicator); test(obj != null); var testController = TestIntfControllerPrx.UncheckedCast(obj); output.Write("testing dispatcher with continuations... "); output.Flush(); { p.op(); Callback cb = new Callback(output); Action <Task> continuation = (Task previous) => { try { previous.Wait(); cb.response(); } catch (AggregateException ex) { cb.exception((Ice.Exception)ex.InnerException); } }; // We use sleepAsync instead of opAsync to ensure the response isn't received before // we setup the continuation var t = p.sleepAsync(500).ContinueWith(continuation, TaskContinuationOptions.ExecuteSynchronously); t.Wait(); cb.check(); var i = p.Clone(connectionId: "dummy"); // // sleepAsync doesn't help here as the test will fail with Ice.NoEndpointException and sleepAsync // will not be called. // //i.sleepAsync(500).ContinueWith(continuation, TaskContinuationOptions.ExecuteSynchronously).Wait(); //cb.check(); // // Expect InvocationTimeoutException. // { // The continuation might be (rarely) executed on the current thread if the setup of the // continuation occurs after the invocation timeout. var thread = Thread.CurrentThread; TestIntfPrx to = p.Clone(invocationTimeout: 20); to.sleepAsync(500).ContinueWith( previous => { try { previous.Wait(); test(false); } catch (AggregateException ex) { test(ex.InnerException is Ice.InvocationTimeoutException); test(Dispatcher.isDispatcherThread() || thread == Thread.CurrentThread); } }, TaskContinuationOptions.ExecuteSynchronously).Wait(); } // // Repeat using the proxy scheduler in this case we don't need to call sleepAsync, continuations // are waranted to run with the dispatcher even if not executed synchronously. // t = p.opAsync().ContinueWith(continuation, p.Scheduler); t.Wait(); cb.check(); i.opAsync().ContinueWith(continuation, i.Scheduler).Wait(); cb.check(); // // Expect InvocationTimeoutException. // { TestIntfPrx to = p.Clone(invocationTimeout: 10); to.sleepAsync(500).ContinueWith( previous => { try { previous.Wait(); test(false); } catch (AggregateException ex) { test(ex.InnerException is Ice.InvocationTimeoutException); test(Dispatcher.isDispatcherThread()); } }, p.Scheduler).Wait(); } // // Hold adapter to ensure the invocations don't complete synchronously // Also disable collocation optimization on p // testController.holdAdapter(); var p2 = p.Clone(collocationOptimized: false); Action <Task> continuation2 = (Task previous) => { test(Dispatcher.isDispatcherThread()); try { previous.Wait(); } catch (AggregateException ex) { test(ex.InnerException is Ice.CommunicatorDestroyedException); } }; byte[] seq = new byte[10 * 1024]; (new Random()).NextBytes(seq); Progress sentSynchronously; do { sentSynchronously = new Progress(); t = p2.opWithPayloadAsync(seq, progress: sentSynchronously).ContinueWith( continuation2, TaskContinuationOptions.ExecuteSynchronously); }while (sentSynchronously.getResult()); testController.resumeAdapter(); t.Wait(); } output.WriteLine("ok"); output.Write("testing dispatcher with async/await... "); output.Flush(); { TaskCompletionSource <object> t = new TaskCompletionSource <object>(); p.opAsync().ContinueWith(async previous => // Execute the code below from the Ice client thread pool { try { await p.opAsync(); test(Dispatcher.isDispatcherThread()); try { TestIntfPrx i = p.Clone(adapterId: "dummy"); await i.opAsync(); test(false); } catch (System.Exception) { test(Dispatcher.isDispatcherThread()); } TestIntfPrx to = p.Clone(invocationTimeout: 10); try { await to.sleepAsync(500); test(false); } catch (Ice.InvocationTimeoutException) { test(Dispatcher.isDispatcherThread()); } t.SetResult(null); } catch (System.Exception ex) { t.SetException(ex); } }, p.Scheduler); t.Task.Wait(); } output.WriteLine("ok"); p.shutdown(); }