Пример #1
0
        public async Task Writing()
        {
            var ms    = new MemoryStream();
            var muxer = new Muxer {
                Channel = ms
            };
            var stream = new Substream {
                Muxer = muxer
            };
            var m1 = new byte[1];

            stream.AddData(new byte[] { 10 });
            Assert.IsTrue(stream.CanRead);
            Assert.IsTrue(stream.CanWrite);

            Assert.AreEqual(1, await stream.ReadAsync(m1, 0, 1));
            await stream.WriteAsync(m1, 0, 1);

            stream.WriteByte(11);
            await stream.FlushAsync();

            ms.Position = 0;
            var header = await Header.ReadAsync(ms);

            var length = await Varint.ReadVarint32Async(ms);

            var payload = new byte[length];

            ms.Read(payload, 0, length);
            Assert.AreEqual(stream.Id, header.StreamId);
            Assert.AreEqual(2, payload.Length);
            CollectionAssert.AreEqual(new byte[] { 10, 11 }, payload);
        }
Пример #2
0
        public async Task Reading_ClosedStream()
        {
            var m1     = new byte[10];
            var stream = new Substream();

            stream.NoMoreData();
            Assert.AreEqual(0, await stream.ReadAsync(m1, 0, 10));
        }
Пример #3
0
        public async Task Reading_Partial()
        {
            var m1     = new byte[] { 1, 2, 3, 4 };
            var m2     = new byte[m1.Length];
            var stream = new Substream();

            stream.AddData(m1);
            stream.NoMoreData();

            Assert.AreEqual(4, await stream.ReadAsync(m2, 0, 5));
            CollectionAssert.AreEqual(m1, m2);

            Assert.AreEqual(-1, stream.ReadByte());
            Assert.IsFalse(stream.CanRead);
        }
Пример #4
0
        public async Task Reading()
        {
            var m1     = new byte[] { 1, 2, 3, 4 };
            var m2     = new byte[m1.Length];
            var stream = new Substream();

            stream.AddData(new byte[] { 1, 2 });
            stream.AddData(new byte[] { 3, 4 });
            stream.NoMoreData();
            Assert.IsTrue(stream.CanRead);

            m2[0] = (byte)stream.ReadByte();
            Assert.AreEqual(1, stream.Read(m2, 1, 1));
            Assert.AreEqual(2, await stream.ReadAsync(m2, 2, 2));
            CollectionAssert.AreEqual(m1, m2);

            Assert.AreEqual(-1, stream.ReadByte());
            Assert.IsFalse(stream.CanRead);
        }
Пример #5
0
        public async Task Reading_Delayed_Partial()
        {
            var m1     = new byte[] { 1, 2, 3, 4 };
            var m2     = new byte[m1.Length];
            var stream = new Substream();

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            Task.Run(async() =>
            {
                await Task.Delay(100);
                stream.AddData(new byte[] { 1, 2 });
                await Task.Delay(100);
                stream.AddData(new byte[] { 3, 4 });
                await Task.Delay(100);
                stream.NoMoreData();
            });
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            Assert.AreEqual(4, await stream.ReadAsync(m2, 0, 5));
            CollectionAssert.AreEqual(m1, m2);
        }