Exemplo n.º 1
0
        /// <summary>Verify the RPC server can shutdown properly when callQueue is full.</summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestRPCServerShutdown()
        {
            int numClients                         = 3;
            IList <Future <Void> > res             = new AList <Future <Void> >();
            ExecutorService        executorService = Executors.NewFixedThreadPool(numClients);
            Configuration          conf            = new Configuration();

            conf.SetInt(CommonConfigurationKeys.IpcClientConnectMaxRetriesKey, 0);
            Server server = new RPC.Builder(conf).SetProtocol(typeof(TestRPC.TestProtocol)).SetInstance
                                (new TestRPC.TestImpl()).SetBindAddress(Address).SetPort(0).SetQueueSizePerHandler
                                (1).SetNumHandlers(1).SetVerbose(true).Build();

            server.Start();
            TestRPC.TestProtocol proxy = RPC.GetProxy <TestRPC.TestProtocol>(TestRPC.TestProtocol
                                                                             .versionID, NetUtils.GetConnectAddress(server), conf);
            try
            {
                // start a sleep RPC call to consume the only handler thread.
                // Start another sleep RPC call to make callQueue full.
                // Start another sleep RPC call to make reader thread block on CallQueue.
                for (int i = 0; i < numClients; i++)
                {
                    res.AddItem(executorService.Submit(new _Callable_1046(proxy)));
                }
                while (server.GetCallQueueLen() != 1 && CountThreads(typeof(CallQueueManager).FullName
                                                                     ) != 1 && CountThreads(typeof(TestRPC.TestProtocol).FullName) != 1)
                {
                    Thread.Sleep(100);
                }
            }
            finally
            {
                try
                {
                    server.Stop();
                    Assert.Equal("Not enough clients", numClients, res.Count);
                    foreach (Future <Void> f in res)
                    {
                        try
                        {
                            f.Get();
                            NUnit.Framework.Assert.Fail("Future get should not return");
                        }
                        catch (ExecutionException e)
                        {
                            Assert.True("Unexpected exception: " + e, e.InnerException is IOException
                                        );
                            Log.Info("Expected exception", e.InnerException);
                        }
                    }
                }
                finally
                {
                    RPC.StopProxy(proxy);
                    executorService.Shutdown();
                }
            }
        }