/// <summary>
        /// Test that we can create a socket and close it, even if it hasn't been
        /// opened.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestSocketCreateAndClose()
        {
            DomainSocket serv = DomainSocket.BindAndListen(new FilePath(sockDir.GetDir(), "test_sock_create_and_close"
                                                                        ).GetAbsolutePath());

            serv.Close();
        }
        /// <summary>Test setting some server options.</summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public virtual void TestServerOptions()
        {
            string TestPath = new FilePath(sockDir.GetDir(), "test_sock_server_options").GetAbsolutePath
                                  ();
            DomainSocket serv = DomainSocket.BindAndListen(TestPath);

            try
            {
                // Let's set a new receive buffer size
                int bufSize    = serv.GetAttribute(DomainSocket.ReceiveBufferSize);
                int newBufSize = bufSize / 2;
                serv.SetAttribute(DomainSocket.ReceiveBufferSize, newBufSize);
                int nextBufSize = serv.GetAttribute(DomainSocket.ReceiveBufferSize);
                Assert.Equal(newBufSize, nextBufSize);
                // Let's set a server timeout
                int newTimeout = 1000;
                serv.SetAttribute(DomainSocket.ReceiveTimeout, newTimeout);
                int nextTimeout = serv.GetAttribute(DomainSocket.ReceiveTimeout);
                Assert.Equal(newTimeout, nextTimeout);
                try
                {
                    serv.Accept();
                    NUnit.Framework.Assert.Fail("expected the accept() to time out and fail");
                }
                catch (SocketTimeoutException e)
                {
                    GenericTestUtils.AssertExceptionContains("accept(2) error: ", e);
                }
            }
            finally
            {
                serv.Close();
                NUnit.Framework.Assert.IsFalse(serv.IsOpen());
            }
        }
        /// <summary>
        /// Test that we get an AsynchronousCloseException when the DomainSocket
        /// we're using is closed during a read or write operation.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        private void TestAsyncCloseDuringIO(bool closeDuringWrite)
        {
            string TestPath = new FilePath(sockDir.GetDir(), "testAsyncCloseDuringIO(" + closeDuringWrite
                                           + ")").GetAbsolutePath();
            DomainSocket    serv           = DomainSocket.BindAndListen(TestPath);
            ExecutorService exeServ        = Executors.NewFixedThreadPool(2);
            Callable <Void> serverCallable = new _Callable_180(serv, closeDuringWrite);
            // The server just continues either writing or reading until someone
            // asynchronously closes the client's socket.  At that point, all our
            // reads return EOF, and writes get a socket error.
            Future <Void>   serverFuture   = exeServ.Submit(serverCallable);
            DomainSocket    clientConn     = DomainSocket.Connect(serv.GetPath());
            Callable <Void> clientCallable = new _Callable_213(closeDuringWrite, clientConn);
            // The client writes or reads until another thread
            // asynchronously closes the socket.  At that point, we should
            // get ClosedChannelException, or possibly its subclass
            // AsynchronousCloseException.
            Future <Void> clientFuture = exeServ.Submit(clientCallable);

            Thread.Sleep(500);
            clientConn.Close();
            serv.Close();
            clientFuture.Get(2, TimeUnit.Minutes);
            serverFuture.Get(2, TimeUnit.Minutes);
        }
        /// <summary>
        /// Test that if one thread is blocking in a read or write operation, another
        /// thread can close the socket and stop the accept.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public virtual void TestSocketAcceptAndClose()
        {
            string TestPath = new FilePath(sockDir.GetDir(), "test_sock_accept_and_close").GetAbsolutePath
                                  ();
            DomainSocket    serv     = DomainSocket.BindAndListen(TestPath);
            ExecutorService exeServ  = Executors.NewSingleThreadExecutor();
            Callable <Void> callable = new _Callable_149(serv);
            Future <Void>   future   = exeServ.Submit(callable);

            Thread.Sleep(500);
            serv.Close();
            future.Get(2, TimeUnit.Minutes);
        }
        /// <summary>Test that we get a read result of -1 on EOF.</summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public virtual void TestSocketReadEof()
        {
            string TestPath = new FilePath(sockDir.GetDir(), "testSocketReadEof").GetAbsolutePath
                                  ();
            DomainSocket    serv     = DomainSocket.BindAndListen(TestPath);
            ExecutorService exeServ  = Executors.NewSingleThreadExecutor();
            Callable <Void> callable = new _Callable_109(serv);
            Future <Void>   future   = exeServ.Submit(callable);
            DomainSocket    conn     = DomainSocket.Connect(serv.GetPath());

            Thread.Sleep(50);
            conn.Close();
            serv.Close();
            future.Get(2, TimeUnit.Minutes);
        }
        /// <summary>Test file descriptor passing.</summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public virtual void TestFdPassing()
        {
            string TestPath = new FilePath(sockDir.GetDir(), "test_sock").GetAbsolutePath();

            byte[] clientMsg1 = new byte[] { unchecked ((int)(0x11)), unchecked ((int)(0x22)),
                                             unchecked ((int)(0x33)), unchecked ((int)(0x44)), unchecked ((int)(0x55)), unchecked (
                                                 (int)(0x66)) };
            byte[] serverMsg1 = new byte[] { unchecked ((int)(0x31)), unchecked ((int)(0x30)),
                                             unchecked ((int)(0x32)), unchecked ((int)(0x34)), unchecked ((int)(0x31)), unchecked (
                                                 (int)(0x33)), unchecked ((int)(0x44)), unchecked ((int)(0x1)), unchecked ((int)(0x1
                                                                                                                                 )), unchecked ((int)(0x1)), unchecked ((int)(0x1)), unchecked ((int)(0x1)) };
            ArrayBlockingQueue <System.Exception> threadResults = new ArrayBlockingQueue <System.Exception
                                                                                          >(2);
            DomainSocket serv = DomainSocket.BindAndListen(TestPath);

            TestDomainSocket.PassedFile[] passedFiles = new TestDomainSocket.PassedFile[] { new
                                                                                            TestDomainSocket.PassedFile(1), new TestDomainSocket.PassedFile(2) };
            FileDescriptor[] passedFds = new FileDescriptor[passedFiles.Length];
            for (int i = 0; i < passedFiles.Length; i++)
            {
                passedFds[i] = passedFiles[i].GetInputStream().GetFD();
            }
            Thread serverThread = new _Thread_597(serv, clientMsg1, passedFds, serverMsg1
                                                  , threadResults);

            // Run server
            serverThread.Start();
            Thread clientThread = new _Thread_620(TestPath, clientMsg1, serverMsg1, passedFds
                                                  , passedFiles, threadResults);

            clientThread.Start();
            for (int i_1 = 0; i_1 < 2; i_1++)
            {
                System.Exception t = threadResults.Take();
                if (!(t is TestDomainSocket.Success))
                {
                    NUnit.Framework.Assert.Fail(t.Message + ExceptionUtils.GetStackTrace(t));
                }
            }
            serverThread.Join(120000);
            clientThread.Join(120000);
            serv.Close();
            foreach (TestDomainSocket.PassedFile pf in passedFiles)
            {
                pf.Cleanup();
            }
        }
        /// <summary>Test a simple client/server interaction.</summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        internal virtual void TestClientServer1(Type writeStrategyClass, Type readStrategyClass
                                                , DomainSocket[] preConnectedSockets)
        {
            string TestPath = new FilePath(sockDir.GetDir(), "test_sock_client_server1").GetAbsolutePath
                                  ();

            byte[] clientMsg1 = new byte[] { unchecked ((int)(0x1)), unchecked ((int)(0x2)), unchecked (
                                                 (int)(0x3)), unchecked ((int)(0x4)), unchecked ((int)(0x5)), unchecked ((int)(0x6)) };
            byte[] serverMsg1 = new byte[] { unchecked ((int)(0x9)), unchecked ((int)(0x8)), unchecked (
                                                 (int)(0x7)), unchecked ((int)(0x6)), unchecked ((int)(0x5)) };
            byte clientMsg2   = unchecked ((int)(0x45));
            ArrayBlockingQueue <Exception> threadResults = new ArrayBlockingQueue <Exception>(2
                                                                                              );
            DomainSocket serv = (preConnectedSockets != null) ? null : DomainSocket.BindAndListen
                                    (TestPath);
            Thread serverThread = new _Thread_435(preConnectedSockets, serv, clientMsg1
                                                  , readStrategyClass, writeStrategyClass, serverMsg1, clientMsg2, threadResults);

            // Run server
            serverThread.Start();
            Thread clientThread = new _Thread_463(preConnectedSockets, TestPath, writeStrategyClass
                                                  , clientMsg1, readStrategyClass, serverMsg1, clientMsg2, threadResults);

            clientThread.Start();
            for (int i = 0; i < 2; i++)
            {
                Exception t = threadResults.Take();
                if (!(t is TestDomainSocket.Success))
                {
                    NUnit.Framework.Assert.Fail(t.Message + ExceptionUtils.GetStackTrace(t));
                }
            }
            serverThread.Join(120000);
            clientThread.Join(120000);
            if (serv != null)
            {
                serv.Close();
            }
        }