Пример #1
0
        public void SocketReader_ReadBytesIntoResponseHandler_StopsReadingBytesIfCancelled()
        {
            // Arrange
            string stringToSend = "123456789";

            byte[] bufferToSend = Encoding.ASCII.GetBytes(stringToSend);

            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendBytes(bufferToSend, 0, bufferToSend.Length);

            MockResponseHandler handler = new MockResponseHandler(Encoding.ASCII);

            CancellationTokenSource cts = new CancellationTokenSource();

            SocketReader reader = new SocketReader(socket);
            Task <int>   task   = reader.ReadBytesIntoResponseHandler(bufferToSend.Length + 1, handler.HandlerMethod, cts.Token);

            // Act
            cts.Cancel();

            // Assert
            TaskAssert.ResultEquals(task, bufferToSend.Length);
            Assert.Equal(stringToSend, handler.Response);
        }
Пример #2
0
        public void SocketReader_ReadChar_KeepsWaitingIfNotEnoughDataIsReturned()
        {
            // Arrange
            byte[] sendBuffer = Encoding.UTF32.GetBytes("ΨΣ"); // Greek Psi, Sigma

            MockSocketAdapter socket = new MockSocketAdapter();

            SocketReader reader = new SocketReader(socket);

            reader.SetEncoding(Encoding.UTF32);

            // Act
            Task <char> result = reader.ReadChar(_dontCancel); // Try to read first character

            // Assert
            TaskAssert.NotCompleted(result, "Before sending any bytes");

            // Act
            socket.SendBytes(sendBuffer, 0, 1); // Send 1 byte

            // Assert
            TaskAssert.NotCompleted(result, "After sending 1 byte");

            // Act
            socket.SendBytes(sendBuffer, 1, 2); // Send 2 more bytes

            // Assert
            TaskAssert.NotCompleted(result, "After sending 2 more bytes");

            // Act
            socket.SendBytes(sendBuffer, 3, 2); // Send 2 more bytes

            // Assert
            TaskAssert.ResultEquals(result, 'Ψ', "After sending all bytes of first character.");

            // Act
            result = reader.ReadChar(_dontCancel); // Try to read second character

            // Assert
            TaskAssert.NotCompleted(result, "Before sending remaining bytes");

            // Act
            socket.SendBytes(sendBuffer, 5, 3); // Send remaining 3 bytes

            // Assert
            TaskAssert.ResultEquals(result, 'Σ', "After sending remaining bytes");
        }
Пример #3
0
        public void SocketReader_ReadBytesIntoResponseHandler_ContinuesWaitingForBytes()
        {
            // Arrange
            string stringToSend = "123456789";

            byte[] bufferToSend = Encoding.ASCII.GetBytes(stringToSend);

            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendBytes(bufferToSend, 0, bufferToSend.Length);

            MockResponseHandler handler = new MockResponseHandler(Encoding.ASCII);

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <int> task = reader.ReadBytesIntoResponseHandler(bufferToSend.Length + 1, handler.HandlerMethod, _dontCancel);

            // Assert
            TaskAssert.NotCompleted(task);
        }
Пример #4
0
        public void SocketReader_ReadBytesIntoResponseHandler_ReadMoreBytesThanBuffer()
        {
            // Arrange
            string stringToSend = new String('Σ', count: 20000);

            byte[] bufferToSend = Encoding.UTF32.GetBytes(stringToSend);

            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendBytes(bufferToSend, 0, bufferToSend.Length);

            MockResponseHandler handler = new MockResponseHandler(Encoding.UTF32);

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <int> result = reader.ReadBytesIntoResponseHandler(bufferToSend.Length, handler.HandlerMethod, _dontCancel);

            // Assert
            TaskAssert.ResultEquals(result, bufferToSend.Length);
            Assert.Equal(stringToSend, handler.Response);
        }
Пример #5
0
        public void SocketReader_ReadChar_TestByteByByteWithManyEncodings()
        {
            // Arrange
            Encoding[] encodings = new Encoding[]
            {
                Encoding.UTF8,
                Encoding.Unicode,
                Encoding.BigEndianUnicode,
                Encoding.UTF32
            };

            foreach (Encoding encoding in encodings)
            {
                MockSocketAdapter socket = new MockSocketAdapter();

                SocketReader reader = new SocketReader(socket);
                reader.SetEncoding(encoding);

                byte[]        sendBuffer = encoding.GetBytes("ΨΣ");
                Task <char>[] results    = new Task <char> [2];

                int currentResult = -1;

                for (int i = 0; i < sendBuffer.Length; i++)
                {
                    if (currentResult < 0 || results[currentResult].IsCompleted)
                    {
                        currentResult++;
                        results[currentResult] = reader.ReadChar(_dontCancel);
                    }

                    socket.SendBytes(sendBuffer, i, 1);
                }

                TaskAssert.ResultEquals(results[0], 'Ψ', "result[0] with encoding {0}", encoding.EncodingName);
                TaskAssert.ResultEquals(results[1], 'Σ', "result[1] with encoding {0}", encoding.EncodingName);
            }
        }