public async Task TestSizeWithSeveralChunks()
        {
            var trafficReader = new TcpDataReader(5);

            var writeTask = trafficReader.NewPackagesAsync(
                new byte[] { 1, 2, 3, 4, 5 },
                new byte[] { 6, 7, 8, 9, 10 });

            var token   = new CancellationTokenSource();
            var result1 = await trafficReader.ReadAsyncAsync(7, token.Token);

            new ReadOnlyMemory <byte>(new byte[] { 1, 2, 3, 4, 5, 6, 7 }).ArrayIsEqualWith(result1.AsArray());
            trafficReader.CommitReadData(result1);

            var result2 = await trafficReader.ReadAsyncAsync(3, token.Token);

            new ReadOnlyMemory <byte>(new byte[] { 8, 9, 10 }).ArrayIsEqualWith(result2.AsArray());
            trafficReader.CommitReadData(result2);

            await writeTask;
        }
        public async Task TestSearchWithSeveralChunks()
        {
            var trafficReader = new TcpDataReader(5);

            var writeTask = trafficReader.NewPackagesAsync(
                new byte[] { 1, 2, 3, 4, 5 },
                new byte[] { 6, 7, 8, 9, 10 }
                );

            var token   = new CancellationTokenSource();
            var result1 = await trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 5, 6 }, token.Token);

            new ReadOnlyMemory <byte>(new byte[] { 1, 2, 3, 4, 5, 6 }).ArrayIsEqualWith(result1.CommittedMemory);
            trafficReader.CommitReadData(result1);

            var result2 = await trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 9, 10 }, token.Token);

            new ReadOnlyMemory <byte>(new byte[] { 7, 8, 9, 10 }).ArrayIsEqualWith(result2.UncommittedMemory);
            trafficReader.CommitReadData(result1);

            await writeTask;
        }
        public async Task TestOverflowFeature()
        {
            var trafficReader = new TcpDataReader(1024);

            var incomingArray1 = new byte[] { 1, 2, 3, 4, 5, 6 };
            var incomingArray2 = new byte[] { 11, 22, 33, 44, 55, 66 };

            var writeTask = trafficReader.NewPackagesAsync(incomingArray1, incomingArray2);

            var token = new CancellationTokenSource();
            var data  = await trafficReader.ReadAsyncAsync(3, token.Token);

            new ReadOnlyMemory <byte>(new byte[] { 1, 2, 3 }).ArrayIsEqualWith(data.AsArray());
            trafficReader.CommitReadData(data);

            data = await trafficReader.ReadAsyncAsync(4, token.Token);

            new ReadOnlyMemory <byte>(new byte[] { 4, 5, 6, 11 }).ArrayIsEqualWith(data.AsArray());
            trafficReader.CommitReadData(data);

            await writeTask;
        }
        public async Task TestFindingTheSequenceFeatureByReadingToArraysAtTheEnd()
        {
            var trafficReader = new TcpDataReader(1024);

            var incomingArray1 = new byte[] { 1, 2, 3, 4, 5 };
            var incomingArray2 = new byte[] { 11, 12, 13, 4, 5 };

            var writingTask = trafficReader.NewPackagesAsync(incomingArray1, incomingArray2);

            var tc   = new CancellationTokenSource();
            var data = await trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 5, 11 }, tc.Token);

            new ReadOnlyMemory <byte>(new byte[] { 1, 2, 3, 4, 5, 11 }).ArraysAreEqual(data.AsArray());
            trafficReader.CommitReadData(data);

            data = await trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 4, 5 }, tc.Token);

            new ReadOnlyMemory <byte>(new byte[] { 12, 13, 4, 5 }).ArraysAreEqual(data.AsArray());
            trafficReader.CommitReadData(data);

            await writingTask;
        }
        public async Task TestFindingTheSequenceRightAway()
        {
            var trafficReader = new TcpDataReader(1024);

            var incomingArray1 = new byte[] { 1, 2, 3, 4, 5 };
            var incomingArray2 = new byte[] { 11, 12, 13, 4, 5 };

            var writeTask = trafficReader.NewPackagesAsync(incomingArray1, incomingArray2);

            var token = new CancellationTokenSource();
            var data  = await trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 4, 5 }, token.Token);

            new ReadOnlyMemory <byte>(new byte[] { 1, 2, 3, 4, 5 }).ArrayIsEqualWith(data.AsArray());
            trafficReader.CommitReadData(data);

            data = await trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 4, 5 }, token.Token);

            new ReadOnlyMemory <byte>(new byte[] { 11, 12, 13, 4, 5 }).ArrayIsEqualWith(data.AsArray());
            trafficReader.CommitReadData(data);

            await writeTask;
        }
        public async Task TestFindingTheSequenceFeatureByReadingToArraysAtTheEndOtherWayAround()
        {
            var trafficReader = new TcpDataReader(1024);

            var token    = new CancellationTokenSource();
            var dataTask = trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 5, 11 }, token.Token);

            var writeTask = trafficReader.NewPackagesAsync(
                new byte[] { 1, 2, 3, 4, 5 },
                new byte[] { 11, 12, 13, 4, 5 });

            var data = await dataTask;

            new ReadOnlyMemory <byte>(new byte[] { 1, 2, 3, 4, 5, 11 }).ArrayIsEqualWith(data.AsArray());
            trafficReader.CommitReadData(data);

            data = await trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 4, 5 }, token.Token);

            new ReadOnlyMemory <byte>(new byte[] { 12, 13, 4, 5 }).ArrayIsEqualWith(data.AsArray());
            trafficReader.CommitReadData(data);

            await writeTask;
        }
        public async Task TestDoubleVeryOverflowFeature()
        {
            var trafficReader = new TcpDataReader(1024);

            var incomingArray1 = new byte[] { 1, 2, 3, 4, 5, 6 };
            var incomingArray2 = new byte[] { 11, 22, 33, 44, 55, 66, };
            var incomingArray3 = new byte[] { 111, 222, 233, 244, 254, 255 };

            var writingTasks = trafficReader.NewPackagesAsync(incomingArray1, incomingArray2, incomingArray3);


            var tc   = new CancellationTokenSource();
            var data = await trafficReader.ReadAsyncAsync(3, tc.Token);

            new ReadOnlyMemory <byte>(new byte[] { 1, 2, 3 }).ArraysAreEqual(data.AsArray());
            trafficReader.CommitReadData(data);

            data = await trafficReader.ReadAsyncAsync(10, tc.Token);

            new ReadOnlyMemory <byte>(new byte[] { 4, 5, 6, 11, 22, 33, 44, 55, 66, 111 }).ArraysAreEqual(data.AsArray());
            trafficReader.CommitReadData(data);

            await writingTasks;
        }
        public async Task TestFindingSequenceWithTwoAsOnePieceAndOtherComplete()
        {
            var trafficReader = new TcpDataReader(1024);

            var token    = new CancellationTokenSource();
            var dataTask = trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 14, 15 }, token.Token);

            var writeTask = trafficReader.NewPackagesAsync(
                new byte[] { 1, 2, 3, 4, 5 },
                new byte[] { 11, 12, 13, 14, 15 },
                new byte[] { 21, 22, 23, 14, 15 });

            var data = await dataTask;

            new ReadOnlyMemory <byte>(new byte[] { 1, 2, 3, 4, 5, 11, 12, 13, 14, 15 }).ArrayIsEqualWith(data.AsArray());
            trafficReader.CommitReadData(data);

            data = await trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 14, 15 }, token.Token);

            new ReadOnlyMemory <byte>(new byte[] { 21, 22, 23, 14, 15 }).ArrayIsEqualWith(data.AsArray());
            trafficReader.CommitReadData(data);

            await writeTask;
        }