示例#1
0
        public async Task Array()
        {
            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.");
            var prod   = StreamProducer.FromArray(seed);
            var output = await prod.ToArrayAsync();

            Assert.True(seed.SequenceEqual(output));
        }
示例#2
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);
示例#3
0
        public async Task Memory()
        {
            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.FromMemory(seed.AsMemory())));
            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));
        }
示例#4
0
        public async Task EncryptAndDecryptAlternative()
        {
            var seed   = @"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.";
            var enc    = new EncryptTransformation();
            var dec    = new DecryptTransformation();
            var prod   = StreamProducer.FromString(seed, Encoding.ASCII);
            var cons   = StreamConsumer.ToString(Encoding.ASCII);
            var output = await prod.ConsumeAsync(cons
                                                 .Chain(dec)
                                                 .Chain(enc)
                                                 );

            Assert.Equal(seed, output);
            Assert.True(enc.HasStarted);
            Assert.True(enc.HasCompleted);
            Assert.Null(enc.Error);
            Assert.True(enc.HasBeenDisposed);
            Assert.True(dec.HasStarted);
            Assert.True(dec.HasCompleted);
            Assert.Null(dec.Error);
            Assert.True(dec.HasBeenDisposed);
        }
示例#5
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);
        }
示例#6
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
     );
示例#7
0
 public ValueTask ConsumeAsync(Stream input, CancellationToken cancellationToken = default)
 => StreamAsync(
     producer: StreamProducer.Create((output, cancellationToken) => _transformation.PerformAsync(input, output, cancellationToken)),
     consumer: _consumer,
     cancellationToken: cancellationToken
     );