コード例 #1
0
 public async Task CancelInsideAsyncProduceDelayed()
 {
     using var cancellationTokenSource = new CancellationTokenSource();
     var data     = new byte[128 * 1024];
     var producer = StreamProducer.Create(async(output, _) =>
     {
         await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationTokenSource.Token);
     });
     var consumer = StreamConsumer.Create((input, cancellationToken) =>
     {
         Assert.True(false, "consumer should not be started until input");
         return(default);
コード例 #2
0
        public async Task CancelInsideAsyncProduce()
        {
            using var cancellationTokenSource = new CancellationTokenSource();
            var data     = new byte[128 * 1024];
            var producer = StreamProducer.Create(async(output, _) =>
            {
                while (true)
                {
                    await output.WriteAsync(data.AsMemory(), cancellationTokenSource.Token);
                    await Task.Delay(TimeSpan.FromMilliseconds(10), cancellationTokenSource.Token);
                }
            });
            var consumerCancellationTriggered = false;
            var consumer = StreamConsumer.Create(async(input, cancellationToken) =>
            {
                var buffer = new byte[64 * 1024];
                try
                {
                    while (true)
                    {
                        await input.ReadAsync(buffer.AsMemory(), cancellationToken);
                    }
                }
                catch (OperationCanceledException)
                {
                    consumerCancellationTriggered = true;
                    throw;
                }
            });
            var task = Assert.ThrowsAnyAsync <OperationCanceledException>(() => PipeStreamer.StreamAsync(producer, consumer, CancellationToken.None).AsTask());

            cancellationTokenSource.CancelAfter(TimeSpan.FromMilliseconds(50));
            await task;

            Assert.True(consumerCancellationTriggered);
        }
コード例 #3
0
 public ValueTask PerformAsync(Stream input, Stream output, CancellationToken cancellationToken = default)
 => StreamAsync(
     producer: StreamProducer.Create((output, cancellationToken) => _first.PerformAsync(input, output, cancellationToken)),
     consumer: StreamConsumer.Create((input, cancellationToken) => _second.PerformAsync(input, output, cancellationToken)),
     cancellationToken: cancellationToken
     );
コード例 #4
0
 public ValueTask ConsumeAsync(Stream input, CancellationToken cancellationToken = default)
 => StreamAsync(
     producer: StreamProducer.Create((output, cancellationToken) => _transformation.PerformAsync(input, output, cancellationToken)),
     consumer: _consumer,
     cancellationToken: cancellationToken
     );
コード例 #5
0
        public async Task MemoryInline()
        {
            var seed = Encoding.ASCII.GetBytes(@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce elementum nisi vel magna rhoncus, in aliquam ipsum accumsan. Phasellus efficitur lectus quis malesuada aliquet. Proin mattis sagittis magna vitae blandit. Cras vel diam sagittis, fringilla nunc vitae, vehicula mi. Nullam et auctor mi. Proin vel pharetra tortor. Donec posuere elementum risus, et aliquet magna pharetra non. Curabitur volutpat maximus sem at euismod. Fusce porta, lacus vel varius varius, lacus felis faucibus ante, fermentum sollicitudin elit neque rhoncus tortor. Aenean eget turpis consequat, luctus lorem vehicula, ullamcorper erat.");

            await using var prod = StreamProducer.Delay(_ => new ValueTask <IStreamProducer>(StreamProducer.Create(async(stream, ctoken) =>
            {
                await stream.WriteAsync(seed.AsMemory(), ctoken);
            })));
            await using var cons = StreamConsumer.Create(async(stream, ctoken) =>
            {
                await using var buffer = new MemoryStream();
                await stream.CopyToAsync(buffer, 8192, ctoken);
                return(buffer.ToArray());
            });
            var output = await prod.ConsumeAsync(cons);

            Assert.True(seed.SequenceEqual(output));
        }