Exemplo n.º 1
0
        public static void allTestsWithController(TestHelper helper, IControllerPrx controller)
        {
            Communicator?communicator = helper.Communicator();

            TestHelper.Assert(communicator != null);
            var timeout = ITimeoutPrx.Parse($"timeout:{helper.GetTestEndpoint(0)}", communicator);

            System.IO.TextWriter output = helper.GetWriter();
            output.Write("testing connect timeout... ");
            output.Flush();
            {
                //
                // Expect ConnectTimeoutException.
                //
                ITimeoutPrx to = timeout.Clone(connectionTimeout: 100);
                controller.holdAdapter(-1);
                try
                {
                    to.op();
                    TestHelper.Assert(false);
                }
                catch (ConnectTimeoutException)
                {
                    // Expected.
                }
                controller.resumeAdapter();
                timeout.op(); // Ensure adapter is active.
            }
            {
                //
                // Expect success.
                //
                ITimeoutPrx to = timeout.Clone(connectionTimeout: -1);
                controller.holdAdapter(100);
                to.op();
            }
            output.WriteLine("ok");

            // The sequence needs to be large enough to fill the write/recv buffers
            byte[] seq = new byte[2000000];

            // TODO: remove or refactor depending on what we decide for connection timeouts
            // output.Write("testing connection timeout... ");
            // output.Flush();
            // {
            //     //
            //     // Expect TimeoutException.
            //     //
            //     var to = timeout.Clone(connectionTimeout: 250);
            //     connect(to);
            //     controller.holdAdapter(-1);
            //     try
            //     {
            //         to.sendData(seq);
            //         test(false);
            //     }
            //     catch (ConnectionTimeoutException)
            //     {
            //         // Expected.
            //     }
            //     controller.resumeAdapter();
            //     timeout.op(); // Ensure adapter is active.
            // }
            // {
            //     //
            //     // Expect success.
            //     //
            //     var to = timeout.Clone(connectionTimeout: 2000);
            //     controller.holdAdapter(100);
            //     try
            //     {
            //         to.sendData(new byte[1000000]);
            //     }
            //     catch (ConnectionTimeoutException)
            //     {
            //         test(false);
            //     }
            // }
            // output.WriteLine("ok");

            output.Write("testing invocation timeout... ");
            output.Flush();
            {
                timeout.IcePing(); // Makes sure a working connection is associated with the proxy
                var connection = timeout.GetConnection();
                var to         = timeout.Clone(invocationTimeout: 100);
                TestHelper.Assert(connection == to.GetConnection());
                try
                {
                    to.sleep(1000);
                    TestHelper.Assert(false);
                }
                catch (TimeoutException)
                {
                }
                timeout.IcePing();
                to = timeout.Clone(invocationTimeout: 1000);
                TestHelper.Assert(connection == to.GetConnection());
                try
                {
                    to.sleep(100);
                }
                catch (TimeoutException)
                {
                    TestHelper.Assert(false);
                }
                TestHelper.Assert(connection == to.GetConnection());
            }
            {
                //
                // Expect TimeoutException.
                //
                var to = timeout.Clone(invocationTimeout: 100);
                try
                {
                    to.sleepAsync(1000).Wait();
                }
                catch (AggregateException ex) when(ex.InnerException is TimeoutException)
                {
                }
                timeout.IcePing();
            }
            {
                //
                // Expect success.
                //
                var to = timeout.Clone(invocationTimeout: 1000);
                to.sleepAsync(100).Wait();
            }

            output.WriteLine("ok");

            // TODO: remove or refactor depending on what we decide for connection timeouts
            // output.Write("testing close timeout... ");
            // output.Flush();
            // {
            //     var to = timeout.Clone(connectionTimeout: 250);
            //     var connection = connect(to);
            //     controller.holdAdapter(-1);
            //     connection.Close(Ice.ConnectionClose.GracefullyWithWait);
            //     try
            //     {
            //         _ = connection.GetConnectionInfo(); // getInfo() doesn't throw in the closing state.
            //     }
            //     catch (System.Exception)
            //     {
            //         test(false);
            //     }
            //
            //     while (true)
            //     {
            //         try
            //         {
            //             _ = connection.GetConnectionInfo();
            //             Thread.Sleep(10);
            //         }
            //         catch (ConnectionClosedLocallyException ex)
            //         {
            //             // Expected (graceful closure)
            //             break;
            //         }
            //     }
            //     controller.resumeAdapter();
            //     timeout.op(); // Ensure adapter is active.
            // }
            // output.WriteLine("ok");

            output.Write("testing timeout overrides... ");
            output.Flush();
            // TODO: remove or refactor depending on what we decide for connection timeouts
            // {
            //     //
            //     // Test Ice.Override.Timeout. This property overrides all
            //     // endpoint timeouts.
            //     //
            //     var properties = communicator.GetProperties();
            //     properties["Ice.Override.ConnectTimeout"] = "250";
            //     properties["Ice.Override.Timeout"] = "100";
            //     var comm = helper.initialize(properties);
            //     var to = ITimeoutPrx.Parse(sref, comm);
            //     connect(to);
            //     controller.holdAdapter(-1);
            //     try
            //     {
            //         to.sendData(seq);
            //         test(false);
            //     }
            //     catch (ConnectionTimeoutException)
            //     {
            //         // Expected.
            //     }
            //     controller.resumeAdapter();
            //     timeout.op(); // Ensure adapter is active.

            //     //
            //     // Calling ice_timeout() should have no effect.
            //     //
            //     to = to.Clone(connectionTimeout: 1000);
            //     connect(to);
            //     controller.holdAdapter(-1);
            //     try
            //     {
            //         to.sendData(seq);
            //         test(false);
            //     }
            //     catch (ConnectionTimeoutException)
            //     {
            //         // Expected.
            //     }
            //     controller.resumeAdapter();
            //     timeout.op(); // Ensure adapter is active.
            //     comm.Destroy();
            // }

            {
                //
                // Test Ice.Override.ConnectTimeout.
                //
                var properties = communicator.GetProperties();
                properties["Ice.Override.ConnectTimeout"] = "250";
                var comm = helper.Initialize(properties);
                controller.holdAdapter(-1);
                var to = ITimeoutPrx.Parse($"timeout:{helper.GetTestEndpoint(0)}", comm);
                try
                {
                    to.op();
                    TestHelper.Assert(false);
                }
                catch (ConnectTimeoutException)
                {
                    // Expected.
                }
                controller.resumeAdapter();
                timeout.op(); // Ensure adapter is active.

                //
                // Calling ice_timeout() should have no effect on the connect timeout.
                //
                controller.holdAdapter(-1);
                to = to.Clone(connectionTimeout: 1000);
                try
                {
                    to.op();
                    TestHelper.Assert(false);
                }
                catch (ConnectTimeoutException)
                {
                    // Expected.
                }
                controller.resumeAdapter();
                timeout.op(); // Ensure adapter is active.

                //
                // Verify that timeout set via ice_timeout() is still used for requests.
                //
                // to = to.Clone(connectionTimeout: 250);
                // connect(to);
                // controller.holdAdapter(-1);
                // try
                // {
                //     to.sendData(seq);
                //     test(false);
                // }
                // catch (ConnectionTimeoutException)
                // {
                //     // Expected.
                // }
                // controller.resumeAdapter();
                // timeout.op(); // Ensure adapter is active.
                comm.Destroy();
            }
            // TODO: remove or refactor depending on what we decide for connection timeouts
            // {
            //     //
            //     // Test Ice.Override.CloseTimeout.
            //     //
            //     var properties = communicator.GetProperties();
            //     properties["Ice.Override.CloseTimeout"] = "100";
            //     var comm = helper.initialize(properties);
            //     IObjectPrx.Parse(sref, comm).GetConnection();
            //     controller.holdAdapter(-1);
            //     long begin = System.DateTime.Now.Ticks;
            //     comm.Destroy();
            //     test(((long)new System.TimeSpan(System.DateTime.Now.Ticks - begin).TotalMilliseconds - begin) < 1000);
            //     controller.resumeAdapter();
            // }
            output.WriteLine("ok");

            output.Write("testing invocation timeouts with collocated calls... ");
            output.Flush();
            {
                communicator.SetProperty("TimeoutCollocated.AdapterId", "timeoutAdapter");

                var adapter = communicator.CreateObjectAdapter("TimeoutCollocated");
                adapter.Activate();

                var proxy = adapter.AddWithUUID(new Timeout(), ITimeoutPrx.Factory).Clone(invocationTimeout: 100);
                try
                {
                    proxy.sleep(500);
                    TestHelper.Assert(false);
                }
                catch (TimeoutException)
                {
                }

                try
                {
                    proxy.sleepAsync(500).Wait();
                    TestHelper.Assert(false);
                }
                catch (AggregateException ex) when(ex.InnerException is TimeoutException)
                {
                }
                adapter.Destroy();
            }
            output.WriteLine("ok");

            controller.shutdown();
        }
Exemplo n.º 2
0
        public static void RunWithController(TestHelper helper, IControllerPrx controller)
        {
            Communicator?communicator = helper.Communicator;

            TestHelper.Assert(communicator != null);

            var timeout = ITimeoutPrx.Parse(helper.GetTestProxy("timeout", 0), communicator);

            System.IO.TextWriter output = helper.Output;
            output.Write("testing connect timeout... ");
            output.Flush();
            {
                Dictionary <string, string>?properties = communicator.GetProperties();
                properties["Ice.ConnectTimeout"] = "100ms";
                using var comm = new Communicator(properties);

                var to = ITimeoutPrx.Parse(helper.GetTestProxy("timeout", 0), comm);

                // Expect ConnectTimeoutException.
                controller.HoldAdapter(-1);
                try
                {
                    to.Op();
                    TestHelper.Assert(false);
                }
                catch (ConnectTimeoutException)
                {
                    // Expected.
                }
                controller.ResumeAdapter();
                timeout.Op(); // Ensure adapter is active.
            }
            {
                // Expect success.
                controller.HoldAdapter(100);
                timeout.Op();
            }
            output.WriteLine("ok");

            // The sequence needs to be large enough to fill the write/recv buffers
            byte[] seq = new byte[2000000];

            output.Write("testing connection timeout... ");
            output.Flush();
            {
                // Expect TimeoutException.
                controller.HoldAdapter(-1);
                timeout.GetConnection() !.Acm = new Acm(TimeSpan.FromMilliseconds(50),
                                                        AcmClose.OnInvocationAndIdle,
                                                        AcmHeartbeat.Off);
                try
                {
                    timeout.SendData(seq);
                    TestHelper.Assert(false);
                }
                catch (ConnectionTimeoutException)
                {
                    // Expected.
                }
                controller.ResumeAdapter();
                timeout.Op(); // Ensure adapter is active.
            }
            {
                // Expect success.
                controller.HoldAdapter(100);
                try
                {
                    timeout.SendData(new byte[1000000]);
                }
                catch (ConnectionTimeoutException)
                {
                    TestHelper.Assert(false);
                }
            }
            output.WriteLine("ok");

            output.Write("testing invocation timeout... ");
            output.Flush();
            {
                timeout.IcePing(); // Makes sure a working connection is associated with the proxy
                Connection?connection = timeout.GetConnection();
                try
                {
                    using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
                    timeout.SleepAsync(1000, cancel: timeoutTokenSource.Token).Wait();
                    TestHelper.Assert(false);
                }
                catch (AggregateException ex) when(ex.InnerException is OperationCanceledException)
                {
                }
                timeout.IcePing();

                TestHelper.Assert(connection == timeout.GetConnection());
                try
                {
                    using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(1000));
                    timeout.SleepAsync(100, cancel: timeoutTokenSource.Token).Wait();
                }
                catch (AggregateException ex) when(ex.InnerException is OperationCanceledException)
                {
                    TestHelper.Assert(false);
                }
                TestHelper.Assert(connection == timeout.GetConnection());
            }
            output.WriteLine("ok");

            output.Write("testing close timeout... ");
            output.Flush();
            {
                Dictionary <string, string> properties = communicator.GetProperties();
                properties["Ice.CloseTimeout"] = "100ms";
                using var comm = new Communicator(properties);

                var to = ITimeoutPrx.Parse(helper.GetTestProxy("timeout", 0), comm);

                Connection?connection  = to.GetConnection();
                Connection?connection2 = timeout.GetConnection();  // No close timeout

                TestHelper.Assert(connection != null && connection2 != null);

                controller.HoldAdapter(-1);

                // Make sure there's no ReadAsync pending
                _ = to.IcePingAsync();
                _ = timeout.IcePingAsync();

                var semaphore = new System.Threading.SemaphoreSlim(0);
                connection.Closed += (sender, args) => semaphore.Release();
                connection.Close(ConnectionClose.Gracefully);
                TestHelper.Assert(semaphore.Wait(500));

                connection2.Closed += (sender, args) => semaphore.Release();
                connection2.Close(ConnectionClose.Gracefully);
                TestHelper.Assert(!semaphore.Wait(500));

                controller.ResumeAdapter();
                timeout.Op(); // Ensure adapter is active.
            }
            output.WriteLine("ok");

            output.Write("testing invocation timeouts with collocated calls... ");
            output.Flush();
            {
                communicator.SetProperty("TimeoutCollocated.AdapterId", "timeoutAdapter");

                ObjectAdapter adapter = communicator.CreateObjectAdapter("TimeoutCollocated");
                adapter.Activate();

                ITimeoutPrx proxy = adapter.AddWithUUID(new Timeout(), ITimeoutPrx.Factory);
                try
                {
                    using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
                    proxy.SleepAsync(500, cancel: timeoutTokenSource.Token).Wait();
                    TestHelper.Assert(false);
                }
                catch (AggregateException ex) when(ex.InnerException is OperationCanceledException)
                {
                }

                adapter.Dispose();
            }
            output.WriteLine("ok");

            controller.Shutdown();
        }
Exemplo n.º 3
0
        public static async Task RunWithControllerAsync(TestHelper helper, IControllerPrx controller)
        {
            Communicator communicator = helper.Communicator;

            bool ice1 = TestHelper.GetTestProtocol(communicator.GetProperties()) == Protocol.Ice1;

            var timeout = ITimeoutPrx.Parse(helper.GetTestProxy("timeout", 0), communicator);

            System.IO.TextWriter output = helper.Output;
            output.Write("testing connect timeout... ");
            output.Flush();
            {
                Dictionary <string, string> properties = communicator.GetProperties();
                properties["Ice.ConnectTimeout"] = "100ms";
                await using var comm             = new Communicator(properties);

                var to = ITimeoutPrx.Parse(helper.GetTestProxy("timeout", 0), comm);

                // Expect ConnectTimeoutException.
                controller.HoldAdapter(-1);
                try
                {
                    to.Op();
                    TestHelper.Assert(false);
                }
                catch (ConnectTimeoutException)
                {
                    // Expected.
                }
                controller.ResumeAdapter();
                timeout.Op(); // Ensure adapter is active.
            }
            {
                // Expect success.
                controller.HoldAdapter(100);
                timeout.Op();
            }
            output.WriteLine("ok");

            output.Write("testing invocation timeout... ");
            output.Flush();
            {
                timeout.IcePing(); // Makes sure a working connection is associated with the proxy
                Connection connection = await timeout.GetConnectionAsync();

                try
                {
                    timeout.Clone(invocationTimeout: TimeSpan.FromMilliseconds(100)).SleepAsync(1000).Wait();
                    TestHelper.Assert(false);
                }
                catch (AggregateException ex) when(ex.InnerException is OperationCanceledException)
                {
                }
                timeout.IcePing();

                TestHelper.Assert(connection == await timeout.GetConnectionAsync());
                try
                {
                    timeout.Clone(invocationTimeout: TimeSpan.FromMilliseconds(1000)).SleepAsync(100).Wait();
                }
                catch (AggregateException ex) when(ex.InnerException is OperationCanceledException)
                {
                    TestHelper.Assert(false);
                }
                TestHelper.Assert(connection == await timeout.GetConnectionAsync());

                try
                {
                    timeout.Clone(invocationTimeout: TimeSpan.FromMilliseconds(100)).Sleep(1000);
                    TestHelper.Assert(false);
                }
                catch (OperationCanceledException)
                {
                }
            }
            output.WriteLine("ok");

            output.Write("testing close timeout... ");
            output.Flush();
            {
                Dictionary <string, string> properties = communicator.GetProperties();
                properties["Ice.CloseTimeout"] = "100ms";
                await using var comm           = new Communicator(properties);

                var to = ITimeoutPrx.Parse(helper.GetTestProxy("timeout", 0), comm);

                Connection connection = await to.GetConnectionAsync();

                Connection connection2 = await timeout.GetConnectionAsync(); // No close timeout

                TestHelper.Assert(connection != null && connection2 != null);

                controller.HoldAdapter(-1);

                // Make sure there's no ReadAsync pending
                _ = to.IcePingAsync();
                _ = timeout.IcePingAsync();

                var semaphore = new System.Threading.SemaphoreSlim(0);
                connection.Closed += (sender, args) => semaphore.Release();
                _ = connection.GoAwayAsync();
                TestHelper.Assert(semaphore.Wait(500));

                connection2.Closed += (sender, args) => semaphore.Release();
                _ = connection2.GoAwayAsync();
                TestHelper.Assert(!semaphore.Wait(500));

                controller.ResumeAdapter();
                timeout.Op(); // Ensure adapter is active.
            }
            output.WriteLine("ok");

            if (!ice1)
            {
                output.Write("testing deadlines... ");
                output.Flush();
                {
                    var comm1 = new Communicator(communicator.GetProperties());

                    comm1.DefaultInvocationInterceptors = ImmutableList.Create <InvocationInterceptor>(
                        (target, request, next, cancel) =>
                    {
                        request.BinaryContextOverride.Add(10, ostr =>
                        {
                            var deadline = (request.Deadline - DateTime.UnixEpoch).TotalMilliseconds;
                            ostr.WriteVarLong((long)deadline);
                        });
                        return(next(target, request, cancel));
                    });

                    for (int i = 1000; i < 5000;)
                    {
                        i += 33;
                        ITimeoutPrx to = ITimeoutPrx.Parse(helper.GetTestProxy("timeout", 0), comm1).Clone(
                            invocationTimeout: TimeSpan.FromMilliseconds(i));
                        TestHelper.Assert(to.CheckDeadline());
                    }
                }
                output.WriteLine("ok");
            }
            controller.Shutdown();
        }
Exemplo n.º 4
0
Arquivo: AllTests.cs Projeto: yzun/ice
        public static void allTestsWithController(global::Test.TestHelper helper, Test.IControllerPrx controller)
        {
            var    communicator = helper.communicator();
            string sref         = "timeout:" + helper.getTestEndpoint(0);
            var    timeout      = ITimeoutPrx.Parse(sref, communicator);
            var    output       = helper.getWriter();

            output.Write("testing connect timeout... ");
            output.Flush();
            {
                //
                // Expect ConnectTimeoutException.
                //
                var to = timeout.Clone(connectionTimeout: 100);
                controller.holdAdapter(-1);
                try
                {
                    to.op();
                    test(false);
                }
                catch (ConnectTimeoutException)
                {
                    // Expected.
                }
                controller.resumeAdapter();
                timeout.op(); // Ensure adapter is active.
            }
            {
                //
                // Expect success.
                //
                var to = timeout.Clone(connectionTimeout: -1);
                controller.holdAdapter(100);
                try
                {
                    to.op();
                }
                catch (Ice.ConnectTimeoutException)
                {
                    test(false);
                }
            }
            output.WriteLine("ok");

            // The sequence needs to be large enough to fill the write/recv buffers
            byte[] seq = new byte[2000000];

            output.Write("testing connection timeout... ");
            output.Flush();
            {
                //
                // Expect TimeoutException.
                //
                var to = timeout.Clone(connectionTimeout: 250);
                connect(to);
                controller.holdAdapter(-1);
                try
                {
                    to.sendData(seq);
                    test(false);
                }
                catch (TimeoutException)
                {
                    // Expected.
                }
                controller.resumeAdapter();
                timeout.op(); // Ensure adapter is active.
            }
            {
                //
                // Expect success.
                //
                var to = timeout.Clone(connectionTimeout: 2000);
                controller.holdAdapter(100);
                try
                {
                    to.sendData(new byte[1000000]);
                }
                catch (TimeoutException)
                {
                    test(false);
                }
            }
            output.WriteLine("ok");

            output.Write("testing invocation timeout... ");
            output.Flush();
            {
                var connection = timeout.GetConnection();
                var to         = timeout.Clone(invocationTimeout: 100);
                test(connection == to.GetConnection());
                try
                {
                    to.sleep(1000);
                    test(false);
                }
                catch (InvocationTimeoutException)
                {
                }
                timeout.IcePing();
                to = timeout.Clone(invocationTimeout: 1000);
                test(connection == to.GetConnection());
                try
                {
                    to.sleep(100);
                }
                catch (InvocationTimeoutException)
                {
                    test(false);
                }
                test(connection == to.GetConnection());
            }
            {
                //
                // Expect InvocationTimeoutException.
                //
                var to = timeout.Clone(invocationTimeout: 100);
                try
                {
                    to.sleepAsync(1000).Wait();
                }
                catch (System.AggregateException ex) when(ex.InnerException is InvocationTimeoutException)
                {
                }
                timeout.IcePing();
            }
            {
                //
                // Expect success.
                //
                var to = timeout.Clone(invocationTimeout: 1000);
                to.sleepAsync(100).Wait();
            }
            {
                //
                // Backward compatible connection timeouts
                //
                var to  = timeout.Clone(invocationTimeout: -2, connectionTimeout: 250);
                var con = connect(to);
                try
                {
                    to.sleep(750);
                    test(false);
                }
                catch (TimeoutException)
                {
                    try
                    {
                        _ = con.GetConnectionInfo();
                        test(false);
                    }
                    catch (TimeoutException)
                    {
                        // Connection got closed as well.
                    }
                }
                timeout.IcePing();

                try
                {
                    con = connect(to);
                    to.sleepAsync(750).Wait();
                    test(false);
                }
                catch (System.AggregateException ex) when(ex.InnerException is TimeoutException)
                {
                    try
                    {
                        _ = con.GetConnectionInfo();
                        test(false);
                    }
                    catch (TimeoutException)
                    {
                        // Connection got closed as well.
                    }
                }
                timeout.IcePing();
            }
            output.WriteLine("ok");

            output.Write("testing close timeout... ");
            output.Flush();
            {
                var to         = timeout.Clone(connectionTimeout: 250);
                var connection = connect(to);
                controller.holdAdapter(-1);
                connection.Close(Ice.ConnectionClose.GracefullyWithWait);
                try
                {
                    _ = connection.GetConnectionInfo(); // getInfo() doesn't throw in the closing state.
                }
                catch (LocalException)
                {
                    test(false);
                }

                while (true)
                {
                    try
                    {
                        _ = connection.GetConnectionInfo();
                        Thread.Sleep(10);
                    }
                    catch (ConnectionManuallyClosedException ex)
                    {
                        // Expected.
                        test(ex.Graceful);
                        break;
                    }
                }
                controller.resumeAdapter();
                timeout.op(); // Ensure adapter is active.
            }
            output.WriteLine("ok");

            output.Write("testing timeout overrides... ");
            output.Flush();
            {
                //
                // Test Ice.Override.Timeout. This property overrides all
                // endpoint timeouts.
                //
                var properties = communicator.GetProperties();
                properties["Ice.Override.ConnectTimeout"] = "250";
                properties["Ice.Override.Timeout"]        = "100";
                var comm = helper.initialize(properties);
                var to   = ITimeoutPrx.Parse(sref, comm);
                connect(to);
                controller.holdAdapter(-1);
                try
                {
                    to.sendData(seq);
                    test(false);
                }
                catch (TimeoutException)
                {
                    // Expected.
                }
                controller.resumeAdapter();
                timeout.op(); // Ensure adapter is active.

                //
                // Calling ice_timeout() should have no effect.
                //
                to = to.Clone(connectionTimeout: 1000);
                connect(to);
                controller.holdAdapter(-1);
                try
                {
                    to.sendData(seq);
                    test(false);
                }
                catch (TimeoutException)
                {
                    // Expected.
                }
                controller.resumeAdapter();
                timeout.op(); // Ensure adapter is active.
                comm.Destroy();
            }
            {
                //
                // Test Ice.Override.ConnectTimeout.
                //
                var properties = communicator.GetProperties();
                properties["Ice.Override.ConnectTimeout"] = "250";
                var comm = helper.initialize(properties);
                controller.holdAdapter(-1);
                var to = ITimeoutPrx.Parse(sref, comm);
                try
                {
                    to.op();
                    test(false);
                }
                catch (ConnectTimeoutException)
                {
                    // Expected.
                }
                controller.resumeAdapter();
                timeout.op(); // Ensure adapter is active.

                //
                // Calling ice_timeout() should have no effect on the connect timeout.
                //
                controller.holdAdapter(-1);
                to = to.Clone(connectionTimeout: 1000);
                try
                {
                    to.op();
                    test(false);
                }
                catch (ConnectTimeoutException)
                {
                    // Expected.
                }
                controller.resumeAdapter();
                timeout.op(); // Ensure adapter is active.

                //
                // Verify that timeout set via ice_timeout() is still used for requests.
                //
                to = to.Clone(connectionTimeout: 250);
                connect(to);
                controller.holdAdapter(-1);
                try
                {
                    to.sendData(seq);
                    test(false);
                }
                catch (TimeoutException)
                {
                    // Expected.
                }
                controller.resumeAdapter();
                timeout.op(); // Ensure adapter is active.
                comm.Destroy();
            }
            {
                //
                // Test Ice.Override.CloseTimeout.
                //
                var properties = communicator.GetProperties();
                properties["Ice.Override.CloseTimeout"] = "100";
                var comm = helper.initialize(properties);
                IObjectPrx.Parse(sref, comm).GetConnection();
                controller.holdAdapter(-1);
                long begin = System.DateTime.Now.Ticks;
                comm.Destroy();
                test(((long)new System.TimeSpan(System.DateTime.Now.Ticks - begin).TotalMilliseconds - begin) < 1000);
                controller.resumeAdapter();
            }
            output.WriteLine("ok");

            output.Write("testing invocation timeouts with collocated calls... ");
            output.Flush();
            {
                communicator.SetProperty("TimeoutCollocated.AdapterId", "timeoutAdapter");

                var adapter = communicator.CreateObjectAdapter("TimeoutCollocated");
                adapter.Activate();

                var proxy = adapter.AddWithUUID(new Timeout(), ITimeoutPrx.Factory).Clone(invocationTimeout: 100);
                try
                {
                    proxy.sleep(500);
                    test(false);
                }
                catch (InvocationTimeoutException)
                {
                }

                try
                {
                    proxy.sleepAsync(500).Wait();
                    test(false);
                }
                catch (System.AggregateException ex) when(ex.InnerException is InvocationTimeoutException)
                {
                }

                try
                {
                    proxy.Clone(invocationTimeout: -2).IcePing();
                    proxy.Clone(invocationTimeout: -2).IcePingAsync().Wait();
                }
                catch (System.Exception)
                {
                    test(false);
                }

                adapter.Destroy();
            }
            output.WriteLine("ok");

            controller.shutdown();
        }
Exemplo n.º 5
0
        public static void RunWithController(TestHelper helper, IControllerPrx controller)
        {
            Communicator?communicator = helper.Communicator;

            TestHelper.Assert(communicator != null);
            bool ice1 = TestHelper.GetTestProtocol(communicator.GetProperties()) == Protocol.Ice1;

            var timeout = ITimeoutPrx.Parse(helper.GetTestProxy("timeout", 0), communicator);

            System.IO.TextWriter output = helper.Output;
            output.Write("testing connect timeout... ");
            output.Flush();
            {
                Dictionary <string, string>?properties = communicator.GetProperties();
                properties["Ice.ConnectTimeout"] = "100ms";
                using var comm = new Communicator(properties);

                var to = ITimeoutPrx.Parse(helper.GetTestProxy("timeout", 0), comm);

                // Expect ConnectTimeoutException.
                controller.HoldAdapter(-1);
                try
                {
                    to.Op();
                    TestHelper.Assert(false);
                }
                catch (ConnectTimeoutException)
                {
                    // Expected.
                }
                controller.ResumeAdapter();
                timeout.Op(); // Ensure adapter is active.
            }
            {
                // Expect success.
                controller.HoldAdapter(100);
                timeout.Op();
            }
            output.WriteLine("ok");

            // The sequence needs to be large enough to fill the write/recv buffers
            byte[] seq = new byte[2000000];

            output.Write("testing connection timeout... ");
            output.Flush();
            {
                // Expect TimeoutException.
                controller.HoldAdapter(-1);
                timeout.GetConnection().Acm = new Acm(TimeSpan.FromMilliseconds(50),
                                                      AcmClose.OnInvocationAndIdle,
                                                      AcmHeartbeat.Off);
                try
                {
                    timeout.SendData(seq);
                    TestHelper.Assert(false);
                }
                catch (ConnectionClosedException)
                {
                    // Expected.
                }
                controller.ResumeAdapter();
                timeout.Op(); // Ensure adapter is active.
            }
            {
                // Expect success.
                controller.HoldAdapter(100);
                try
                {
                    timeout.SendData(new byte[1000000]);
                }
                catch (ConnectionClosedException)
                {
                    TestHelper.Assert(false);
                }
            }
            output.WriteLine("ok");

            output.Write("testing invocation timeout... ");
            output.Flush();
            {
                timeout.IcePing(); // Makes sure a working connection is associated with the proxy
                Connection?connection = timeout.GetConnection();
                try
                {
                    timeout.Clone(invocationTimeout: TimeSpan.FromMilliseconds(100)).SleepAsync(1000).Wait();
                    TestHelper.Assert(false);
                }
                catch (AggregateException ex) when(ex.InnerException is OperationCanceledException)
                {
                }
                timeout.IcePing();

                TestHelper.Assert(connection == timeout.GetConnection());
                try
                {
                    timeout.Clone(invocationTimeout: TimeSpan.FromMilliseconds(1000)).SleepAsync(100).Wait();
                }
                catch (AggregateException ex) when(ex.InnerException is OperationCanceledException)
                {
                    TestHelper.Assert(false);
                }
                TestHelper.Assert(connection == timeout.GetConnection());
            }
            output.WriteLine("ok");

            output.Write("testing close timeout... ");
            output.Flush();
            {
                Dictionary <string, string> properties = communicator.GetProperties();
                properties["Ice.CloseTimeout"] = "100ms";
                using var comm = new Communicator(properties);

                var to = ITimeoutPrx.Parse(helper.GetTestProxy("timeout", 0), comm);

                Connection?connection  = to.GetConnection();
                Connection?connection2 = timeout.GetConnection();  // No close timeout

                TestHelper.Assert(connection != null && connection2 != null);

                controller.HoldAdapter(-1);

                // Make sure there's no ReadAsync pending
                _ = to.IcePingAsync();
                _ = timeout.IcePingAsync();

                var semaphore = new System.Threading.SemaphoreSlim(0);
                connection.Closed += (sender, args) => semaphore.Release();
                connection.GoAwayAsync();
                TestHelper.Assert(semaphore.Wait(500));

                connection2.Closed += (sender, args) => semaphore.Release();
                connection2.GoAwayAsync();
                TestHelper.Assert(!semaphore.Wait(500));

                controller.ResumeAdapter();
                timeout.Op(); // Ensure adapter is active.
            }
            output.WriteLine("ok");

            output.Write("testing invocation timeouts with collocated calls... ");
            output.Flush();
            {
                communicator.SetProperty("TimeoutCollocated.AdapterId", "timeoutAdapter");

                ObjectAdapter adapter = communicator.CreateObjectAdapter("TimeoutCollocated");
                adapter.Activate();

                ITimeoutPrx proxy = adapter.AddWithUUID(new Timeout(), ITimeoutPrx.Factory);
                try
                {
                    proxy.Clone(invocationTimeout: TimeSpan.FromMilliseconds(100)).SleepAsync(500).Wait();
                    TestHelper.Assert(false);
                }
                catch (AggregateException ex) when(ex.InnerException is OperationCanceledException)
                {
                }

                adapter.Dispose();
            }
            output.WriteLine("ok");

            if (!ice1)
            {
                output.Write("testing deadlines... ");
                output.Flush();
                {
                    var comm1 = new Communicator(
                        communicator.GetProperties(),
                        invocationInterceptors: new InvocationInterceptor[]
                    {
                        (target, request, next, cancel) =>
                        {
                            request.AddBinaryContextEntry(10, request.Deadline, (ostr, value) =>
                            {
                                var deadline = (value - DateTime.UnixEpoch).TotalMilliseconds;
                                ostr.WriteVarLong((long)deadline);
                            });
                            return(next(target, request, cancel));
                        }
                    });

                    for (int i = 1000; i < 5000;)
                    {
                        i += 33;
                        ITimeoutPrx to = ITimeoutPrx.Parse(helper.GetTestProxy("timeout", 0), comm1).Clone(
                            invocationTimeout: TimeSpan.FromMilliseconds(i));
                        TestHelper.Assert(to.CheckDeadline());
                    }
                }
                output.WriteLine("ok");
            }
            controller.Shutdown();
        }