Пример #1
0
        public void SocketReader_ReadBytesIntoResponseHandler_ReadsMoreBytes()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendString("This is a test. This is the next sentence.", Encoding.ASCII);

            MockResponseHandler handler = new MockResponseHandler(Encoding.ASCII);

            SocketReader reader = new SocketReader(socket);

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

            // Assert
            TaskAssert.ResultEquals(result, 15);
            Assert.Equal("This is a test.", handler.Response);

            // Act
            result = reader.ReadBytesIntoResponseHandler(27, handler.HandlerMethod, _dontCancel);

            // Assert
            TaskAssert.ResultEquals(result, 27);
            Assert.Equal("This is a test. This is the next sentence.", handler.Response);
        }
Пример #2
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);
        }
Пример #3
0
        public void SocketReader_ReadBytesIntoResponseHandler_WaitsForHandlerToComplete()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendString("This is a test. This sentence isn't read.", Encoding.UTF8);

            MockResponseHandler handler = new MockResponseHandler(Encoding.UTF8);

            handler.Block = true;

            SocketReader reader = new SocketReader(socket);

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

            // Assert
            TaskAssert.NotCompleted(result, "Before handler is unblocked.");

            // Act
            handler.Block = false;

            // Assert
            TaskAssert.ResultEquals(result, 15, "After handler is unblocked.");
            Assert.Equal("This is a test.", handler.Response);
        }
Пример #4
0
        public void SocketReader_ReadBytesIntoResponseHandler_WaitsForData()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            MockResponseHandler handler = new MockResponseHandler(Encoding.Unicode);

            SocketReader reader = new SocketReader(socket);

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

            // Assert
            TaskAssert.NotCompleted(result, "Before any data sent");

            // Act
            socket.SendString("This is ", Encoding.Unicode);

            // Assert
            TaskAssert.NotCompleted(result, "After some data sent");

            // Act
            socket.SendString("a test. And then some.", Encoding.Unicode);

            // Assert
            TaskAssert.ResultEquals(result, 30, "After all data sent");
            Assert.Equal("This is a test.", handler.Response);
        }
Пример #5
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);
        }
Пример #6
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);
        }
Пример #7
0
            private async Task ReadBytesIntoResponse(long bytesToRead)
            {
                ResponseHandler handler = await _setResponseHandlerTask.Task;

                await _socketReader.ReadBytesIntoResponseHandler(bytesToRead, handler, _cancelSource.Token);
            }