public void the_10kb_payload() { var bufMgr = new BufferManager(65535, 1); var context = new WriterContext(bufMgr); var buf = bufMgr.Dequeue(); bufMgr.Enqueue(buf); var sut = new MessageFrame(); sut.SequenceNumber = 22; var payload = Encoding.ASCII.GetBytes("Hello world".PadRight(10000)); sut.PayloadBuffer = new ArraySegment <byte>(payload, 0, payload.Length); sut.Write(context); var pos = 0; var len = context.GetPackets().First().Count; var frame2 = new MessageFrame(); frame2.Read(buf.Array, ref pos, ref len); frame2.SequenceNumber.Should().Be(22); frame2.IsFlaggedAsSmall.Should().BeFalse(); Encoding.ASCII.GetString(sut.PayloadBuffer.Array, 0, sut.PayloadBuffer.Count).TrimEnd(' ').Should().Be("Hello world"); }
public void code_decode_with_properties_test() { var bufMgr = new BufferManager(65535, 1); var context = new WriterContext(bufMgr); var buf = bufMgr.Dequeue(); bufMgr.Enqueue(buf); var sut = new MessageFrame(); sut.SequenceNumber = 22; sut.Properties.Add("hello", "world"); var payload = Encoding.UTF8.GetBytes("Hello world"); sut.PayloadBuffer = new ArraySegment <byte>(payload, 0, payload.Length); sut.Write(context); var pos = 0; var len = context.GetPackets().First().Count; var frame2 = new MessageFrame(); frame2.Read(buf.Array, ref pos, ref len); frame2.SequenceNumber.Should().Be(22); Encoding.ASCII.GetString(sut.PayloadBuffer.Array, 0, sut.PayloadBuffer.Count).Should().Be("Hello world"); sut.Properties["hello"].Should().Be("world"); }
public void frame_with_extension_properties_can_be_codec() { var bufferManager = new BufferManager(8192, 1); var context = new WriterContext(bufferManager); var buffer = bufferManager.Dequeue(); bufferManager.Enqueue(buffer); var sut = new HandshakeFrame(); sut.Identity = "Client"; sut.OptionalExtensions = new[] { new HandshakeExtension("json", new Dictionary <string, string> { { "Key", "Value" } }), new HandshakeExtension("ack") }; sut.RequiredExtensions = new[] { new HandshakeExtension("dotnet") }; sut.VersionMajor = 1; sut.Write(context); sut.ResetWrite(context); var offset = buffer.Offset; var receiveFrame = new HandshakeFrame(); var len = context.GetPackets()[0].Count; receiveFrame.Read(buffer.Array, ref offset, ref len); receiveFrame.OptionalExtensions[0].Properties["Key"].Should().Be("Value"); }
public void doing_a_partial_payload_write_properly() { var bufMgr = new BufferManager(5010, 1); var context = new WriterContext(bufMgr); var buf1 = bufMgr.Dequeue(); bufMgr.Enqueue(buf1); var sut = new MessageFrame(); sut.SequenceNumber = 22; var payload = Encoding.ASCII.GetBytes("Hello world".PadRight(10000)); sut.PayloadBuffer = new ArraySegment <byte>(payload, 0, payload.Length); var actual1 = sut.Write(context); var pos = 0; var len = context.GetPackets().First().Count; var frame2 = new MessageFrame(); frame2.Read(buf1.Array, ref pos, ref len); var actual2 = sut.Write(context); pos = 0; len = context.GetPackets().Last().Count; frame2.Read(buf1.Array, ref pos, ref len); frame2.SequenceNumber.Should().Be(22); frame2.IsFlaggedAsSmall.Should().BeFalse(); Encoding.ASCII.GetString(sut.PayloadBuffer.Array, 0, sut.PayloadBuffer.Count).TrimEnd(' ').Should().Be("Hello world"); actual1.Should().BeFalse(); actual2.Should().BeTrue(); }
public void frame_Should_be_able_to_parse_multiple_incoming_buffers_if_reseted_in_between() { var bufferManager = new BufferManager(8192, 1); var context = new WriterContext(bufferManager); var buffer = bufferManager.Dequeue(); bufferManager.Enqueue(buffer); var sut = new HandshakeFrame(); sut.Identity = "Client"; sut.OptionalExtensions = new[] { new HandshakeExtension("json"), new HandshakeExtension("ack") }; sut.RequiredExtensions = new[] { new HandshakeExtension("dotnet") }; sut.VersionMajor = 1; sut.Write(context); sut.ResetWrite(context); var offset = buffer.Offset; var len = context.GetPackets()[0].Count; sut.Read(buffer.Array, ref offset, ref len); sut.ResetRead(); offset = buffer.Offset; len = context.GetPackets()[0].Count; sut.Read(buffer.Array, ref offset, ref len); len.Should().Be(0); }
public void cannot_Return_someone_elses_buffer() { var sut = new BufferManager(10, 2); Action actual = () => sut.Enqueue(new ArraySegment <byte>()); actual.ShouldThrow <ArgumentException>(); }
public void can_return_buffer() { var sut = new BufferManager(10, 2); var buf = sut.Dequeue(); sut.Enqueue(buf); sut.Dequeue(); sut.Dequeue(); }
public void serialize_Simplistic_filter() { var bufferManager = new BufferManager(8192, 1); var context = new WriterContext(bufferManager); var buffer = bufferManager.Dequeue(); bufferManager.Enqueue(buffer); var expected = new byte[] { 0, //flags, 0, 2, // sequence number, 2, //destination length, (byte)'M', (byte)'Q', 0, 16, // filter length (byte)'l', (byte)'a', (byte)'s', (byte)'t', (byte)'_', (byte)'n', (byte)'a', (byte)'m', (byte)'e', //filter part 1 (byte)':', (byte)'k', (byte)'a', (byte)'l', (byte)'l', (byte)'e', (byte)';', //filter part 2 6, //payload length (byte)'m', (byte)'o', (byte)'t', (byte)'h', (byte)'e', (byte)'r' //payload }; var sut = new MessageFrame(); sut.SequenceNumber = 2; sut.Destination = "MQ"; sut.Properties.Add("last_name", "kalle"); var payload = Encoding.ASCII.GetBytes("mother"); sut.PayloadBuffer = new ArraySegment <byte>(payload, 0, payload.Length); var actual = sut.Write(context); for (int i = 0; i < expected.Length; i++) { if (expected[i] != buffer.Array[i]) { throw new InvalidOperationException("Differs at " + i); } } }
public int ReadBuffer(BufferManager<byte> bufferManager, FileStream sourceFileStream) { int bytesRead = 0; var buffer = bufferManager.Allocate(); try { bytesRead = sourceFileStream.Read(buffer.Data, 0, buffer.Length); buffer.UsedLength = bytesRead; } catch { bufferManager.Free(buffer); throw; } bufferManager.Enqueue(buffer); return bytesRead; }