public void close_external_Stream()
        {
            var serializer = new StringSerializer();
            var slice      = new BufferSlice(new byte[65535], 0, 65535);
            var msg        = new MemoryStream();
            var text       = Encoding.ASCII.GetBytes("Hello world");

            msg.Write(text, 0, text.Length);
            msg.Position = 0;
            var buffer = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);

            sut.Prepare(msg);
            sut.Send(buffer);
            sut.Clear();

            var field = sut.GetType().GetField("_bodyStream", BindingFlags.Instance | BindingFlags.NonPublic);

            if (field == null)
            {
                throw new ArgumentNullException("field");
            }
            var value = ((Stream)field.GetValue(sut));

            value.Should().BeNull();
        }
Ejemplo n.º 2
0
        public void codec_a_stream()
        {
            object receivedMessage = null;
            var    encoderSlice    = new BufferSlice(new byte[65535], 0, 65535);
            var    serializer      = new DataContractMessageSerializer();
            var    encoder         = new MicroMessageEncoder(serializer, encoderSlice);
            var    decoder         = new MicroMessageDecoder(serializer)
            {
                MessageReceived = o => receivedMessage = o
            };
            var expected = "Hello world";
            var stream   = new MemoryStream(Encoding.ASCII.GetBytes(expected));

            stream.SetLength(expected.Length);
            var encoderArgs = new SocketBufferFake();

            encoder.Prepare(stream);
            encoder.Send(encoderArgs);
            encoderArgs.BytesTransferred = encoderArgs.Count;
            decoder.ProcessReadBytes(encoderArgs);

            var reader = new StreamReader((Stream)receivedMessage);
            var msg    = reader.ReadToEnd();

            msg.Should().Be(expected);
        }
        public void partial_send__continue_sending_rest_of_the_buffer_before_doing_anything_else()
        {
            var serializer = new StringSerializer();
            var slice      = new BufferSlice(new byte[65535], 0, 65535);
            var msg        = "Hello world";
            var buffer     = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);

            sut.Prepare(msg);
            sut.Send(buffer);
            sut.OnSendCompleted(10);
            sut.Send(buffer);

            buffer.Offset.Should().Be(10);
            buffer.Count.Should().Be(2 + MicroMessageEncoder.FixedHeaderLength + "string".Length + msg.Length - 10, "2 for the header length");
        }
        public void too_small_buffer_requires_multiple_sends()
        {
            var serializer = new StringSerializer();
            var slice      = new BufferSlice(new byte[520], 0, 520);
            var msg        = "Hello world".PadRight(520);
            var buffer     = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);

            sut.Prepare(msg);
            sut.Send(buffer);
            sut.OnSendCompleted(520).Should().BeFalse();
            sut.Send(buffer);

            buffer.Offset.Should().Be(0);
            // headerlength + fixed header length + content type length + content type - sent in first batch
            buffer.Count.Should().Be(2 + MicroMessageEncoder.FixedHeaderLength + "string".Length + msg.Length - 520);
        }
        public void reset_after_each_successful_message()
        {
            var serializer = new StringSerializer();
            var slice      = new BufferSlice(new byte[520], 0, 520);
            var msg        = "Hello world";
            var buffer     = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);

            sut.Prepare(msg);
            sut.Send(buffer);
            sut.OnSendCompleted(13);
            sut.Send(buffer);
            sut.OnSendCompleted(2 + MicroMessageEncoder.FixedHeaderLength + "string".Length + msg.Length - 13);
            sut.Prepare(msg);
            sut.Send(buffer);
            sut.OnSendCompleted(5);
            sut.Send(buffer);


            buffer.Offset.Should().Be(5);
            buffer.Count.Should().Be(2 + MicroMessageEncoder.FixedHeaderLength + "string".Length + msg.Length - 5);
        }
        public void dont_close_our_internal_stream()
        {
            var serializer = new StringSerializer();
            var slice      = new BufferSlice(new byte[65535], 0, 65535);
            var msg        = "Hello world";
            var buffer     = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);

            sut.Prepare(msg);
            sut.Send(buffer);
            sut.Clear();

            var field = sut.GetType().GetField("_bodyStream", BindingFlags.Instance | BindingFlags.NonPublic);

            ((Stream)field.GetValue(sut)).CanWrite.Should().BeTrue();
        }
Ejemplo n.º 7
0
        public void codec_a_string_message()
        {
            object receivedMessage = null;
            var    encoderSlice    = new BufferSlice(new byte[65535], 0, 65535);
            var    serializer      = new DataContractMessageSerializer();
            var    encoder         = new MicroMessageEncoder(serializer, encoderSlice);
            var    decoder         = new MicroMessageDecoder(serializer)
            {
                MessageReceived = o => receivedMessage = o
            };
            var expected    = "Hello world";
            var encoderArgs = new SocketBufferFake();

            encoder.Prepare(expected);
            encoder.Send(encoderArgs);
            encoderArgs.BytesTransferred = encoderArgs.Count;
            decoder.ProcessReadBytes(encoderArgs);

            receivedMessage.Should().Be(expected);
        }
Ejemplo n.º 8
0
        public void codec_a_custom_type()
        {
            object receivedMessage = null;
            var    encoderSlice    = new BufferSlice(new byte[65535], 0, 65535);
            var    serializer      = new DataContractMessageSerializer();
            var    encoder         = new MicroMessageEncoder(serializer, encoderSlice);
            var    decoder         = new MicroMessageDecoder(serializer)
            {
                MessageReceived = o => receivedMessage = o
            };
            var expected = new CustomType {
                Name = "Arne"
            };
            var encoderArgs = new SocketBufferFake();

            encoder.Prepare(expected);
            encoder.Send(encoderArgs);
            encoderArgs.BytesTransferred = encoderArgs.Count;
            decoder.ProcessReadBytes(encoderArgs);

            receivedMessage.Should().BeOfType <CustomType>();
            receivedMessage.As <CustomType>().Name.Should().Be(expected.Name);
        }
        public void write_a_complete_string_message_directly()
        {
            var serializer = new StringSerializer();
            var slice      = new BufferSlice(new byte[65535], 0, 65535);
            var msg        = "Hello world";
            var buffer     = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);

            sut.Prepare(msg);
            sut.Send(buffer);

            var headerLen = MicroMessageEncoder.FixedHeaderLength + "string".Length;

            buffer.Buffer[0].Should().Be((byte)headerLen);
            buffer.Buffer[2].Should().Be(1, "first version");
            BitConverter.ToInt32(buffer.Buffer, 3).Should().Be(msg.Length);
            buffer.Buffer[7].Should().Be((byte)"string".Length);
            Encoding.ASCII.GetString(buffer.Buffer, 8, "string".Length).Should().Be("string");
            Encoding.ASCII.GetString(buffer.Buffer, MicroMessageEncoder.FixedHeaderLength + "string".Length + 2, msg.Length)
            .Should()
            .Be(msg);
        }
        public void write_a_complete_string_message_directly()
        {
            var contentType = "text/plain;type=System.String";
            var serializer  = new StringSerializer();
            var slice       = new BufferSlice(new byte[65535], 0, 65535);
            var msg         = "Hello world";
            var buffer      = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);

            sut.Prepare(msg);
            sut.Send(buffer);

            var headerLen = MicroMessageEncoder.FixedHeaderLength + contentType.Length;

            BitConverter.ToInt16(buffer.Buffer, 0).Should().Be((short)headerLen);
            buffer.Buffer[2].Should().Be(1, "first version");
            BitConverter.ToInt32(buffer.Buffer, 3).Should().Be(msg.Length);
            buffer.Buffer[7].Should().Be((byte)contentType.Length);
            Encoding.ASCII.GetString(buffer.Buffer, 8, contentType.Length).Should().Be(contentType);
            Encoding.ASCII.GetString(buffer.Buffer, 2 + MicroMessageEncoder.FixedHeaderLength + contentType.Length, msg.Length)
            .Should()
            .Be(msg);
        }
Ejemplo n.º 11
0
        public void codec_a_byte_buffer()
        {
            object receivedMessage = null;
            var    encoderSlice    = new BufferSlice(new byte[65535], 0, 65535);
            var    serializer      = new DataContractMessageSerializer();
            var    encoder         = new MicroMessageEncoder(serializer, encoderSlice);
            var    decoder         = new MicroMessageDecoder(serializer)
            {
                MessageReceived = o => receivedMessage = o
            };
            var expected    = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
            var encoderArgs = new SocketBufferFake();

            encoder.Prepare(expected);
            encoder.Send(encoderArgs);
            encoderArgs.BytesTransferred = encoderArgs.Count;
            decoder.ProcessReadBytes(encoderArgs);

            var ms  = (MemoryStream)receivedMessage;
            var buf = ms.GetBuffer();

            expected.Should().BeSubsetOf(buf);
            ms.Length.Should().Be(expected.Length);
        }