public async Task WritesCanBeCanceledViaProvidedCancellationToken()
 {
     var writeOnlyStream = new WriteOnlyPipeStream(new HangingPipeWriter());
     var pipeWriter      = new StreamPipeWriter(writeOnlyStream);
     var cts             = new CancellationTokenSource(1);
     await Assert.ThrowsAsync <TaskCanceledException>(async() => await pipeWriter.WriteAsync(new byte[1], cts.Token));
 }
        public async Task WriteCanBeCanceledViaCancelPendingFlushWhenFlushIsAsync()
        {
            var         pipeWriter  = new StreamPipeWriter(new HangingStream());
            FlushResult flushResult = new FlushResult();

            var tcs = new TaskCompletionSource <int>(TaskCreationOptions.RunContinuationsAsynchronously);

            var task = Task.Run(async() =>
            {
                try
                {
                    var writingTask = pipeWriter.WriteAsync(Encoding.ASCII.GetBytes("data"));
                    tcs.SetResult(0);
                    flushResult = await writingTask;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
            });

            await tcs.Task;

            pipeWriter.CancelPendingFlush();

            await task;

            Assert.True(flushResult.IsCanceled);
        }
        public async Task CancelPendingFlushAfterAllWritesAllDataIsPreserved()
        {
            MemoryStream = new CannotFlushStream();
            Writer       = new StreamPipeWriter(MemoryStream);
            FlushResult flushResult = new FlushResult();

            var tcs = new TaskCompletionSource <int>(TaskCreationOptions.RunContinuationsAsynchronously);

            var task = Task.Run(async() =>
            {
                try
                {
                    // Create two Segments
                    // First one will succeed to write, other one will hang.
                    var writingTask = Writer.WriteAsync(Encoding.ASCII.GetBytes("data"));
                    tcs.SetResult(0);
                    flushResult = await writingTask;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
            });

            await tcs.Task;

            Writer.CancelPendingFlush();

            await task;

            Assert.True(flushResult.IsCanceled);
        }
        public async Task CheckBasicWritePipeApi()
        {
            var pipe            = new Pipe();
            var writeOnlyStream = new WriteOnlyPipeStream(pipe.Writer);
            var pipeWriter      = new StreamPipeWriter(writeOnlyStream);
            await pipeWriter.WriteAsync(new byte[10]);

            var res = await pipe.Reader.ReadAsync();

            Assert.Equal(new byte[10], res.Buffer.ToArray());
        }
        public async Task CancelPendingFlushBetweenWritesAllDataIsPreserved()
        {
            MemoryStream = new SingleWriteStream();
            Writer       = new StreamPipeWriter(MemoryStream);
            FlushResult flushResult = new FlushResult();

            var tcs = new TaskCompletionSource <int>(TaskCreationOptions.RunContinuationsAsynchronously);

            var task = Task.Run(async() =>
            {
                try
                {
                    await Writer.WriteAsync(Encoding.ASCII.GetBytes("data"));

                    var writingTask = Writer.WriteAsync(Encoding.ASCII.GetBytes(" data"));
                    tcs.SetResult(0);
                    flushResult = await writingTask;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
            });

            await tcs.Task;

            Writer.CancelPendingFlush();

            await task;

            Assert.True(flushResult.IsCanceled);

            await Writer.WriteAsync(Encoding.ASCII.GetBytes(" more data"));

            Assert.Equal(Encoding.ASCII.GetBytes("data data more data"), Read());
        }
예제 #6
0
        public async Task CanAdvanceWithPartialConsumptionOfFirstSegment(int firstWriteLength)
        {
            Writer = new StreamPipeWriter(Stream, MinimumSegmentSize, new TestMemoryPool(maxBufferSize: 20000));
            await Writer.WriteAsync(Encoding.ASCII.GetBytes("a"));

            var memory = Writer.GetMemory(firstWriteLength);

            Writer.Advance(firstWriteLength);

            memory = Writer.GetMemory();
            Writer.Advance(memory.Length);

            await Writer.FlushAsync();

            Assert.Equal(firstWriteLength + memory.Length + 1, Read().Length);
        }
        public async Task CheckNestedPipeApi()
        {
            var pipe   = new Pipe();
            var writer = pipe.Writer;

            for (var i = 0; i < 3; i++)
            {
                var writeOnlyStream = new WriteOnlyPipeStream(writer);
                writer = new StreamPipeWriter(writeOnlyStream);
            }

            await writer.WriteAsync(new byte[10]);

            var res = await pipe.Reader.ReadAsync();

            Assert.Equal(new byte[10], res.Buffer.ToArray());
        }
 public async Task WriteCanBeCancelledViaProvidedCancellationToken()
 {
     var pipeWriter = new StreamPipeWriter(new HangingStream());
     var cts        = new CancellationTokenSource(1);
     await Assert.ThrowsAsync <TaskCanceledException>(async() => await pipeWriter.WriteAsync(Encoding.ASCII.GetBytes("data"), cts.Token));
 }
예제 #9
0
 public async Task WriteHelloWorld()
 {
     await _pipeWriter.WriteAsync(_helloWorldBytes);
 }