public async Task Should_be_able_to_process_two_messages_from_the_same_receive()
        {
            var serializer = new JsonTransportSerializer();
            var encoder    = new SimpleProtocolEncoder(serializer);
            var msg1       = new TransportMessage("Test1");
            var msg2       = new TransportMessage("Test2");
            var sender     = Substitute.For <ISendState>();
            await encoder.EncodeMessage(sender, msg1);

            await encoder.EncodeMessage(sender, msg2);

            var buffer       = new byte[65535];
            var bufferOffset = 0;

            foreach (var call in sender.ReceivedCalls())
            {
                var buf    = call.GetArguments()[0].As <byte[]>();
                var offset = call.GetArguments()[1].As <int>();
                var len    = call.GetArguments()[2].As <int>();
                Buffer.BlockCopy(buf, offset, buffer, bufferOffset, len);
                bufferOffset += len;
            }
            var state = new TestReceiveState();

            state.AddBuffer(buffer, 0, bufferOffset);
            state.WriteOffset = bufferOffset;

            var sut     = new SimpleProtocolDecoder(serializer);
            var actual1 = (await sut.Decode(state)).As <TransportMessage>();
            var actual2 = (await sut.Decode(state)).As <TransportMessage>();

            actual1.Body.Should().Be("Test1");
            actual2.Body.Should().Be("Test2");
        }
        public async Task Should_be_able_to_encode_and_decode()
        {
            var serialiser = new JsonTransportSerializer();
            var sender     = Substitute.For <ISendState>();
            var msg        = new TransportMessage("Hello world");

            var sut = new SimpleProtocolEncoder(serialiser);
            await sut.EncodeMessage(sender, msg);

            var headerCall = sender.ReceivedCalls().First().GetArguments();
            var bodyCall   = sender.ReceivedCalls().Last().GetArguments();

            var receiveState = new TestReceiveState();

            receiveState.AddBuffer(headerCall[0].As <byte[]>(), 0, headerCall[2].As <int>());
            receiveState.AddBuffer(bodyCall[0].As <byte[]>(), 0, bodyCall[2].As <int>());
            var decoder = new SimpleProtocolDecoder(serialiser);
            var actual  = await decoder.Decode(receiveState);

            var actualMessage = actual as TransportMessage;

            actualMessage.Body.Should().Be("Hello world");
        }
        public async Task Should_be_able_to_decode_an_nak()
        {
            var serializer = new JsonTransportSerializer();
            var expectedId = Guid.NewGuid();
            var state      = new TestReceiveState();
            var buf        = new byte[17];

            buf[0] = 3; //nak
            var guidBytes = expectedId.ToByteArray();

            Buffer.BlockCopy(guidBytes, 0, buf, 1, 16);
            state.AddBuffer(buf, 0, buf.Length);
            state.WriteOffset = 17;

            var sut     = new SimpleProtocolDecoder(serializer);
            var message = await sut.Decode(state);

            message.As <Nak>().MessageId.Should().Be(expectedId);
        }