Ejemplo n.º 1
0
        public void PipingStreamSyncTest()
        {
            var Encoding = new UTF8Encoding(false);
            var Data     = Enumerable.Range(0, 5).Select(v => $"number: {v}").ToArray();

            using var TokenSource = CreateTokenSource(TimeSpan.FromMinutes(1));
            using var Buffers     = DisposableList.Create(Enumerable.Range(0, 5).Select(v => new PipelineStream()));
            using var Piping      = new PipingStream(Buffers);
            var Token = TokenSource.Token;

            using (var writer = new StreamWriter(Piping, Encoding, 1024, true))
            {
                foreach (var Text in Data)
                {
                    Token.ThrowIfCancellationRequested();
                    writer.WriteLine(Text);
                    Trace.WriteLine($"write: {Text}");
                }
            }
            foreach (var(os, index) in Buffers.Select((o, i) => (o, i)))
            {
                using var reader = new StreamReader(os, Encoding, false, 1024, true);
                foreach (var ExpectText in Data)
                {
                    Token.ThrowIfCancellationRequested();
                    var Text = reader.ReadLine();
                    Trace.WriteLine($"cache {index} read: {Text}");
                    Assert.AreEqual(ExpectText, Text);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task PipingStreamAsyncSyncTestAsync(int receivers)
        {
            var Encoding = new UTF8Encoding(false);
            var Data     = Enumerable.Range(0, 5).Select(v => $"number: {v}\r\n").ToArray();

            using var TokenSource = CreateTokenSource(TimeSpan.FromMinutes(1));
            using var Buffers     = DisposableList.Create(Enumerable.Range(0, receivers).Select(v => new PipelineStream()));
            using var Piping      = new PipingStream(Buffers);
            var Token = TokenSource.Token;

            foreach (var Text in Data)
            {
                Token.ThrowIfCancellationRequested();
                await Piping.WriteAsync(Encoding.GetBytes(Text).AsMemory(), Token);

                Trace.WriteLine($"write: {Text}");
            }
            var ExpectText = Data.Aggregate("", (v1, v2) => v1 + v2);
            var buffer     = new byte[Encoding.GetByteCount(ExpectText)].AsMemory();

            foreach (var(os, index) in Buffers.Select((v, i) => (v, i)))
            {
                var _buffer = buffer;
                Token.ThrowIfCancellationRequested();
                while (_buffer.Length > 0)
                {
                    var count = await os.ReadAsync(_buffer, Token);

                    _buffer = _buffer.Slice(count);
                }
                var Text = Encoding.GetString(buffer.Span);
                Trace.WriteLine($"cache {index} read: {Text}");
                Assert.AreEqual(ExpectText, Text);
            }
        }
Ejemplo n.º 3
0
        public async Task PipingStreamAsyncTestAsync(int receivers)
        {
            var Encoding = new UTF8Encoding(false);
            var Data     = Enumerable.Range(0, 5).Select(v => $"number: {v}\r\n").ToArray();
            var Time     = TimeSpan.FromMinutes(1);
            var Delay    = Time / (5 * 2);

            using var TokenSource = CreateTokenSource(Time);
            using var Buffers     = DisposableList.Create(Enumerable.Range(0, receivers).Select(v => new PipelineStream()));
            using var Piping      = new PipingStream(Buffers);
            var ExpectText  = Data.Aggregate(string.Empty, (v1, v2) => v1 + v2);
            var bufferCount = Encoding.GetByteCount(ExpectText);
            var Token       = TokenSource.Token;
            var writerTask  = Task.Run(async() =>
            {
                foreach (var Text in Data)
                {
                    Token.ThrowIfCancellationRequested();
                    await Piping.WriteAsync(Encoding.GetBytes(Text).AsMemory(), Token);
                    Trace.WriteLine($"write: '{Text.TrimEnd('\r', '\n')}'");
                    await Task.Delay(Delay, Token);
                }
            });
            var cacheReaderTasks =
                Buffers.Select((os, index) =>
                               Task.Run(async() =>
            {
                var buffer = new byte[bufferCount].AsMemory();
                Token.ThrowIfCancellationRequested();
                var _buffer = buffer;
                while (_buffer.Length > 0)
                {
                    var count = await os.ReadAsync(_buffer, Token);
                    Trace.WriteLine($"{index}: {string.Join(' ', _buffer.Slice(0, count).ToArray().Select(v => $"{v:X2}"))}");
                    _buffer = _buffer.Slice(count);
                }
                var Text = Encoding.GetString(buffer.Span);
                Trace.WriteLine($"cache {index} read: {Text}");
                Assert.AreEqual(ExpectText, Text);
            }));
            await Task.WhenAll(new[] { writerTask }.Concat(cacheReaderTasks));
        }