public void ShouldFlushBytesCorrectlyWhenMessageIsBiggerThanChunkSize() { var mockClient = new Mock <ITcpSocketClient>(); var mockWriteStream = new Mock <Stream>(); mockClient.Setup(x => x.WriteStream).Returns(mockWriteStream.Object); var mockLogger = new Mock <ILogger>(); var chunker = new ChunkedOutputStream(mockClient.Object, new BigEndianTargetBitConverter(), mockLogger.Object, 8); byte[] bytes = new byte[10]; for (int i = 0; i < bytes.Length; i++) { bytes[i] = (byte)(i + 1); } chunker.Write(bytes); chunker.Flush(); byte[] expected1 = { 0x00, 0x04, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00 }; byte[] expected2 = { 0x00, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00 }; byte[] expected3 = { 0x00, 0x02, 0x09, 0x0A, 0x00, 0x00, 0x00, 0x00 }; mockWriteStream.Verify(x => x.Write(expected1, 0, 6), Times.Once); mockWriteStream.Verify(x => x.Write(expected2, 0, 6), Times.Once); mockWriteStream.Verify(x => x.Write(expected3, 0, 4), Times.Once); mockWriteStream.Verify(x => x.Flush(), Times.Exactly(3)); }
public void ShouldWriteTailCorrectlyWhenNotInChunk() { var mockClient = new Mock<ITcpSocketClient>(); var mockWriteStream = new Mock<Stream>(); mockClient.Setup(x => x.WriteStream).Returns(mockWriteStream.Object); var mockLogger = new Mock<ILogger>(); var chunker = new ChunkedOutputStream(mockClient.Object, mockLogger.Object, 8); chunker.Write(0x01, 0x02, 0x03, 0x04); // only one byte left chunker.Flush(); // somehow we get this flushed chunker.WriteMessageTail(); chunker.Flush(); byte[] expected1 = { 0x00, 0x04, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00 }; byte[] expected2 = new byte[8]; // all 0s mockWriteStream.Verify(x => x.Write(expected1, 0, 6), Times.Once); mockWriteStream.Verify(x => x.Write(expected2, 0, 2), Times.Once); mockWriteStream.Verify(x => x.Flush(), Times.Exactly(2)); }
public void ShouldWriteTailInCurrentBufferWhenTwoBytesLeftInBuffer() { var mockClient = new Mock<ITcpSocketClient>(); var mockWriteStream = new Mock<Stream>(); mockClient.Setup(x => x.WriteStream).Returns(mockWriteStream.Object); var mockLogger = new Mock<ILogger>(); var chunker = new ChunkedOutputStream(mockClient.Object, mockLogger.Object, 8); chunker.Write(0x01, 0x02, 0x03, 0x04); //two bytes left chunker.WriteMessageTail(); chunker.Flush(); byte[] expected1 = { 0x00, 0x04, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00 }; mockWriteStream.Verify(x => x.Write(expected1, 0, 8), Times.Once); mockWriteStream.Verify(x => x.Flush(), Times.Exactly(1)); }
public void ShouldWriteEachByteCorrectlyIfNotInChunkInTheMiddleOfTheBuffer() { var mockClient = new Mock<ITcpSocketClient>(); var mockWriteStream = new Mock<Stream>(); mockClient.Setup(x => x.WriteStream).Returns(mockWriteStream.Object); var mockLogger = new Mock<ILogger>(); var chunker = new ChunkedOutputStream(mockClient.Object, mockLogger.Object, 10); chunker.Write(0x01, 0x02, 0x03); chunker.WriteMessageTail(); // not in chunk chunker.Write(0x0A); chunker.Flush(); byte[] expected1 = { 0x00, 0x03, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x01, 0x0A }; mockWriteStream.Verify(x => x.Write(expected1, 0, 10), Times.Once); mockWriteStream.Verify(x => x.Flush(), Times.Exactly(1)); }
public void ShouldWriteTailInNextBufferWhenNoPlaceLeftInCurrentBuffer() { var mockClient = new Mock<ITcpSocketClient>(); var mockWriteStream = new Mock<Stream>(); mockClient.Setup(x => x.WriteStream).Returns(mockWriteStream.Object); var mockLogger = new Mock<ILogger>(); var chunker = new ChunkedOutputStream(mockClient.Object, mockLogger.Object, 8); chunker.Write(0x01, 0x02, 0x03, 0x04, 0x05, 0x06); //no byte left chunker.WriteMessageTail(); chunker.Flush(); byte[] expected1 = { 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; byte[] expected2 = new byte[8]; // all 0s mockWriteStream.Verify(x => x.Write(expected1, 0, 8), Times.Once); mockWriteStream.Verify(x => x.Write(expected2, 0, 2), Times.Once); mockWriteStream.Verify(x => x.Flush(), Times.Exactly(2)); }
public void Flush() { _outputStream.Flush(); }