Beispiel #1
0
    [Fact] // Bizarre behavior when using the built-in Pipe class: https://github.com/dotnet/corefx/issues/31696
    public async Task CancelPendingRead()
    {
        var stream = new HalfDuplexStream();
        var reader = this.CreatePipeReader(stream, sizeHint: 50);

        ValueTask <ReadResult> readTask = reader.ReadAsync(this.TimeoutToken);

        reader.CancelPendingRead();
        var readResult = await readTask.AsTask().WithCancellation(this.TimeoutToken);

        Assert.True(readResult.IsCanceled);
        ////reader.AdvanceTo(readResult.Buffer.End);

        // Verify we can read after that without cancellation.
        readTask = reader.ReadAsync(this.TimeoutToken);
        stream.Write(new byte[] { 1, 2, 3 }, 0, 3);
        await stream.FlushAsync(this.TimeoutToken);

        readResult = await readTask;
        Assert.False(readResult.IsCanceled);
        Assert.Equal(3, readResult.Buffer.Length);
        reader.AdvanceTo(readResult.Buffer.End);

        // Now cancel again
        readTask = reader.ReadAsync(this.TimeoutToken);
        reader.CancelPendingRead();
        readResult = await readTask;
        Assert.True(readResult.IsCanceled);
    }
    public async Task ReadAsyncAfterExamining()
    {
        byte[] expectedBuffer = this.GetRandomBuffer(2048);
        var    stream         = new HalfDuplexStream();

        stream.Write(expectedBuffer, 0, 50);
        await stream.FlushAsync(this.TimeoutToken);

        var reader = this.CreatePipeReader(stream, sizeHint: 50);

        byte[] actualBuffer = new byte[expectedBuffer.Length];

        ReadResult result = await reader.ReadAsync(this.TimeoutToken);

        reader.AdvanceTo(result.Buffer.Start, result.Buffer.GetPosition(1));

        // Since we didn't examine all the bytes already in the buffer, the next read should be synchronous,
        // and shouldn't give us any more buffer.
        ValueTask <ReadResult> resultTask = reader.ReadAsync(this.TimeoutToken);

        Assert.True(resultTask.IsCompleted);
        Assert.Equal(result.Buffer.Length, resultTask.Result.Buffer.Length);

        // Now examine everything, but don't consume it. We should get more.
        reader.AdvanceTo(resultTask.Result.Buffer.Start, resultTask.Result.Buffer.End);
        ValueTask <ReadResult> resultTask2 = reader.ReadAsync(this.TimeoutToken);

        Assert.False(resultTask2.IsCompleted);
        stream.Write(expectedBuffer, 50, 50);
        await stream.FlushAsync(this.TimeoutToken);

        var result2 = await resultTask2;

        Assert.True(result2.Buffer.Length > result.Buffer.Length);

        // Now consume everything and get even more.
        reader.AdvanceTo(result2.Buffer.End);
        stream.Write(expectedBuffer, 100, expectedBuffer.Length - 100);
        await stream.FlushAsync(this.TimeoutToken);

        ReadResult result3 = await reader.ReadAsync(this.TimeoutToken);

        Assert.True(result3.Buffer.Length > 0);
    }