Ejemplo n.º 1
0
    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();
    }