Пример #1
0
        private void VerifyTimeout(int readTimeout)
        {
            using (var com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (var com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    IAsyncResult readAsyncResult;

                    var asyncRead           = new AsyncRead(com1);
                    var asyncEndRead        = new Task(asyncRead.EndRead);
                    var asyncCallbackCalled = false;

                    com1.Open();
                    com2.Open();
                    com1.ReadTimeout = readTimeout;

                    readAsyncResult = com1.BaseStream.BeginRead(new byte[8], 0, 8,
                                                                delegate(IAsyncResult ar) { asyncCallbackCalled = true; }, null);
                    asyncRead.ReadAsyncResult = readAsyncResult;

                    Thread.Sleep(100 > com1.ReadTimeout ? 2 * com1.ReadTimeout : 200);
                    // Sleep for 200ms or 2 times the ReadTimeout

                    if (readAsyncResult.IsCompleted)
                    {
                        // Verify the IAsyncResult has not completed
                        Fail("Err_565088aueiud!!!: Expected read to not have completed");
                    }

                    asyncEndRead.Start();
                    TCSupport.WaitForTaskToStart(asyncEndRead);
                    Thread.Sleep(100 < com1.ReadTimeout ? 2 * com1.ReadTimeout : 200);
                    // Sleep for 200ms or 2 times the ReadTimeout

                    if (!asyncEndRead.IsCompleted)
                    {
                        // Verify EndRead is blocking and is still alive
                        Fail("Err_4085858aiehe!!!: Expected read to not have completed");
                    }

                    if (asyncCallbackCalled)
                    {
                        Fail("Err_750551aiuehd!!!: Expected AsyncCallback not to be called");
                    }

                    com2.Write(new byte[8], 0, 8);

                    TCSupport.WaitForTaskCompletion(asyncEndRead);
                    var waitTime = 0;
                    while (!asyncCallbackCalled && waitTime < 5000)
                    {
                        Thread.Sleep(50);
                        waitTime += 50;
                    }

                    Assert.True(asyncCallbackCalled, "Err_21208aheide!!!: Expected AsyncCallback to be called after some data was written to the port");
                }
        }
Пример #2
0
    private bool VerifyTimeout(int readTimeout)
    {
        SerialPort com1     = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort com2     = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
        bool       retValue = true;

        System.IAsyncResult readAsyncResult;

        AsyncRead asyncRead = new AsyncRead(com1);

        System.Threading.Thread asyncEndRead = new System.Threading.Thread(new System.Threading.ThreadStart(asyncRead.EndRead));
        int  waitTime;
        bool asyncCallbackCalled = false;

        com1.Open();
        com2.Open();
        com1.ReadTimeout = readTimeout;

        readAsyncResult           = com1.BaseStream.BeginRead(new byte[8], 0, 8, delegate(IAsyncResult ar) { asyncCallbackCalled = true; }, null);
        asyncRead.ReadAsyncResult = readAsyncResult;

        System.Threading.Thread.Sleep(100 > com1.ReadTimeout ? 2 * com1.ReadTimeout : 200); //Sleep for 200ms or 2 times the ReadTimeout

        if (readAsyncResult.IsCompleted)
        {//Verify the IAsyncResult has not completed
            Console.WriteLine("Err_565088aueiud!!!: Expected read to not have completed");
            retValue = false;
        }

        asyncEndRead.Start();

        waitTime = 0;
        while (asyncEndRead.ThreadState == System.Threading.ThreadState.Unstarted && waitTime < MAX_WAIT_THREAD)
        {//Wait for the thread to start
            System.Threading.Thread.Sleep(50);
            waitTime += 50;
        }

        if (MAX_WAIT_THREAD <= waitTime)
        {
            Console.WriteLine("Err_018158ajied!!!: Expected EndRead to have returned");
            retValue = false;
        }

        System.Threading.Thread.Sleep(100 < com1.ReadTimeout ? 2 * com1.ReadTimeout : 200); //Sleep for 200ms or 2 times the ReadTimeout

        if (!asyncEndRead.IsAlive)
        {//Verify EndRead is blocking and is still alive
            Console.WriteLine("Err_4085858aiehe!!!: Expected read to not have completed");
            retValue = false;
        }

        if (asyncCallbackCalled)
        {
            Console.WriteLine("Err_750551aiuehd!!!: Expected AsyncCallback not to be called");
            retValue = false;
        }

        com2.Write(new byte[8], 0, 8);

        waitTime = 0;
        while (asyncEndRead.IsAlive && waitTime < MAX_WAIT_THREAD)
        {
            System.Threading.Thread.Sleep(50);
            waitTime += 50;
        }

        if (MAX_WAIT_THREAD <= waitTime)
        {
            Console.WriteLine("Err_018158ajied!!!: Expected EndRead to have returned");
            retValue = false;
        }

        waitTime = 0;
        while (!asyncCallbackCalled && waitTime < 5000)
        {
            System.Threading.Thread.Sleep(50);
            waitTime += 50;
        }

        if (!asyncCallbackCalled)
        {
            Console.WriteLine("Err_21208aheide!!!: Expected AsyncCallback to be called after some data was written to the port");
            retValue = false;
        }


        if (com1.IsOpen)
        {
            com1.Close();
        }

        if (com2.IsOpen)
        {
            com2.Close();
        }

        return(retValue);
    }