Example #1
0
        [Explicit] // Wierd threading issues?????
        public void AcceptsWithStopStartInBetween()
        {
            IAsyncResult ar;
            var          lsnr = SetupListenerWithPorts(
                NullBtListener.LsnrSetting.ConnectsImmediately,
                NullBtListener.LsnrSetting.ConnectsImmediately,
                NullBtListener.LsnrSetting.ConnectsImmediately,
                NullBtListener.LsnrSetting.ConnectsImmediately
                ).Lsnr;

            lsnr.Start();
            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            TestsApmUtils.SafeNoHangWaitShort(ar, "EndAccept-1");
            lsnr.EndAcceptBluetoothClient(ar);
            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            TestsApmUtils.SafeNoHangWaitShort(ar, "EndAccept-2");
            lsnr.EndAcceptBluetoothClient(ar);
            //
            lsnr.Stop();
            ForceFinalization();
            lsnr.Start();
            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            TestsApmUtils.SafeNoHangWaitShort(ar, "EndAccept-3");
            lsnr.EndAcceptBluetoothClient(ar);
            ForceFinalization();
        }
Example #2
0
        public void TenAcceptsOnTenPortsPlusLastOneNotComplete()
        {
            int          N = 10;
            IAsyncResult ar;
            var          settings = new List <NullBtListener.LsnrSetting>();

            for (int i = 0; i < N; ++i)
            {
                settings.Add(NullBtListener.LsnrSetting.ConnectsImmediately);
            }
            settings.Add(NullBtListener.LsnrSetting.None);
            var lsnr = SetupListenerWithPorts(settings.ToArray()).Lsnr;

            lsnr.Start();
            //
            for (int i = 0; i < N; ++i)
            {
                ar = lsnr.BeginAcceptBluetoothClient(null, null);
                TestsApmUtils.SafeNoHangWaitShort(ar, "EndAccept-#" + i.ToString());
                lsnr.EndAcceptBluetoothClient(ar);
            }
            // Not completes
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            TestsApmUtils.SafeNotCompletesMiddling(ar, "EndAccept-Last");
        }
Example #3
0
        public void AcceptOverStop_TryTcpSocket()
        {
            System.Net.Sockets.Socket lsnr = null;
            Converter <System.Net.IPEndPoint, System.Net.Sockets.Socket> createListener
                = delegate(System.Net.IPEndPoint epX)
                {
                var s = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
                                                      System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Unspecified);
                s.Bind(epX);
                return(s);
                };
            var ep = new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 0);

            try {
                //-- NO start --
                lsnr = createListener(ep);
                lsnr.Listen(1);
                var arAccept = lsnr.BeginAccept(null, null);
                lsnr.Close();
                TestsApmUtils.SafeNoHangWaitShort(arAccept, "EndAccept");
                try {
                    lsnr.EndAccept(arAccept);
                    Assert.Fail("should have thrown");
                } catch (ObjectDisposedException ex) {
                    Assert.AreEqual(typeof(ObjectDisposedException), ex.GetType());
                }
            } finally {
                if (lsnr != null)
                {
                    lsnr.Close();
                }
            }
        }
Example #4
0
        public void AcceptOverStop_TryTcpListener()
        {
            System.Net.Sockets.TcpListener lsnr = null;
            var ep = new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 0);

            try {
                lsnr = new System.Net.Sockets.TcpListener(ep);
                lsnr.Start();
                var arAccept = lsnr.BeginAcceptTcpClient(null, null);
                // *
                lsnr.Stop();
                TestsApmUtils.SafeNoHangWaitShort(arAccept, "EndAccept");
                try {
                    lsnr.EndAcceptTcpClient(arAccept);
                    Assert.Fail("should have thrown");
                } catch (ObjectDisposedException ex) {
                    Assert.AreEqual(typeof(ObjectDisposedException), ex.GetType());
                }
            } finally {
                if (lsnr != null)
                {
                    lsnr.Stop();
                }
            }
        }
Example #5
0
        public void SecondPortCreateFails_ThenOkButServerStaysError_BeginAcceptBeforeFirstConn()
        {
            /*  The server is dead after any DoOpenServer etc port error.
             */
            var lsnr = SetupListenerWithPorts(
                NullBtListener.LsnrSetting.ConnectsImmediately,
                NullBtListener.LsnrSetting.ErrorOnOpenServer,
                NullBtListener.LsnrSetting.ConnectsImmediately,
                NullBtListener.LsnrSetting.ConnectsImmediately).Lsnr;

            lsnr.Start();
            //
            var ar1 = lsnr.BeginAcceptBluetoothClient(null, null);
            var ar2 = lsnr.BeginAcceptBluetoothClient(null, null);
            var ar3 = lsnr.BeginAcceptBluetoothClient(null, null);

            //
            TestsApmUtils.SafeNoHangWaitShort(ar1, "EndAccept-good");
            lsnr.EndAcceptBluetoothClient(ar1);
            //
            TestsApmUtils.SafeNoHangWaitShort(ar2, "EndAccept-Error");
            try {
                lsnr.EndAcceptBluetoothClient(ar2);
                Assert.Fail("should have thrown!");
            } catch (Exception) {
            }
            //
            TestsApmUtils.SafeNoHangWaitShort(ar3, "EndAccept-good-3");
            try {
                lsnr.EndAcceptBluetoothClient(ar3);
                Assert.Fail("should have thrown!");
            } catch (Exception) {
            }
        }
Example #6
0
        TestData SecondPortCreateFails_ThenStop__()
        {
            /*  The server is dead after any DoOpenServer etc port error.
             */
            var data = SetupListenerWithPorts(
                NullBtListener.LsnrSetting.ConnectsImmediately,
                NullBtListener.LsnrSetting.ErrorOnOpenServer,
                NullBtListener.LsnrSetting.ConnectsImmediately,
                NullBtListener.LsnrSetting.ConnectsImmediately);
            var lsnr = data.Lsnr;

            Assert.IsFalse(data.Fcty.TheBtLsnr.AllDisposed, "disposed 1 NOT");
            lsnr.Start();
            Assert.IsFalse(data.Fcty.TheBtLsnr.AllDisposed, "disposed 2 NOT");
            //
            var ar1 = lsnr.BeginAcceptBluetoothClient(null, null);

            //
            TestsApmUtils.SafeNoHangWaitShort(ar1, "EndAccept-good");
            lsnr.EndAcceptBluetoothClient(ar1);
            //
            var ar2 = lsnr.BeginAcceptBluetoothClient(null, null);

            TestsApmUtils.SafeNoHangWaitShort(ar2, "EndAccept-Error");
            try {
                lsnr.EndAcceptBluetoothClient(ar2);
                Assert.Fail("should have thrown!");
            } catch (Exception) {
            }
            //
            lsnr.Stop();
            Assert.IsTrue(data.Fcty.TheBtLsnr.AllDisposed);
            //
            return(data);
        }
Example #7
0
        public void LocalClose_WriteAfter()
        {
            BsTestNullApi api;
            var           conn = CreateConnectedStream(out api);

            AssertIsOpen(conn, true, "top");
            conn.Flush();
            var buf    = new byte[10];
            var action = (Action <Version>) delegate {
                conn.Write(buf, 0, buf.Length);
            };

            //var arList = new IAsyncResult[3];
            //for (int i = 0; i < arList.Length; ++i) {
            //    arList[i] = action.BeginInvoke(null, null, null);
            //}
            //Thread.Sleep(WeeWait);
            //for (int i = 0; i < arList.Length; ++i) {
            //    Assert.IsFalse(arList[i].IsCompleted, "before: IsCompleted [" + i + "]");
            //}
            AssertIsOpen(conn, true, "before");
            conn.Close(); // ************
            // TODO This should be 'true' -- till an I/O op fails: AssertIsOpen(conn, true, "after");
            AssertIsOpen(conn, false, "after");
            //Thread.Sleep(WeeWait);
            //for (int i = 0; i < arList.Length; ++i) {
            //    Assert.IsTrue(arList[i].IsCompleted, "after: IsCompleted [" + i + "]");
            //}
            //for (int i = 0; i < arList.Length; ++i) {
            //    action.EndInvoke(arList[i]);
            //}
            //
            var arAfter = action.BeginInvoke(null, null, null);

            TestsApmUtils.SafeNoHangWaitShort(arAfter, "after after");
            Assert.IsTrue(arAfter.IsCompleted, "after after: IsCompleted");
            try {
                action.EndInvoke(arAfter);
                Assert.Fail("should have thrown!");
            } catch (ObjectDisposedException) {
                //} catch (IOException ioex) {
                //    Exception ex = ioex.InnerException;
                //    Assert.IsInstanceOfType(typeof(ObjectDisposedException), ex);
            } catch (Exception ex) {
                Assert.Fail("Bad exception from Write: " + ex);
            }
            AssertIsOpen(conn, false, "bottom");
            // NetworkStream doesn't use Flush, so it doesn't throw on connection
            // close, thus we need to duplicate that. :-(
            conn.Flush();
            //
            conn.Close();
            conn.Close();
            conn.Close();
        }
        public void OneFailedIncomingConnection()
        {
            TestWcLsnrBluetoothFactory f      = new TestWcLsnrBluetoothFactory();
            TestLsnrRfCommIf           commIf = new TestLsnrRfCommIf();

            f.queueIRfCommIf.Enqueue(commIf);
            TestLsnrRfcommPort port0 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port0);
            BluetoothFactory.SetFactory(f);
            //
            port0.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            BluetoothListener lsnr = new BluetoothListener(BluetoothService.VideoSink);

            lsnr.ServiceName = "weeee";
            lsnr.Start();
            IAsyncResult       ar    = lsnr.BeginAcceptBluetoothClient(null, null);
            TestLsnrRfcommPort port1 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port1);
            port1.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS); // now begun immediately
            port0.NewEvent(PORT_EV.CONNECT_ERR);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            port0.AssertOpenServerCalledAndClear(29);
            try {
                try {
                    BluetoothClient cli = lsnr.EndAcceptBluetoothClient(ar);
                } catch (System.IO.IOException ioexShouldNotWrapSEx) { //HACK ioexShouldNotWrapSEx
                    throw ioexShouldNotWrapSEx.InnerException;
                }
                Assert.Fail("should have thrown!");
            } catch (SocketException) {
            }
            TestSdpService2 sdpSvc = f.GetTestSdpService();

            Assert.AreEqual(0, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            lsnr.Stop();
            Assert.AreEqual(1, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            //
            Assert.AreEqual(0, f.queueIRfCommPort.Count, "Used both ports");
            port1.AssertCloseCalledOnce("second acceptor closed");
            //port0.AssertCloseCalledOnce("first failed connection now closed");
            //
            BluetoothEndPoint lep = lsnr.LocalEndPoint;

            sdpSvc.AssertCalls(
                "AddServiceClassIdList: 00001304-0000-1000-8000-00805f9b34fb" + NewLine
                + "AddRFCommProtocolDescriptor: " + lep.Port + NewLine
                + "AddServiceName: weeee" + NewLine
                );
        }
Example #9
0
        public void AcceptBeforeStart_Async()
        {
            var lsnr = SetupListener();
            var ar   = lsnr.BeginAcceptBluetoothClient(null, null);

            TestsApmUtils.SafeNoHangWaitShort(ar, "EndAccept");
            try {
                lsnr.EndAcceptBluetoothClient(ar);
                Assert.Fail("should have thrown!");
            } catch (InvalidOperationException ex) {
                Assert.AreEqual(typeof(InvalidOperationException), ex.GetType());
            }
        }
Example #10
0
        public void NetworkClose_Reads()
        {
            BsTestNullApi api;
            var           conn = CreateConnectedStream(out api);

            AssertIsOpen(conn, true, "top");
            int readLen;
            var buf    = new byte[10];
            var action = (Converter <Version, int>) delegate {
                int readLenInner = conn.Read(buf, 0, buf.Length);
                return(readLenInner);
            };
            var arList = new IAsyncResult[3];

            for (int i = 0; i < arList.Length; ++i)
            {
                arList[i] = action.BeginInvoke(null, null, null);
            }
            Thread.Sleep(WeeWait);
            for (int i = 0; i < arList.Length; ++i)
            {
                Assert.IsFalse(arList[i].IsCompleted, "before: IsCompleted [" + i + "]");
            }
            AssertIsOpen(conn, true, "before");
            RaiseNetworkClose(conn, api); // ************
            // TODO This should be 'true' -- till an I/O op fails: AssertIsOpen(conn, true, "after");
            AssertIsOpen(conn, false, "after");
            for (int i = 0; i < arList.Length; ++i)
            {
                TestsApmUtils.SafeNoHangWaitShort(arList[i], "after: completed [" + i + "]");
                Assert.IsTrue(arList[i].IsCompleted, "after: IsCompleted [" + i + "]");
            }
            for (int i = 0; i < arList.Length; ++i)
            {
                readLen = action.EndInvoke(arList[i]);
                Assert.AreEqual(0, readLen, "after: readLen [" + i + "]");
            }
            AssertIsOpen(conn, false, "bottom");
            //
            var arAfter = action.BeginInvoke(null, null, null);

            TestsApmUtils.SafeNoHangWaitShort(arAfter, "after after");
            Assert.IsTrue(arAfter.IsCompleted, "after after: IsCompleted");
            readLen = action.EndInvoke(arAfter);
            Assert.AreEqual(0, readLen, "after after result: readLen");
            AssertIsOpen(conn, false, "bottom-bottom");
            //
            conn.Close();
            conn.Close();
            conn.Close();
        }
Example #11
0
        public void OneConnectionTwoAccepts()
        {
            IAsyncResult ar;
            var          lsnr = SetupListenerWithPorts(
                NullBtListener.LsnrSetting.ConnectsImmediately,
                NullBtListener.LsnrSetting.None).Lsnr;

            lsnr.Start();
            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            TestsApmUtils.SafeNoHangWaitShort(ar, "EndAccept-1");
            lsnr.EndAcceptBluetoothClient(ar);
            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            TestsApmUtils.SafeNotCompletesShort(ar, "EndAccept-2");
        }
Example #12
0
        public void AcceptOverStop_Async()
        {
            var lsnr = SetupListener();

            lsnr.Start();
            var ar = lsnr.BeginAcceptBluetoothClient(null, null);

            lsnr.Stop();
            TestsApmUtils.SafeNoHangWaitShort(ar, "EndAccept");
            try {
                lsnr.EndAcceptBluetoothClient(ar);
                Assert.Fail("should have thrown!");
            } catch (ObjectDisposedException ex) {
                Assert.AreEqual(typeof(ObjectDisposedException), ex.GetType());
            }
        }
        public void OneConnection_PeerImmediatelyCloses()
        {
            TestWcLsnrBluetoothFactory f      = new TestWcLsnrBluetoothFactory();
            TestLsnrRfCommIf           commIf = new TestLsnrRfCommIf();

            f.queueIRfCommIf.Enqueue(commIf);
            TestLsnrRfcommPort port0 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port0);
            BluetoothFactory.SetFactory(f);
            //
            port0.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            BluetoothListener lsnr = new BluetoothListener(BluetoothService.VideoSource);

            lsnr.Start();
            IAsyncResult ar = lsnr.BeginAcceptBluetoothClient(null, null);

            port0.AssertOpenServerCalledAndClear(29);//adter Start?
            TestLsnrRfcommPort port1 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port1);
            port1.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS); // now begun immediately
            FireOpenReceiveCloseEvents firer = new FireOpenReceiveCloseEvents(port0);

            firer.Run(); //port0.NewEvent(PORT_EV.CONNECTED);
            //Assert.IsFalse(ar.IsCompleted, "Connect 1 completed"); // 100ms later...
            firer.Complete();
            port0.AssertCloseCalledOnce("first accepted connection now closed");
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            BluetoothClient cli = lsnr.EndAcceptBluetoothClient(ar);

            lsnr.Stop();
            //
            //TODO ! Assert.IsTrue(cli.Connected, "cli.Connected");
            Assert.AreEqual(0, f.queueIRfCommPort.Count, "Used both ports");
            port1.AssertCloseCalledOnce("second acceptor closed");
            //
            Stream peer = cli.GetStream();

            byte[] buf     = new byte[10];
            int    readLen = TestsApmUtils.SafeNoHangRead(peer, buf, 0, buf.Length);

            Assert.AreEqual(1, readLen, "readLen");
            cli.Close();
            port0.AssertCloseCalledAtLeastOnce("first accepted connection now closed");
        }
        public void OneConnection()
        {
            TestWcLsnrBluetoothFactory f      = new TestWcLsnrBluetoothFactory();
            TestLsnrRfCommIf           commIf = new TestLsnrRfCommIf();

            f.queueIRfCommIf.Enqueue(commIf);
            TestLsnrRfcommPort port0 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port0);
            BluetoothFactory.SetFactory(f);
            TestLsnrRfcommPort port1 = AddSomeCreatablePorts(f);

            //
            port0.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            BluetoothListener lsnr = new BluetoothListener(BluetoothService.VideoSource);

            lsnr.Start();
            IAsyncResult ar = lsnr.BeginAcceptBluetoothClient(null, null);

            port0.AssertOpenServerCalledAndClear(29);//adter Start?
            port0.NewEvent(PORT_EV.CONNECTED);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            BluetoothClient cli    = lsnr.EndAcceptBluetoothClient(ar);
            TestSdpService2 sdpSvc = f.GetTestSdpService();

            Assert.AreEqual(0, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            lsnr.Stop();
            Assert.AreEqual(1, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            //
            Assert.IsTrue(cli.Connected, "cli.Connected");
            Assert.AreEqual(0, f.queueIRfCommPort.Count, "Used both ports");
            port1.AssertCloseCalledOnce("second acceptor closed");
            cli.Close();
            port0.AssertCloseCalledOnce("first accepted connection now closed");
            //
            BluetoothEndPoint lep = lsnr.LocalEndPoint;

            sdpSvc.AssertCalls(
                "AddServiceClassIdList: 00001303-0000-1000-8000-00805f9b34fb" + NewLine
                + "AddRFCommProtocolDescriptor: " + lep.Port + NewLine
                );
        }
Example #15
0
        private static void ConnectBluetoothClient(TestRfcommPort port, BluetoothClient cli)
        {
            BluetoothEndPoint bep = new BluetoothEndPoint(BluetoothAddress.Parse("00:1F:2E:3D:4C:5B"),
                                                          InTheHand.Net.Bluetooth.BluetoothService.Empty, 5);

            byte[]    AddressInWidcomm = CanonicalOrderBytes(bep.Address);
            const int ChannelNumber    = 5;
            //
            IAsyncResult ar;

            //
            // Success
            port.SetOpenClientResult(PORT_RETURN_CODE.SUCCESS);
            ar = cli.BeginConnect(bep, null, null);
            port.AssertOpenClientCalledAndClear(AddressInWidcomm, ChannelNumber);
            port.NewEvent(PORT_EV.CONNECTED);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Connect 1");
            Assert.IsTrue(ar.IsCompleted, "Connect 1 completed");
            cli.EndConnect(ar);
        }
Example #16
0
        public void SecondPortCreateFails_BeginAcceptBeforeFirstConn()
        {
            var lsnr = SetupListenerWithPorts(
                NullBtListener.LsnrSetting.ConnectsImmediately,
                NullBtListener.LsnrSetting.ErrorOnOpenServer).Lsnr;

            lsnr.Start();
            //
            var ar1 = lsnr.BeginAcceptBluetoothClient(null, null);
            var ar2 = lsnr.BeginAcceptBluetoothClient(null, null);

            TestsApmUtils.SafeNoHangWaitShort(ar1, "EndAccept-good");
            lsnr.EndAcceptBluetoothClient(ar1);
            //
            TestsApmUtils.SafeNoHangWaitShort(ar2, "EndAccept-Error");
            try {
                lsnr.EndAcceptBluetoothClient(ar2);
                Assert.Fail("should have thrown!");
            } catch (Exception) {
            }
        }
        public void StartInquiryFails_ThenTestOne()
        {
            TestInquiryBtIf btIf;
            BluetoothClient cli;

            //
            Create_BluetoothClient(out btIf, out cli);
            Assert.IsFalse(btIf.testDoCallbacksFromWithinStartInquiry, "tdcfwsi");
            //
            btIf.startInquiryFails = true;
            try {
                BluetoothDeviceInfo[] devicesX = cli.DiscoverDevices();
                Assert.Fail("should have thrown!");
            } catch (System.Net.Sockets.SocketException) {
            }
            bool signalled1 = btIf.startInquiryCalled.WaitOne(0, false);

            Assert.IsTrue(signalled1, "!signalled_a!!!!!");
            //
            btIf.startInquiryCalled.Reset();
            btIf.startInquiryFails = false;
            //
            ThreadStart dlgt = delegate {
                bool signalled = btIf.startInquiryCalled.WaitOne(10000, false);
                Assert.IsTrue(signalled, "!signalled!!!!!");
                btIf.InquiryEventsOne();
            };
            IAsyncResult arDlgt = Delegate2.BeginInvoke(dlgt, null, null);

            //
            WidcommBluetoothClient.ReadKnownDeviceFromTheRegistry = false;
            IAsyncResult arDD = cli.BeginDiscoverDevices(255, true, true, true, true, null, null);

            TestsApmUtils.SafeNoHangWait(arDD, "DiscoverDevices");
            BluetoothDeviceInfo[] devices = cli.EndDiscoverDevices(arDD);
            Delegate2.EndInvoke(dlgt, arDlgt); // any exceptions?
            VerifyDevicesOne(devices);
            Assert.AreEqual(1, btIf.stopInquiryCalled, "stopInquiryCalled");
        }
Example #18
0
        public void LocalClose_Reads()
        {
            BsTestNullApi api;
            var           conn = CreateConnectedStream(out api);

            AssertIsOpen(conn, true, "top");
            int readLen;
            var buf    = new byte[10];
            var action = (Converter <Version, int>) delegate {
                int readLenInner = conn.Read(buf, 0, buf.Length);
                return(readLenInner);
            };
            var arList = new IAsyncResult[3];

            for (int i = 0; i < arList.Length; ++i)
            {
                arList[i] = action.BeginInvoke(null, null, null);
            }
            Thread.Sleep(WeeWait);
            for (int i = 0; i < arList.Length; ++i)
            {
                Assert.IsFalse(arList[i].IsCompleted, "before: IsCompleted [" + i + "]");
            }
            AssertIsOpen(conn, true, "before");
            conn.Close(); // ************
            // TODO This should be 'true' -- till an I/O op fails: AssertIsOpen(conn, true, "after");
            AssertIsOpen(conn, false, "after");
            for (int i = 0; i < arList.Length; ++i)
            {
                TestsApmUtils.SafeNoHangWaitShort(arList[i], "after: completed [" + i + "]");
                Assert.IsTrue(arList[i].IsCompleted, "after: IsCompleted [" + i + "]");
            }
            for (int i = 0; i < arList.Length; ++i)
            {
                try {
                    readLen = action.EndInvoke(arList[i]);
                    Assert.Fail("should have thrown");
                } catch (IOException ioex) {
                    Exception ex = ioex.InnerException;
                    Assert.IsInstanceOfType(typeof(SocketException), ex, "[" + i + "] InInstance");
                    var soex = (SocketException)ex;
                    Assert.AreEqual(SocketError.Interrupted, soex.SocketErrorCode, "[" + i + "] SocketErrorCode");
                } catch (ObjectDisposedException) {
                    Assert.Greater(i, 0, "[" + i + "] For first pending Read the error must be IOEx(SocketEx((WSAEINTR))).");
                } catch (Exception ex) {
                    Assert.Fail("[" + i + "] Bad exception from Read: " + ex);
                }
            }
            AssertIsOpen(conn, false, "bottom");
            //
            var arAfter = action.BeginInvoke(null, null, null);

            TestsApmUtils.SafeNoHangWaitShort(arAfter, "after after");
            Assert.IsTrue(arAfter.IsCompleted, "after after: IsCompleted");
            try {
                readLen = action.EndInvoke(arAfter);
                Assert.Fail("should have thrown");
            } catch (ObjectDisposedException) {
                //IOException ioex) {
                //Exception ex = ioex.InnerException;
                //Assert.IsInstanceOfType(typeof(ObjectDisposedException), ex);
            } catch (Exception ex) {
                Assert.Fail("Bad exception from Read: " + ex);
            }
            AssertIsOpen(conn, false, "bottom-bottom");
            //
            conn.Close();
            conn.Close();
            conn.Close();
        }
        void MultipleConnection_(bool auth, bool encrypt,
                                 BTM_SEC expectedSecurityLevel)
        {
            TestWcLsnrBluetoothFactory f      = new TestWcLsnrBluetoothFactory();
            TestLsnrRfCommIf           commIf = new TestLsnrRfCommIf();

            f.queueIRfCommIf.Enqueue(commIf);
            TestLsnrRfcommPort port0 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port0);
            port0.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            BluetoothFactory.SetFactory(f);
            //
            BluetoothListener lsnr = new BluetoothListener(BluetoothService.VideoSource);

            if (auth)
            {
                lsnr.Authenticate = true;
            }
            if (encrypt)
            {
                lsnr.Encrypt = true;
            }
            Assert.AreEqual(auth, lsnr.Authenticate, ".Authenticate 1");
            Assert.AreEqual(encrypt, lsnr.Encrypt, ".Encrypt 1");
            lsnr.Start();
            IAsyncResult ar;

            commIf.AssertSetSecurityLevel(expectedSecurityLevel, true);
            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            port0.AssertOpenServerCalledAndClear(29);//adter Start?
            TestLsnrRfcommPort port1 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port1);
            port1.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            port0.NewEvent(PORT_EV.CONNECTED);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            BluetoothClient cli0 = lsnr.EndAcceptBluetoothClient(ar);

            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            port1.AssertOpenServerCalledAndClear(29);//adter Start?
            TestLsnrRfcommPort port2 = new TestLsnrRfcommPort();

            port2.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            f.queueIRfCommPort.Enqueue(port2);
            port1.NewEvent(PORT_EV.CONNECTED);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            BluetoothClient cli1 = lsnr.EndAcceptBluetoothClient(ar);

            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            port2.AssertOpenServerCalledAndClear(29);//adter Start?
            TestLsnrRfcommPort port3 = new TestLsnrRfcommPort();

            port3.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            f.queueIRfCommPort.Enqueue(port3);
            port2.NewEvent(PORT_EV.CONNECTED);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            BluetoothClient cli2 = lsnr.EndAcceptBluetoothClient(ar);

            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            port3.AssertOpenServerCalledAndClear(29);//adter Start?
            TestLsnrRfcommPort port4 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port4);
            port4.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            port3.NewEvent(PORT_EV.CONNECTED);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            BluetoothClient cli3 = lsnr.EndAcceptBluetoothClient(ar);
            //
            TestSdpService2 sdpSvc = f.GetTestSdpService();

            Assert.AreEqual(0, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            lsnr.Stop();
            Assert.AreEqual(1, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            Assert.AreEqual(auth, lsnr.Authenticate, ".Authenticate 2");
            Assert.AreEqual(encrypt, lsnr.Encrypt, ".Encrypt 2");
            //
            Assert.IsTrue(cli0.Connected, "0 cli.Connected");
            Assert.AreEqual(0, f.queueIRfCommPort.Count, "Used both ports");
            port4.AssertCloseCalledOnce("4 acceptor closed");
            cli0.Close();
            port0.AssertCloseCalledOnce("0 accepted connection now closed");
            //
            Assert.IsTrue(cli1.Connected, "1 cli.Connected");
            cli1.Close();
            port1.AssertCloseCalledOnce("1 accepted connection now closed");
            //
            Assert.IsTrue(cli2.Connected, "2 cli.Connected");
            cli2.Close();
            port2.AssertCloseCalledOnce("2 accepted connection now closed");
            //
            Assert.IsTrue(cli3.Connected, "3 cli.Connected");
            cli3.Close();
            port3.AssertCloseCalledOnce("3 accepted connection now closed");
        }