Example #1
0
        public async Task MessageBroker_SendMessage_With_NAK_NAK_ACK_Should_Succeed()
        {
            var cancellation = new CancellationTokenSource();

            var stream  = new MockDuplexStream();
            var counter = 0;

            stream.AfterWrite += (s, e) =>
            {
                if (counter < 2)
                {
                    stream.Input.WriteByte((byte)FrameHeader.NAK);
                }
                else
                {
                    stream.Input.WriteByte((byte)FrameHeader.ACK);
                }
                stream.Input.Position--;
                counter++;
            };

            var broker = new MessageBroker(stream);

            broker.Run(cancellation.Token);

            var payload = new Payload(new byte[] { (byte)Function.ApplicationUpdate, 1, 2, 3 });
            await broker.Send(new RequestMessage(payload), cancellation.Token);

            cancellation.Cancel();

            await broker;
        }
Example #2
0
        public async Task MessageBroker_SendMessage_No_ACK_ShouldTimeout()
        {
            var cancellation = new CancellationTokenSource();

            var stream = new MockDuplexStream();
            var broker = new MessageBroker(stream);

            broker.Run(cancellation.Token);

            var payload = new Payload(new byte[] { (byte)Function.ApplicationUpdate, 1, 2, 3 });

            await broker.Send(new RequestMessage(payload), cancellation.Token);

            cancellation.Cancel();

            await broker;
        }
Example #3
0
        public async Task WriteReadDataFrame()
        {
            var input  = new byte[] { 1, 12, 0, 4, 0, 15, 6, 49, 5, 4, 34, 0, 3, 239 };
            var stream = new MockDuplexStream();

            stream.Input.Write(input, 0, input.Length);
            stream.Input.Position = 0;
            var reader   = new FrameReader(stream);
            var response = (DataFrame)(await reader.Read(CancellationToken.None));

            var request = new DataFrame(DataFrameType.REQ, response.Payload);
            var writer  = new FrameWriter(stream);
            await writer.Write(request, CancellationToken.None);

            var output = stream.Output.ToArray();

            Assert.IsTrue(input.SequenceEqual(output));
        }
Example #4
0
        public async Task WriteReadAckFrame()
        {
            var stream = new MockDuplexStream();

            var writer  = new FrameWriter(stream);
            var request = Frame.ACK;
            await writer.Write(request, CancellationToken.None);

            var data = stream.Output.ToArray();
            await stream.Input.WriteAsync(data, 0, data.Length);

            stream.Input.Position = 0;

            var reader   = new FrameReader(stream);
            var response = await reader.Read(CancellationToken.None);

            Assert.AreEqual(request, response);
        }
Example #5
0
        public async Task MessageBroker_SendMessage_With_NAK_Should_Throw_NakResponseException()
        {
            var cancellation = new CancellationTokenSource();

            var stream = new MockDuplexStream();

            stream.AfterWrite += (s, e) =>
            {
                stream.Input.WriteByte((byte)FrameHeader.NAK);
                stream.Input.Position--;
            };

            var broker = new MessageBroker(stream);

            broker.Run(cancellation.Token);

            var payload = new Payload(new byte[] { (byte)Function.ApplicationUpdate, 1, 2, 3 });
            await broker.Send(new RequestMessage(payload), cancellation.Token);

            cancellation.Cancel();

            await broker;
        }