예제 #1
0
파일: AllTests.cs 프로젝트: wandec/ice
                base("invocation heartbeat", com, helper) => SetServerACM(1, -1, -1); // Faster ACM to make sure we receive enough ACM heartbeats

            public override void RunTestCase(IRemoteObjectAdapterPrx adapter, ITestIntfPrx proxy)
            {
                proxy.sleep(4);

                lock (this)
                {
                    TestHelper.Assert(Heartbeat >= 4);
                }
            }
예제 #2
0
파일: AllTests.cs 프로젝트: cyyever/ice
            public override void RunTestCase(IRemoteObjectAdapterPrx adapter, ITestIntfPrx proxy)
            {
                proxy.Sleep(4);

                lock (Mutex)
                {
                    TestHelper.Assert(Heartbeat >= 2);
                    TestHelper.Assert(!Closed);
                }
            }
예제 #3
0
파일: AllTests.cs 프로젝트: mreinart/ice
            public override Task RunTestCaseAsync(IRemoteObjectAdapterPrx adapter, ITestIntfPrx proxy)
            {
                proxy.Sleep(4);

                lock (Mutex)
                {
                    TestHelper.Assert(Heartbeat >= 2);
                    TestHelper.Assert(!Closed);
                }
                return(Task.CompletedTask);
            }
예제 #4
0
            public override void RunTestCase(IRemoteObjectAdapterPrx adapter, ITestIntfPrx proxy)
            {
                proxy.StartHeartbeatCount();
                Connection con = proxy.GetConnection() !;

                con.Ping();
                con.Ping();
                con.Ping();
                con.Ping();
                con.Ping();
                proxy.WaitForHeartbeatCount(5);
            }
예제 #5
0
 private static void Shuffle(ref List <IRemoteObjectAdapterPrx> array)
 {
     for (int i = 0; i < array.Count - 1; ++i)
     {
         int r = _rand.Next(array.Count - i) + i;
         TestHelper.Assert(r >= i && r < array.Count);
         if (r != i)
         {
             IRemoteObjectAdapterPrx tmp = array[i];
             array[i] = array[r];
             array[r] = tmp;
         }
     }
 }
예제 #6
0
파일: AllTests.cs 프로젝트: mreinart/ice
            public override async Task RunTestCaseAsync(IRemoteObjectAdapterPrx adapter, ITestIntfPrx proxy)
            {
                proxy.StartHeartbeatCount();
                Connection con = await proxy.GetConnectionAsync();

                await con.PingAsync();

                await con.PingAsync();

                await con.PingAsync();

                await con.PingAsync();

                await con.PingAsync();

                proxy.WaitForHeartbeatCount(5);
            }
예제 #7
0
파일: AllTests.cs 프로젝트: mreinart/ice
            public override async Task RunTestCaseAsync(IRemoteObjectAdapterPrx adapter, ITestIntfPrx proxy)
            {
                Connection?con = proxy.GetCachedConnection();

                TestHelper.Assert(con != null);

                TestHelper.Assert(con.IdleTimeout == TimeSpan.FromSeconds(1));
                TestHelper.Assert(con.KeepAlive == false);

                con.KeepAlive = true;
                TestHelper.Assert(con.KeepAlive == true);

                try
                {
                    con.IdleTimeout = TimeSpan.FromSeconds(1);
                }
                catch
                {
                }

                proxy.StartHeartbeatCount();
                proxy.WaitForHeartbeatCount(2);

                var t1 = new TaskCompletionSource <object?>();
                var t2 = new TaskCompletionSource <object?>();

                con.Closed += (sender, args) => t1.SetResult(null);
                con.Closed += (sender, args) => t2.SetResult(null);

                await con.GoAwayAsync();

                TestHelper.Assert(t1.Task.Result == null);
                TestHelper.Assert(t2.Task.Result == null);

                TestHelper.Assert(!con.IsActive);

                var t3 = new TaskCompletionSource <object?>();

                con.Closed += (sender, args) => t3.SetResult(null);
                TestHelper.Assert(t3.Task.Result == null);

                con.PingReceived += (sender, args) => TestHelper.Assert(false);

                foreach ((int idleTimeout, string keepAlive) in new (int, string)[]
예제 #8
0
파일: AllTests.cs 프로젝트: keno1213/ice
 public abstract void RunTestCase(IRemoteObjectAdapterPrx adapter, ITestIntfPrx proxy);
예제 #9
0
        public static void Run(TestHelper helper)
        {
            Communicator?communicator = helper.Communicator();

            TestHelper.Assert(communicator != null);
            var com = IRemoteCommunicatorPrx.Parse(helper.GetTestProxy("communicator", 0), communicator);

            var rand = new Random(unchecked ((int)DateTime.Now.Ticks));

            System.IO.TextWriter output = helper.GetWriter();

            output.Write("testing binding with single endpoint... ");
            output.Flush();
            {
                IRemoteObjectAdapterPrx?adapter = com.CreateObjectAdapter("Adapter", "default");
                TestHelper.Assert(adapter != null);
                ITestIntfPrx?test1 = adapter.GetTestIntf();
                ITestIntfPrx?test2 = adapter.GetTestIntf();
                TestHelper.Assert(test1 != null && test2 != null);
                TestHelper.Assert(test1.GetConnection() == test2.GetConnection());

                test1.IcePing();
                test2.IcePing();

                com.DeactivateObjectAdapter(adapter);

                var test3 = ITestIntfPrx.UncheckedCast(test1);
                TestHelper.Assert(test3.GetConnection() == test1.GetConnection());
                TestHelper.Assert(test3.GetConnection() == test2.GetConnection());

                try
                {
                    test3.IcePing();
                    TestHelper.Assert(false);
                }
                catch (ConnectFailedException)
                {
                }
            }
            output.WriteLine("ok");

            output.Write("testing binding with multiple endpoints... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("Adapter11", "default") !,
                    com.CreateObjectAdapter("Adapter12", "default") !,
                    com.CreateObjectAdapter("Adapter13", "default") !
                };

                // Ensure that when a connection is opened it's reused for new proxies and that all endpoints are
                // eventually tried.
                var names = new List <string>
                {
                    "Adapter11",
                    "Adapter12",
                    "Adapter13"
                };

                while (names.Count > 0)
                {
                    var adpts = new List <IRemoteObjectAdapterPrx>(adapters);

                    ITestIntfPrx test1 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test2 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test3 = CreateTestIntfPrx(adpts);
                    test1.IcePing();
                    TestHelper.Assert(test1.GetConnection() == test2.GetConnection());
                    TestHelper.Assert(test2.GetConnection() == test3.GetConnection());

                    names.Remove(test1.GetAdapterName());
                    test1.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                // Ensure that the proxy correctly caches the connection(we always send the request over the same
                // connection.)
                {
                    foreach (IRemoteObjectAdapterPrx adpt in adapters)
                    {
                        adpt.GetTestIntf() !.IcePing();
                    }

                    ITestIntfPrx t    = CreateTestIntfPrx(adapters);
                    string       name = t.GetAdapterName();

                    for (int i = 0; i < 10 && t.GetAdapterName().Equals(name); i++)
                    {
                        TestHelper.Assert(t.GetAdapterName() == name);
                    }

                    foreach (IRemoteObjectAdapterPrx adpt in adapters)
                    {
                        adpt.GetTestIntf() !.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                    }
                }

                // Deactivate an adapter and ensure that we can still establish the connection to the remaining
                // adapters.
                com.DeactivateObjectAdapter(adapters[0]);
                names.Add("Adapter12");
                names.Add("Adapter13");
                while (names.Count > 0)
                {
                    var adpts = new List <IRemoteObjectAdapterPrx>(adapters);

                    ITestIntfPrx test1 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test2 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test3 = CreateTestIntfPrx(adpts);

                    TestHelper.Assert(test1.GetConnection() == test2.GetConnection());
                    TestHelper.Assert(test2.GetConnection() == test3.GetConnection());

                    names.Remove(test1.GetAdapterName());
                    test1.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                // Deactivate an adapter and ensure that we can still establish the connection to the remaining
                // adapter.
                com.DeactivateObjectAdapter(adapters[2]);
                ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                TestHelper.Assert(obj.GetAdapterName().Equals("Adapter12"));

                Deactivate(com, adapters);
            }
            output.WriteLine("ok");

            output.Write("testing binding with multiple random endpoints... ");
            output.Flush();
            {
                var adapters = new IRemoteObjectAdapterPrx[5]
                {
                    com.CreateObjectAdapter("AdapterRandom11", "default") !,
                    com.CreateObjectAdapter("AdapterRandom12", "default") !,
                    com.CreateObjectAdapter("AdapterRandom13", "default") !,
                    com.CreateObjectAdapter("AdapterRandom14", "default") !,
                    com.CreateObjectAdapter("AdapterRandom15", "default") !
                };

                int count        = 20;
                int adapterCount = adapters.Length;
                while (--count > 0)
                {
                    ITestIntfPrx[] proxies;
                    if (count == 1)
                    {
                        com.DeactivateObjectAdapter(adapters[4]);
                        --adapterCount;
                    }
                    proxies = new ITestIntfPrx[10];

                    int i;
                    for (i = 0; i < proxies.Length; ++i)
                    {
                        var adpts = new IRemoteObjectAdapterPrx[rand.Next(adapters.Length)];
                        if (adpts.Length == 0)
                        {
                            adpts = new IRemoteObjectAdapterPrx[1];
                        }
                        for (int j = 0; j < adpts.Length; ++j)
                        {
                            adpts[j] = adapters[rand.Next(adapters.Length)];
                        }
                        proxies[i] = CreateTestIntfPrx(new List <IRemoteObjectAdapterPrx>(adpts));
                    }

                    for (i = 0; i < proxies.Length; i++)
                    {
                        proxies[i].GetAdapterNameAsync();
                    }
                    for (i = 0; i < proxies.Length; i++)
                    {
                        try
                        {
                            proxies[i].IcePing();
                        }
                        catch
                        {
                        }
                    }

                    var connections = new List <Connection>();
                    for (i = 0; i < proxies.Length; i++)
                    {
                        if (proxies[i].GetCachedConnection() is Connection connection)
                        {
                            if (!connections.Contains(connection))
                            {
                                connections.Add(connection);
                            }
                        }
                    }
                    TestHelper.Assert(connections.Count <= adapterCount);

                    foreach (IRemoteObjectAdapterPrx a in adapters)
                    {
                        try
                        {
                            a.GetTestIntf() !.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                        }
                        catch
                        {
                            // Expected if adapter is down.
                        }
                    }
                }
            }
            output.WriteLine("ok");

            output.Write("testing binding with multiple endpoints and AMI... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("AdapterAMI11", "default") !,
                    com.CreateObjectAdapter("AdapterAMI12", "default") !,
                    com.CreateObjectAdapter("AdapterAMI13", "default") !
                };

                //
                // Ensure that when a connection is opened it's reused for new
                // proxies and that all endpoints are eventually tried.
                //
                var names = new List <string>
                {
                    "AdapterAMI11",
                    "AdapterAMI12",
                    "AdapterAMI13"
                };

                while (names.Count > 0)
                {
                    var adpts = new List <IRemoteObjectAdapterPrx>(adapters);

                    ITestIntfPrx test1 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test2 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test3 = CreateTestIntfPrx(adpts);
                    test1.IcePing();
                    TestHelper.Assert(test1.GetConnection() == test2.GetConnection());
                    TestHelper.Assert(test2.GetConnection() == test3.GetConnection());

                    names.Remove(GetAdapterNameWithAMI(test1));
                    test1.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                // Ensure that the proxy correctly caches the connection (we always send the request over the
                // same connection.)
                {
                    foreach (IRemoteObjectAdapterPrx?adpt in adapters)
                    {
                        adpt.GetTestIntf() !.IcePing();
                    }

                    ITestIntfPrx t    = CreateTestIntfPrx(adapters);
                    string       name = GetAdapterNameWithAMI(t);

                    for (int i = 0; i < 10; i++)
                    {
                        TestHelper.Assert(GetAdapterNameWithAMI(t) == name);
                    }

                    foreach (IRemoteObjectAdapterPrx?adpt in adapters)
                    {
                        adpt.GetTestIntf() !.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                    }
                }

                //
                // Deactivate an adapter and ensure that we can still
                // establish the connection to the remaining adapters.
                //
                com.DeactivateObjectAdapter(adapters[0]);
                names.Add("AdapterAMI12");
                names.Add("AdapterAMI13");
                while (names.Count > 0)
                {
                    var adpts = new List <IRemoteObjectAdapterPrx>(adapters);

                    ITestIntfPrx test1 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test2 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test3 = CreateTestIntfPrx(adpts);

                    TestHelper.Assert(test1.GetConnection() == test2.GetConnection());
                    TestHelper.Assert(test2.GetConnection() == test3.GetConnection());

                    names.Remove(GetAdapterNameWithAMI(test1));
                    test1.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                // Deactivate an adapter and ensure that we can still establish the connection
                // to the remaining adapter.
                com.DeactivateObjectAdapter(adapters[2]);
                ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                TestHelper.Assert(GetAdapterNameWithAMI(obj).Equals("AdapterAMI12"));

                Deactivate(com, adapters);
            }
            output.WriteLine("ok");

            output.Write("testing random endpoint selection... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("Adapter21", "default") !,
                    com.CreateObjectAdapter("Adapter22", "default") !,
                    com.CreateObjectAdapter("Adapter23", "default") !
                };

                ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                TestHelper.Assert(obj.EndpointSelection == EndpointSelectionType.Random);

                var names = new List <string>
                {
                    "Adapter21",
                    "Adapter22",
                    "Adapter23"
                };

                while (names.Count > 0)
                {
                    names.Remove(obj.GetAdapterName());
                    obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                obj = obj.Clone(endpointSelection: EndpointSelectionType.Random);
                TestHelper.Assert(obj.EndpointSelection == EndpointSelectionType.Random);

                names.Add("Adapter21");
                names.Add("Adapter22");
                names.Add("Adapter23");
                while (names.Count > 0)
                {
                    names.Remove(obj.GetAdapterName());
                    obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                Deactivate(com, adapters);
            }
            output.WriteLine("ok");

            output.Write("testing ordered endpoint selection... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("Adapter31", "default") !,
                    com.CreateObjectAdapter("Adapter32", "default") !,
                    com.CreateObjectAdapter("Adapter33", "default") !
                };

                ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                obj = obj.Clone(endpointSelection: EndpointSelectionType.Ordered);
                TestHelper.Assert(obj.EndpointSelection == EndpointSelectionType.Ordered);

                // Ensure that endpoints are tried in order by deactivating the adapters
                // one after the other.
                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter31");
                }
                com.DeactivateObjectAdapter(adapters[0]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter32");
                }
                com.DeactivateObjectAdapter(adapters[1]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter33");
                }
                com.DeactivateObjectAdapter(adapters[2]);

                try
                {
                    obj.GetAdapterName();
                }
                catch (ConnectFailedException)
                {
                }

                IReadOnlyList <Endpoint> endpoints = obj.Endpoints;

                adapters.Clear();

                // TODO: ice1-only for now, because we send the client endpoints for use in OA configuration.
                if (communicator.DefaultProtocol == Protocol.Ice1)
                {
                    // Now, re-activate the adapters with the same endpoints in the opposite order.
                    adapters.Add(com.CreateObjectAdapterWithEndpoints("Adapter36", endpoints[2].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName() == "Adapter36");
                    }
                    obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);

                    adapters.Add(com.CreateObjectAdapterWithEndpoints("Adapter35", endpoints[1].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName() == "Adapter35");
                    }
                    obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);

                    adapters.Add(com.CreateObjectAdapterWithEndpoints("Adapter34", endpoints[0].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName() == "Adapter34");
                    }
                    Deactivate(com, adapters);
                }
            }
            output.WriteLine("ok");

            output.Write("testing per request binding with single endpoint... ");
            output.Flush();
            {
                IRemoteObjectAdapterPrx?adapter = com.CreateObjectAdapter("Adapter41", "default");
                TestHelper.Assert(adapter != null);
                ITestIntfPrx test1 = adapter.GetTestIntf() !.Clone(cacheConnection: false);
                ITestIntfPrx test2 = adapter.GetTestIntf() !.Clone(cacheConnection: false);
                TestHelper.Assert(!test1.IsConnectionCached);
                TestHelper.Assert(!test2.IsConnectionCached);
                TestHelper.Assert(test1.GetConnection() != null && test2.GetConnection() != null);
                TestHelper.Assert(test1.GetConnection() == test2.GetConnection());

                test1.IcePing();

                com.DeactivateObjectAdapter(adapter);

                var test3 = ITestIntfPrx.UncheckedCast(test1);
                try
                {
                    TestHelper.Assert(test3.GetConnection() == test1.GetConnection());
                    TestHelper.Assert(false);
                }
                catch (ConnectFailedException)
                {
                }
            }
            output.WriteLine("ok");

            output.Write("testing per request binding with multiple endpoints... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("Adapter51", "default") !,
                    com.CreateObjectAdapter("Adapter52", "default") !,
                    com.CreateObjectAdapter("Adapter53", "default") !
                };

                ITestIntfPrx obj = CreateTestIntfPrx(adapters).Clone(cacheConnection: false);
                TestHelper.Assert(!obj.IsConnectionCached);

                var names = new List <string>
                {
                    "Adapter51",
                    "Adapter52",
                    "Adapter53"
                };
                while (names.Count > 0)
                {
                    names.Remove(obj.GetAdapterName());
                }

                com.DeactivateObjectAdapter(adapters[0]);

                names.Add("Adapter52");
                names.Add("Adapter53");
                while (names.Count > 0)
                {
                    names.Remove(obj.GetAdapterName());
                }

                com.DeactivateObjectAdapter(adapters[2]);

                TestHelper.Assert(obj.GetAdapterName().Equals("Adapter52"));

                Deactivate(com, adapters);
            }
            output.WriteLine("ok");

            output.Write("testing per request binding with multiple endpoints and AMI... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("AdapterAMI51", "default") !,
                    com.CreateObjectAdapter("AdapterAMI52", "default") !,
                    com.CreateObjectAdapter("AdapterAMI53", "default") !
                };

                ITestIntfPrx obj = CreateTestIntfPrx(adapters).Clone(cacheConnection: false);
                TestHelper.Assert(!obj.IsConnectionCached);

                var names = new List <string>
                {
                    "AdapterAMI51",
                    "AdapterAMI52",
                    "AdapterAMI53"
                };
                while (names.Count > 0)
                {
                    names.Remove(GetAdapterNameWithAMI(obj));
                }

                com.DeactivateObjectAdapter(adapters[0]);

                names.Add("AdapterAMI52");
                names.Add("AdapterAMI53");
                while (names.Count > 0)
                {
                    names.Remove(GetAdapterNameWithAMI(obj));
                }

                com.DeactivateObjectAdapter(adapters[2]);

                TestHelper.Assert(GetAdapterNameWithAMI(obj).Equals("AdapterAMI52"));

                Deactivate(com, adapters);
            }
            output.WriteLine("ok");

            output.Write("testing per request binding and ordered endpoint selection... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("Adapter61", "default") !,
                    com.CreateObjectAdapter("Adapter62", "default") !,
                    com.CreateObjectAdapter("Adapter63", "default") !
                };

                ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                obj = obj.Clone(endpointSelection: EndpointSelectionType.Ordered);
                TestHelper.Assert(obj.EndpointSelection == EndpointSelectionType.Ordered);
                obj = obj.Clone(cacheConnection: false);
                TestHelper.Assert(!obj.IsConnectionCached);

                // Ensure that endpoints are tried in order by deactivating the adapters one after the other.
                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter61");
                }
                com.DeactivateObjectAdapter(adapters[0]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter62");
                }
                com.DeactivateObjectAdapter(adapters[1]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter63");
                }
                com.DeactivateObjectAdapter(adapters[2]);

                try
                {
                    obj.GetAdapterName();
                }
                catch (ConnectFailedException)
                {
                }

                IReadOnlyList <Endpoint> endpoints = obj.Endpoints;

                adapters.Clear();

                // TODO: ice1-only for now, because we send the client endpoints for use in OA configuration.
                if (communicator.DefaultProtocol == Protocol.Ice1)
                {
                    // Now, re-activate the adapters with the same endpoints in the opposite order.
                    adapters.Add(com.CreateObjectAdapterWithEndpoints("Adapter66", endpoints[2].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName() == "Adapter66");
                    }

                    adapters.Add(com.CreateObjectAdapterWithEndpoints("Adapter65", endpoints[1].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName() == "Adapter65");
                    }

                    adapters.Add(com.CreateObjectAdapterWithEndpoints("Adapter64", endpoints[0].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName() == "Adapter64");
                    }

                    Deactivate(com, adapters);
                }
            }
            output.WriteLine("ok");

            output.Write("testing per request binding and ordered endpoint selection and AMI... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("AdapterAMI61", "default") !,
                    com.CreateObjectAdapter("AdapterAMI62", "default") !,
                    com.CreateObjectAdapter("AdapterAMI63", "default") !
                };

                ITestIntfPrx?obj = CreateTestIntfPrx(adapters);
                obj = obj.Clone(endpointSelection: EndpointSelectionType.Ordered);
                TestHelper.Assert(obj.EndpointSelection == EndpointSelectionType.Ordered);
                obj = obj.Clone(cacheConnection: false);
                TestHelper.Assert(!obj.IsConnectionCached);

                // Ensure that endpoints are tried in order by deactivating the adapters one after the other.
                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI61");
                }
                com.DeactivateObjectAdapter(adapters[0]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI62");
                }
                com.DeactivateObjectAdapter(adapters[1]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI63");
                }
                com.DeactivateObjectAdapter(adapters[2]);

                try
                {
                    obj.GetAdapterName();
                }
                catch (ConnectFailedException)
                {
                }

                IReadOnlyList <Endpoint> endpoints = obj.Endpoints;

                adapters.Clear();

                // TODO: ice1-only for now, because we send the client endpoints for use in OA configuration.
                if (communicator.DefaultProtocol == Protocol.Ice1)
                {
                    // Now, re-activate the adapters with the same endpoints in the opposite order.
                    adapters.Add(com.CreateObjectAdapterWithEndpoints("AdapterAMI66", endpoints[2].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI66");
                    }

                    adapters.Add(com.CreateObjectAdapterWithEndpoints("AdapterAMI65", endpoints[1].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI65");
                    }

                    adapters.Add(com.CreateObjectAdapterWithEndpoints("AdapterAMI64", endpoints[0].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI64");
                    }
                    Deactivate(com, adapters);
                }
            }
            output.WriteLine("ok");

            if (communicator.DefaultProtocol == Protocol.Ice1)
            {
                output.Write("testing endpoint mode filtering... ");
                output.Flush();
                {
                    var adapters = new List <IRemoteObjectAdapterPrx>
                    {
                        com.CreateObjectAdapter("Adapter71", "default"),
                        com.CreateObjectAdapter("Adapter72", "udp")
                    };

                    ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                    TestHelper.Assert(obj.GetAdapterName().Equals("Adapter71"));

                    ITestIntfPrx testUDP = obj.Clone(invocationMode: InvocationMode.Datagram);
                    TestHelper.Assert(obj.GetConnection() != testUDP.GetConnection());
                    try
                    {
                        testUDP.GetAdapterName();
                        TestHelper.Assert(false);
                    }
                    catch (InvalidOperationException)
                    {
                        // expected
                    }
                }
                output.WriteLine("ok");
            }
            if (communicator.GetProperty("Ice.Plugin.IceSSL") != null)
            {
                output.Write("testing secure and non-secure endpoints... ");
                output.Flush();
                {
                    var adapters = new List <IRemoteObjectAdapterPrx>
                    {
                        com.CreateObjectAdapter("Adapter81", "ssl") !,
                        com.CreateObjectAdapter("Adapter82", "tcp") !
                    };

                    ITestIntfPrx obj = CreateTestIntfPrx(adapters);

                    for (int i = 0; i < 5; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName().Equals("Adapter82"));
                        obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                    }

                    ITestIntfPrx testNonSecure = obj.Clone(preferNonSecure: true);
                    // TODO: update when PreferNonSecure default is updated
                    ITestIntfPrx testSecure = obj.Clone(preferNonSecure: false);
                    TestHelper.Assert(obj.GetConnection() != testSecure.GetConnection());
                    TestHelper.Assert(obj.GetConnection() == testNonSecure.GetConnection());

                    com.DeactivateObjectAdapter(adapters[1]);

                    for (int i = 0; i < 5; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName().Equals("Adapter81"));
                        obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                    }

                    // TODO: ice1-only for now, because we send the client endpoints for use in OA configuration.
                    if (communicator.DefaultProtocol == Protocol.Ice1)
                    {
                        com.CreateObjectAdapterWithEndpoints("Adapter83", obj.Endpoints[1].ToString()); // Recreate a tcp OA.

                        for (int i = 0; i < 5; i++)
                        {
                            TestHelper.Assert(obj.GetAdapterName().Equals("Adapter83"));
                            obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                        }
                    }

                    com.DeactivateObjectAdapter(adapters[0]);

                    try
                    {
                        testSecure.IcePing();
                        TestHelper.Assert(false);
                    }
                    catch (ConnectionRefusedException)
                    {
                        // expected
                    }
                    Deactivate(com, adapters);
                }
                output.WriteLine("ok");
            }

            {
                output.Write("testing ipv4 & ipv6 connections... ");
                output.Flush();

                var ipv4 = new Dictionary <string, string>()
                {
                    { "IPv4", "1" },
                    { "IPv6", "0" },
                    { "Adapter.Endpoints", "tcp -h localhost" }
                };

                var ipv6 = new Dictionary <string, string>()
                {
                    { "IPv4", "0" },
                    { "IPv6", "1" },
                    { "Adapter.Endpoints", "tcp -h localhost" }
                };

                var bothPreferIPv4 = new Dictionary <string, string>()
                {
                    { "IPv4", "1" },
                    { "IPv6", "1" },
                    { "PreferIPv6Address", "0" },
                    { "Adapter.Endpoints", "tcp -h localhost" }
                };

                var bothPreferIPv6 = new Dictionary <string, string>()
                {
                    { "IPv4", "1" },
                    { "IPv6", "1" },
                    { "PreferIPv6Address", "1" },
                    { "Adapter.Endpoints", "tcp -h localhost" }
                };

                Dictionary <string, string>[] clientProps =
                {
                    ipv4, ipv6, bothPreferIPv4, bothPreferIPv6
                };

                string endpoint = "tcp -p " + helper.GetTestPort(2).ToString();

                var anyipv4 = new Dictionary <string, string>(ipv4)
                {
                    ["Adapter.Endpoints"]          = endpoint,
                    ["Adapter.PublishedEndpoints"] = $"{endpoint} -h 127.0.0.1"
                };

                var anyipv6 = new Dictionary <string, string>(ipv6)
                {
                    ["Adapter.Endpoints"]          = endpoint,
                    ["Adapter.PublishedEndpoints"] = $"{endpoint} -h \".1\""
                };

                var anyboth = new Dictionary <string, string>()
                {
                    { "IPv4", "1" },
                    { "IPv6", "1" },
                    { "Adapter.Endpoints", endpoint },
                    { "Adapter.PublishedEndpoints", $"{endpoint} -h \"::1\":{endpoint} -h 127.0.0.1" }
                };

                var localipv4 = new Dictionary <string, string>(ipv4)
                {
                    ["Adapter.Endpoints"] = "tcp -h 127.0.0.1"
                };

                var localipv6 = new Dictionary <string, string>(ipv6)
                {
                    ["Adapter.Endpoints"] = "tcp -h \"::1\""
                };

                Dictionary <string, string>[] serverProps =
                {
                    anyipv4,
                    anyipv6,
                    anyboth,
                    localipv4,
                    localipv6
                };

                bool ipv6NotSupported = false;
                foreach (Dictionary <string, string> p in serverProps)
                {
                    var           serverCommunicator = new Communicator(p);
                    ObjectAdapter oa;
                    try
                    {
                        oa = serverCommunicator.CreateObjectAdapter("Adapter");
                        oa.Activate();
                    }
                    catch (DNSException)
                    {
                        serverCommunicator.Dispose();
                        continue; // IP version not supported.
                    }
                    catch (TransportException)
                    {
                        if (p == ipv6)
                        {
                            ipv6NotSupported = true;
                        }
                        serverCommunicator.Dispose();
                        continue; // IP version not supported.
                    }

                    IObjectPrx prx = oa.CreateProxy("dummy", IObjectPrx.Factory);
                    try
                    {
                        prx.IcePing();
                    }
                    catch (DNSException) // TODO: is this really an expected exception?
                    {
                        serverCommunicator.Dispose();
                        continue;
                    }
                    catch (ObjectNotExistException) // TODO: is this really an expected exception?
                    {
                        serverCommunicator.Dispose();
                        continue;
                    }

                    string strPrx = prx.ToString() !;
                    foreach (Dictionary <string, string> q in clientProps)
                    {
                        var clientCommunicator = new Communicator(q);
                        prx = IObjectPrx.Parse(strPrx, clientCommunicator);
                        try
                        {
                            prx.IcePing();
                            TestHelper.Assert(false);
                        }
                        catch (ObjectNotExistException)
                        {
                            // Expected, no object registered.
                        }
                        catch (DNSException)
                        {
                            // Expected if no IPv4 or IPv6 address is
                            // associated to localhost or if trying to connect
                            // to an any endpoint with the wrong IP version,
                            // e.g.: resolving an IPv4 address when only IPv6
                            // is enabled fails with a DNS exception.
                        }
                        catch (TransportException)
                        {
                            TestHelper.Assert((p == ipv4 && q == ipv6) || (p == ipv6 && q == ipv4) ||
                                              (p == bothPreferIPv4 && q == ipv6) || (p == bothPreferIPv6 && q == ipv4) ||
                                              (p == bothPreferIPv6 && q == ipv6 && ipv6NotSupported) ||
                                              (p == anyipv4 && q == ipv6) || (p == anyipv6 && q == ipv4) ||
                                              (p == localipv4 && q == ipv6) || (p == localipv6 && q == ipv4) ||
                                              (p == ipv6 && q == bothPreferIPv4) || (p == ipv6 && q == bothPreferIPv6) ||
                                              (p == bothPreferIPv6 && q == ipv6));
                        }
                        clientCommunicator.Dispose();
                    }
                    serverCommunicator.Dispose();
                }

                output.WriteLine("ok");
            }

            com.Shutdown();
        }
예제 #10
0
 // Colocated call.
 public ValueTask DeactivateObjectAdapterAsync(
     IRemoteObjectAdapterPrx adapter,
     Current current,
     CancellationToken cancel) =>
예제 #11
0
 deactivateObjectAdapter(IRemoteObjectAdapterPrx adapter, Current current) => adapter.deactivate(); // Collocated call.
예제 #12
0
        public static void Run(TestHelper helper)
        {
            Communicator?communicator = helper.Communicator;
            bool         ice1         = helper.Protocol == Protocol.Ice1;

            TestHelper.Assert(communicator != null);
            var    com           = IRemoteCommunicatorPrx.Parse(helper.GetTestProxy("communicator", 0), communicator);
            string testTransport = helper.Transport;

            var rand = new Random(unchecked ((int)DateTime.Now.Ticks));

            System.IO.TextWriter output = helper.Output;

            output.Write("testing binding with single endpoint... ");
            output.Flush();
            {
                // Use "default" here to ensure that it still works
                IRemoteObjectAdapterPrx?adapter = com.CreateObjectAdapter("Adapter", "default");
                TestHelper.Assert(adapter != null);
                ITestIntfPrx?test1 = adapter.GetTestIntf();
                ITestIntfPrx?test2 = adapter.GetTestIntf();
                TestHelper.Assert(test1 != null && test2 != null);
                TestHelper.Assert(test1.GetConnection() == test2.GetConnection());

                test1.IcePing();
                test2.IcePing();

                com.DeactivateObjectAdapter(adapter);

                var test3 = test1.Clone(ITestIntfPrx.Factory);
                TestHelper.Assert(test3.GetConnection() == test1.GetConnection());
                TestHelper.Assert(test3.GetConnection() == test2.GetConnection());

                try
                {
                    test3.IcePing();
                    TestHelper.Assert(false);
                }
                catch (ConnectFailedException)
                {
                }
            }
            output.WriteLine("ok");

            output.Write("testing binding with multiple endpoints... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("Adapter11", testTransport) !,
                    com.CreateObjectAdapter("Adapter12", testTransport) !,
                    com.CreateObjectAdapter("Adapter13", testTransport) !
                };

                // Ensure that when a connection is opened it's reused for new proxies and that all endpoints are
                // eventually tried.
                var names = new List <string>
                {
                    "Adapter11",
                    "Adapter12",
                    "Adapter13"
                };

                while (names.Count > 0)
                {
                    var adpts = new List <IRemoteObjectAdapterPrx>(adapters);

                    ITestIntfPrx test1 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test2 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test3 = CreateTestIntfPrx(adpts);
                    test1.IcePing();
                    TestHelper.Assert(test1.GetConnection() == test2.GetConnection());
                    TestHelper.Assert(test2.GetConnection() == test3.GetConnection());

                    names.Remove(test1.GetAdapterName());
                    test1.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                // Ensure that the proxy correctly caches the connection(we always send the request over the same
                // connection.)
                {
                    foreach (IRemoteObjectAdapterPrx adpt in adapters)
                    {
                        adpt.GetTestIntf() !.IcePing();
                    }

                    ITestIntfPrx t    = CreateTestIntfPrx(adapters);
                    string       name = t.GetAdapterName();

                    for (int i = 0; i < 10 && t.GetAdapterName().Equals(name); i++)
                    {
                        TestHelper.Assert(t.GetAdapterName() == name);
                    }

                    foreach (IRemoteObjectAdapterPrx adpt in adapters)
                    {
                        adpt.GetTestIntf() !.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                    }
                }

                // Deactivate an adapter and ensure that we can still establish the connection to the remaining
                // adapters.
                com.DeactivateObjectAdapter(adapters[0]);
                names.Add("Adapter12");
                names.Add("Adapter13");
                while (names.Count > 0)
                {
                    var adpts = new List <IRemoteObjectAdapterPrx>(adapters);

                    ITestIntfPrx test1 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test2 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test3 = CreateTestIntfPrx(adpts);

                    TestHelper.Assert(test1.GetConnection() == test2.GetConnection());
                    TestHelper.Assert(test2.GetConnection() == test3.GetConnection());

                    names.Remove(test1.GetAdapterName());
                    test1.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                // Deactivate an adapter and ensure that we can still establish the connection to the remaining
                // adapter.
                com.DeactivateObjectAdapter(adapters[2]);
                ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                TestHelper.Assert(obj.GetAdapterName().Equals("Adapter12"));

                Deactivate(com, adapters);
            }
            output.WriteLine("ok");

            output.Write("testing binding with multiple random endpoints... ");
            output.Flush();
            {
                var adapters = new IRemoteObjectAdapterPrx[5]
                {
                    com.CreateObjectAdapter("AdapterRandom11", testTransport) !,
                    com.CreateObjectAdapter("AdapterRandom12", testTransport) !,
                    com.CreateObjectAdapter("AdapterRandom13", testTransport) !,
                    com.CreateObjectAdapter("AdapterRandom14", testTransport) !,
                    com.CreateObjectAdapter("AdapterRandom15", testTransport) !
                };

                int count        = 20;
                int adapterCount = adapters.Length;
                while (--count > 0)
                {
                    ITestIntfPrx[] proxies;
                    if (count == 1)
                    {
                        com.DeactivateObjectAdapter(adapters[4]);
                        --adapterCount;
                    }
                    proxies = new ITestIntfPrx[10];

                    int i;
                    for (i = 0; i < proxies.Length; ++i)
                    {
                        var adpts = new IRemoteObjectAdapterPrx[rand.Next(adapters.Length)];
                        if (adpts.Length == 0)
                        {
                            adpts = new IRemoteObjectAdapterPrx[1];
                        }
                        for (int j = 0; j < adpts.Length; ++j)
                        {
                            adpts[j] = adapters[rand.Next(adapters.Length)];
                        }
                        proxies[i] = CreateTestIntfPrx(new List <IRemoteObjectAdapterPrx>(adpts));
                    }

                    for (i = 0; i < proxies.Length; i++)
                    {
                        proxies[i].GetAdapterNameAsync();
                    }
                    for (i = 0; i < proxies.Length; i++)
                    {
                        try
                        {
                            proxies[i].IcePing();
                        }
                        catch
                        {
                        }
                    }

                    var connections = new List <Connection>();
                    for (i = 0; i < proxies.Length; i++)
                    {
                        if (proxies[i].GetCachedConnection() is Connection connection)
                        {
                            if (!connections.Contains(connection))
                            {
                                connections.Add(connection);
                            }
                        }
                    }
                    TestHelper.Assert(connections.Count <= adapterCount);

                    foreach (IRemoteObjectAdapterPrx a in adapters)
                    {
                        try
                        {
                            a.GetTestIntf() !.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                        }
                        catch
                        {
                            // Expected if adapter is down.
                        }
                    }
                }
            }
            output.WriteLine("ok");

            output.Write("testing binding with multiple endpoints and AMI... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("AdapterAMI11", testTransport) !,
                    com.CreateObjectAdapter("AdapterAMI12", testTransport) !,
                    com.CreateObjectAdapter("AdapterAMI13", testTransport) !
                };

                // Ensure that when a connection is opened it's reused for new proxies and that all endpoints are
                // eventually tried.
                var names = new List <string>
                {
                    "AdapterAMI11",
                    "AdapterAMI12",
                    "AdapterAMI13"
                };

                while (names.Count > 0)
                {
                    var adpts = new List <IRemoteObjectAdapterPrx>(adapters);

                    ITestIntfPrx test1 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test2 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test3 = CreateTestIntfPrx(adpts);
                    test1.IcePing();
                    TestHelper.Assert(test1.GetConnection() == test2.GetConnection());
                    TestHelper.Assert(test2.GetConnection() == test3.GetConnection());

                    names.Remove(GetAdapterNameWithAMI(test1));
                    test1.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                // Ensure that the proxy correctly caches the connection (we always send the request over the
                // same connection.)
                {
                    foreach (IRemoteObjectAdapterPrx?adpt in adapters)
                    {
                        adpt.GetTestIntf() !.IcePing();
                    }

                    ITestIntfPrx t    = CreateTestIntfPrx(adapters);
                    string       name = GetAdapterNameWithAMI(t);

                    for (int i = 0; i < 10; i++)
                    {
                        TestHelper.Assert(GetAdapterNameWithAMI(t) == name);
                    }

                    foreach (IRemoteObjectAdapterPrx?adpt in adapters)
                    {
                        adpt.GetTestIntf() !.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                    }
                }

                // Deactivate an adapter and ensure that we can still establish the connection to the remaining
                // adapters.
                com.DeactivateObjectAdapter(adapters[0]);
                names.Add("AdapterAMI12");
                names.Add("AdapterAMI13");
                while (names.Count > 0)
                {
                    var adpts = new List <IRemoteObjectAdapterPrx>(adapters);

                    ITestIntfPrx test1 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test2 = CreateTestIntfPrx(adpts);
                    Shuffle(ref adpts);
                    ITestIntfPrx test3 = CreateTestIntfPrx(adpts);

                    TestHelper.Assert(test1.GetConnection() == test2.GetConnection());
                    TestHelper.Assert(test2.GetConnection() == test3.GetConnection());

                    names.Remove(GetAdapterNameWithAMI(test1));
                    test1.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                // Deactivate an adapter and ensure that we can still establish the connection to the remaining
                // adapter.
                com.DeactivateObjectAdapter(adapters[2]);
                ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                TestHelper.Assert(GetAdapterNameWithAMI(obj).Equals("AdapterAMI12"));

                Deactivate(com, adapters);
            }
            output.WriteLine("ok");

            output.Write("testing random endpoint selection... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("Adapter21", testTransport) !,
                    com.CreateObjectAdapter("Adapter22", testTransport) !,
                    com.CreateObjectAdapter("Adapter23", testTransport) !
                };

                ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                TestHelper.Assert(obj.EndpointSelection == EndpointSelectionType.Random);

                var names = new List <string>
                {
                    "Adapter21",
                    "Adapter22",
                    "Adapter23"
                };

                while (names.Count > 0)
                {
                    names.Remove(obj.GetAdapterName());
                    obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                obj = obj.Clone(endpointSelection: EndpointSelectionType.Random);
                TestHelper.Assert(obj.EndpointSelection == EndpointSelectionType.Random);

                names.Add("Adapter21");
                names.Add("Adapter22");
                names.Add("Adapter23");
                while (names.Count > 0)
                {
                    names.Remove(obj.GetAdapterName());
                    obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                }

                Deactivate(com, adapters);
            }
            output.WriteLine("ok");

            output.Write("testing ordered endpoint selection... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("Adapter31", testTransport) !,
                    com.CreateObjectAdapter("Adapter32", testTransport) !,
                    com.CreateObjectAdapter("Adapter33", testTransport) !
                };

                ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                obj = obj.Clone(endpointSelection: EndpointSelectionType.Ordered);
                TestHelper.Assert(obj.EndpointSelection == EndpointSelectionType.Ordered);

                // Ensure that endpoints are tried in order by deactivating the adapters
                // one after the other.
                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter31");
                }
                com.DeactivateObjectAdapter(adapters[0]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter32");
                }
                com.DeactivateObjectAdapter(adapters[1]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter33");
                }
                com.DeactivateObjectAdapter(adapters[2]);

                try
                {
                    obj.GetAdapterName();
                }
                catch (ConnectFailedException)
                {
                }
                adapters.Clear();
            }
            output.WriteLine("ok");

            output.Write("testing per request binding with single endpoint... ");
            output.Flush();
            {
                IRemoteObjectAdapterPrx?adapter = com.CreateObjectAdapter("Adapter41", testTransport);
                TestHelper.Assert(adapter != null);
                ITestIntfPrx test1 = adapter.GetTestIntf() !.Clone(cacheConnection: false);
                ITestIntfPrx test2 = adapter.GetTestIntf() !.Clone(cacheConnection: false);
                TestHelper.Assert(!test1.IsConnectionCached);
                TestHelper.Assert(!test2.IsConnectionCached);
                TestHelper.Assert(test1.GetConnection() != null && test2.GetConnection() != null);
                TestHelper.Assert(test1.GetConnection() == test2.GetConnection());

                test1.IcePing();

                com.DeactivateObjectAdapter(adapter);

                var test3 = test1.Clone(ITestIntfPrx.Factory);
                try
                {
                    TestHelper.Assert(test3.GetConnection() == test1.GetConnection());
                    TestHelper.Assert(false);
                }
                catch (ConnectFailedException)
                {
                }
            }
            output.WriteLine("ok");

            output.Write("testing per request binding with multiple endpoints... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("Adapter51", testTransport) !,
                    com.CreateObjectAdapter("Adapter52", testTransport) !,
                    com.CreateObjectAdapter("Adapter53", testTransport) !
                };

                ITestIntfPrx obj = CreateTestIntfPrx(adapters).Clone(cacheConnection: false);
                TestHelper.Assert(!obj.IsConnectionCached);

                var names = new List <string>
                {
                    "Adapter51",
                    "Adapter52",
                    "Adapter53"
                };
                while (names.Count > 0)
                {
                    names.Remove(obj.GetAdapterName());
                }

                com.DeactivateObjectAdapter(adapters[0]);

                names.Add("Adapter52");
                names.Add("Adapter53");
                while (names.Count > 0)
                {
                    names.Remove(obj.GetAdapterName());
                }

                com.DeactivateObjectAdapter(adapters[2]);

                TestHelper.Assert(obj.GetAdapterName().Equals("Adapter52"));

                Deactivate(com, adapters);
            }
            output.WriteLine("ok");

            output.Write("testing per request binding with multiple endpoints and AMI... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("AdapterAMI51", testTransport) !,
                    com.CreateObjectAdapter("AdapterAMI52", testTransport) !,
                    com.CreateObjectAdapter("AdapterAMI53", testTransport) !
                };

                ITestIntfPrx obj = CreateTestIntfPrx(adapters).Clone(cacheConnection: false);
                TestHelper.Assert(!obj.IsConnectionCached);

                var names = new List <string>
                {
                    "AdapterAMI51",
                    "AdapterAMI52",
                    "AdapterAMI53"
                };
                while (names.Count > 0)
                {
                    names.Remove(GetAdapterNameWithAMI(obj));
                }

                com.DeactivateObjectAdapter(adapters[0]);

                names.Add("AdapterAMI52");
                names.Add("AdapterAMI53");
                while (names.Count > 0)
                {
                    names.Remove(GetAdapterNameWithAMI(obj));
                }

                com.DeactivateObjectAdapter(adapters[2]);

                TestHelper.Assert(GetAdapterNameWithAMI(obj).Equals("AdapterAMI52"));

                Deactivate(com, adapters);
            }
            output.WriteLine("ok");

            output.Write("testing per request binding and ordered endpoint selection... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("Adapter61", testTransport) !,
                    com.CreateObjectAdapter("Adapter62", testTransport) !,
                    com.CreateObjectAdapter("Adapter63", testTransport) !
                };

                ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                obj = obj.Clone(endpointSelection: EndpointSelectionType.Ordered);
                TestHelper.Assert(obj.EndpointSelection == EndpointSelectionType.Ordered);
                obj = obj.Clone(cacheConnection: false);
                TestHelper.Assert(!obj.IsConnectionCached);

                // Ensure that endpoints are tried in order by deactivating the adapters one after the other.
                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter61");
                }
                com.DeactivateObjectAdapter(adapters[0]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter62");
                }
                com.DeactivateObjectAdapter(adapters[1]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(obj.GetAdapterName() == "Adapter63");
                }
                com.DeactivateObjectAdapter(adapters[2]);

                try
                {
                    obj.GetAdapterName();
                }
                catch (ConnectFailedException)
                {
                }

                IReadOnlyList <Endpoint> endpoints = obj.Endpoints;

                adapters.Clear();

                // TODO: ice1-only for now, because we send the client endpoints for use in OA configuration.
                if (helper.Protocol == Protocol.Ice1)
                {
                    // Now, re-activate the adapters with the same endpoints in the opposite order.
                    adapters.Add(com.CreateObjectAdapterWithEndpoints("Adapter66", endpoints[2].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName() == "Adapter66");
                    }

                    adapters.Add(com.CreateObjectAdapterWithEndpoints("Adapter65", endpoints[1].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName() == "Adapter65");
                    }

                    adapters.Add(com.CreateObjectAdapterWithEndpoints("Adapter64", endpoints[0].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName() == "Adapter64");
                    }

                    Deactivate(com, adapters);
                }
            }
            output.WriteLine("ok");

            output.Write("testing per request binding and ordered endpoint selection and AMI... ");
            output.Flush();
            {
                var adapters = new List <IRemoteObjectAdapterPrx>
                {
                    com.CreateObjectAdapter("AdapterAMI61", testTransport) !,
                    com.CreateObjectAdapter("AdapterAMI62", testTransport) !,
                    com.CreateObjectAdapter("AdapterAMI63", testTransport) !
                };

                ITestIntfPrx?obj = CreateTestIntfPrx(adapters);
                obj = obj.Clone(endpointSelection: EndpointSelectionType.Ordered);
                TestHelper.Assert(obj.EndpointSelection == EndpointSelectionType.Ordered);
                obj = obj.Clone(cacheConnection: false);
                TestHelper.Assert(!obj.IsConnectionCached);

                // Ensure that endpoints are tried in order by deactivating the adapters one after the other.
                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI61");
                }
                com.DeactivateObjectAdapter(adapters[0]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI62");
                }
                com.DeactivateObjectAdapter(adapters[1]);

                for (int i = 0; i < 3; i++)
                {
                    TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI63");
                }
                com.DeactivateObjectAdapter(adapters[2]);

                try
                {
                    obj.GetAdapterName();
                }
                catch (ConnectFailedException)
                {
                }

                IReadOnlyList <Endpoint> endpoints = obj.Endpoints;

                adapters.Clear();

                // TODO: ice1-only for now, because we send the client endpoints for use in OA configuration.
                if (helper.Protocol == Protocol.Ice1)
                {
                    // Now, re-activate the adapters with the same endpoints in the opposite order.
                    adapters.Add(com.CreateObjectAdapterWithEndpoints("AdapterAMI66", endpoints[2].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI66");
                    }

                    adapters.Add(com.CreateObjectAdapterWithEndpoints("AdapterAMI65", endpoints[1].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI65");
                    }

                    adapters.Add(com.CreateObjectAdapterWithEndpoints("AdapterAMI64", endpoints[0].ToString()));
                    for (int i = 0; i < 3; i++)
                    {
                        TestHelper.Assert(GetAdapterNameWithAMI(obj) == "AdapterAMI64");
                    }
                    Deactivate(com, adapters);
                }
            }
            output.WriteLine("ok");

            if (helper.Protocol == Protocol.Ice1)
            {
                output.Write("testing endpoint mode filtering... ");
                output.Flush();
                {
                    var adapters = new List <IRemoteObjectAdapterPrx>
                    {
                        com.CreateObjectAdapter("Adapter71", testTransport),
                        com.CreateObjectAdapter("Adapter72", "udp")
                    };

                    ITestIntfPrx obj = CreateTestIntfPrx(adapters);
                    TestHelper.Assert(obj.GetAdapterName().Equals("Adapter71"));

                    ITestIntfPrx testUDP = obj.Clone(invocationMode: InvocationMode.Datagram);
                    TestHelper.Assert(obj.GetConnection() != testUDP.GetConnection());
                    try
                    {
                        testUDP.GetAdapterName();
                        TestHelper.Assert(false);
                    }
                    catch (InvalidOperationException)
                    {
                        // expected
                    }
                }
                output.WriteLine("ok");
            }
            if (communicator.GetProperty("Ice.Plugin.IceSSL") != null)
            {
                output.Write("testing secure and non-secure endpoints... ");
                output.Flush();
                {
                    var adapters = new List <IRemoteObjectAdapterPrx>
                    {
                        com.CreateObjectAdapter("Adapter81", "ssl") !,
                        com.CreateObjectAdapter("Adapter82", "tcp") !
                    };

                    ITestIntfPrx obj = CreateTestIntfPrx(adapters);

                    for (int i = 0; i < 5; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName().Equals("Adapter82"));
                        obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                    }

                    ITestIntfPrx testNonSecure = obj.Clone(preferNonSecure: true);
                    // TODO: update when PreferNonSecure default is updated
                    ITestIntfPrx testSecure = obj.Clone(preferNonSecure: false);
                    TestHelper.Assert(obj.GetConnection() != testSecure.GetConnection());
                    TestHelper.Assert(obj.GetConnection() == testNonSecure.GetConnection());

                    com.DeactivateObjectAdapter(adapters[1]);

                    for (int i = 0; i < 5; i++)
                    {
                        TestHelper.Assert(obj.GetAdapterName().Equals("Adapter81"));
                        obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                    }

                    // TODO: ice1-only for now, because we send the client endpoints for use in OA configuration.
                    if (helper.Protocol == Protocol.Ice1)
                    {
                        com.CreateObjectAdapterWithEndpoints("Adapter83", obj.Endpoints[1].ToString()); // Recreate a tcp OA.

                        for (int i = 0; i < 5; i++)
                        {
                            TestHelper.Assert(obj.GetAdapterName().Equals("Adapter83"));
                            obj.GetConnection() !.Close(ConnectionClose.GracefullyWithWait);
                        }
                    }

                    com.DeactivateObjectAdapter(adapters[0]);

                    try
                    {
                        testSecure.IcePing();
                        TestHelper.Assert(false);
                    }
                    catch (ConnectionRefusedException)
                    {
                        // expected
                    }
                    Deactivate(com, adapters);
                }
                output.WriteLine("ok");
            }

            {
                output.Write("testing ipv4 & ipv6 connections... ");
                output.Flush();

                Func <string, string> getEndpoint = host =>
                                                    TestHelper.GetTestEndpoint(
                    new Dictionary <string, string>(communicator.GetProperties())
                {
                    ["Test.Host"] = host
                },
                    2,
                    "tcp");

                Func <string, string, string> getProxy = (identity, host) =>
                                                         TestHelper.GetTestProxy(
                    identity,
                    new Dictionary <string, string>(communicator.GetProperties())
                {
                    ["Test.Host"] = host
                },
                    2,
                    "tcp");

                var anyipv4 = new Dictionary <string, string>()
                {
                    ["Adapter.Endpoints"]          = getEndpoint("0.0.0.0"),
                    ["Adapter.PublishedEndpoints"] = getEndpoint("127.0.0.1")
                };

                var anyipv6 = new Dictionary <string, string>()
                {
                    ["Adapter.Endpoints"]          = getEndpoint("::0"),
                    ["Adapter.PublishedEndpoints"] = getEndpoint("::1")
                };

                var anyipv46 = new Dictionary <string, string>()
                {
                    ["Adapter.Endpoints"]          = getEndpoint("::0"),
                    ["Adapter.PublishedEndpoints"] = getEndpoint("127.0.0.1")
                };

                var anylocalhost = new Dictionary <string, string>()
                {
                    { "Adapter.Endpoints", getEndpoint("::0") },
                    { "Adapter.PublishedEndpoints", getEndpoint("localhost") }
                };

                var localipv4 = new Dictionary <string, string>()
                {
                    ["Adapter.Endpoints"] = getEndpoint("127.0.0.1")
                };

                var localipv6 = new Dictionary <string, string>()
                {
                    ["Adapter.Endpoints"] = getEndpoint("::1")
                };

                var localhost = new Dictionary <string, string>()
                {
                    ["Adapter.Endpoints"] = getEndpoint("localhost")
                };

                var serverProps = new Dictionary <string, string>[]
                {
                    anyipv4,
                    anyipv6,
                    anyipv46,
                    anylocalhost,
                    localipv4,
                    localipv6,
                    localhost
                };

                foreach (Dictionary <string, string> p in serverProps)
                {
                    using var serverCommunicator = new Communicator(p);
                    ObjectAdapter oa = serverCommunicator.CreateObjectAdapter("Adapter");
                    oa.Activate();

                    IObjectPrx prx = oa.CreateProxy("dummy", IObjectPrx.Factory);
                    try
                    {
                        using var clientCommunicator = new Communicator();
                        prx = IObjectPrx.Parse(prx.ToString() !, clientCommunicator);
                        prx.IcePing();
                        TestHelper.Assert(false);
                    }
                    catch (ObjectNotExistException)
                    {
                        // Expected. OA is reachable but there's no "dummy" object
                    }
                }

                // Test IPv6 dual mode socket
                {
                    using Communicator serverCommunicator = new Communicator();
                    string        endpoint = getEndpoint("::0");
                    ObjectAdapter oa       = serverCommunicator.CreateObjectAdapterWithEndpoints(endpoint);
                    oa.Activate();

                    try
                    {
                        using ObjectAdapter ipv4OA =
                                  serverCommunicator.CreateObjectAdapterWithEndpoints(getEndpoint("0.0.0.0"));
                        ipv4OA.Activate();
                        TestHelper.Assert(false);
                    }
                    catch (TransportException)
                    {
                        // Expected. ::0 is a dual-mode socket so binding 0.0.0.0 will fail
                    }

                    try
                    {
                        using Communicator clientCommunicator = new Communicator();
                        var prx = IObjectPrx.Parse(getProxy("dummy", "127.0.0.1"), clientCommunicator);
                        prx.IcePing();
                    }
                    catch (ObjectNotExistException)
                    {
                        // Expected, no object registered.
                    }
                }

                // Test IPv6 only socket
                {
                    using Communicator serverCommunicator = new Communicator();
                    string        endpoint = getEndpoint("::0") + (ice1 ? " --ipv6Only" : "?ipv6-only=true");
                    ObjectAdapter oa       = serverCommunicator.CreateObjectAdapterWithEndpoints(endpoint);
                    oa.Activate();

                    // Returned endpoints must be IPv6
                    {
                        IObjectPrx prx = oa.CreateProxy("dummy", IObjectPrx.Factory);
                        TestHelper.Assert(prx.Endpoints.Count > 0);
                        TestHelper.Assert(prx.Endpoints.All(e => e.Host.Contains(":")));
                    }

                    // 0.0.0.0 can still be bound if ::0 is IPv6 only
                    {
                        string ipv4Endpoint = getEndpoint("0.0.0.0");
                        using ObjectAdapter ipv4OA = serverCommunicator.CreateObjectAdapterWithEndpoints(ipv4Endpoint);
                        ipv4OA.Activate();
                    }

                    try
                    {
                        using Communicator clientCommunicator = new Communicator();
                        var prx = IObjectPrx.Parse(getProxy("dummy", "127.0.0.1"), clientCommunicator);
                        prx.IcePing();
                        TestHelper.Assert(false);
                    }
                    catch (ConnectionRefusedException)
                    {
                        // Expected, server socket is IPv6 only.
                    }
                }

                // Listen on IPv4 loopback with IPv6 dual mode socket
                {
                    using Communicator serverCommunicator = new Communicator();
                    string        endpoint = getEndpoint("::ffff:127.0.0.1");
                    ObjectAdapter oa       = serverCommunicator.CreateObjectAdapterWithEndpoints(endpoint);
                    oa.Activate();

                    try
                    {
                        string ipv4Endpoint = getEndpoint("127.0.0.1");
                        using ObjectAdapter ipv4OA = serverCommunicator.CreateObjectAdapterWithEndpoints(ipv4Endpoint);
                        ipv4OA.Activate();
                        TestHelper.Assert(false);
                    }
                    catch (TransportException)
                    {
                        // Expected. 127.0.0.1 is already in use
                    }

                    // Returned endpoint must be IPv6
                    {
                        IObjectPrx prx = oa.CreateProxy("dummy", IObjectPrx.Factory);
                        TestHelper.Assert(prx.Endpoints.Count == 1);
                        TestHelper.Assert(prx.Endpoints.All(e => e.Host.Contains(":")));
                    }

                    try
                    {
                        using Communicator clientCommunicator = new Communicator();
                        var prx = IObjectPrx.Parse(getProxy("dummy", "127.0.0.1"), clientCommunicator);
                        prx.IcePing();
                    }
                    catch (ObjectNotExistException)
                    {
                        // Expected, no object registered.
                    }
                }

                output.WriteLine("ok");
            }

            com.Shutdown();
        }