public void ReadAsyncCancellationDeadlock() { var cts = new CancellationTokenSource(); var cts2 = new CancellationTokenSource(); var e = new ManualResetEventSlim(); ValueTaskAwaiter <ReadResult> awaiter = Pipe.Reader.ReadAsync(cts.Token).GetAwaiter(); awaiter.OnCompleted( () => { // We are on cancellation thread and need to wait until another ReadAsync call // takes pipe state lock e.Wait(); // Make sure we had enough time to reach _cancellationTokenRegistration.Dispose Thread.Sleep(100); // Try to take pipe state lock Pipe.Reader.ReadAsync(); }); // Start a thread that would run cancellation callbacks Task cancellationTask = Task.Run(() => cts.Cancel()); // Start a thread that would call ReadAsync with different token // and block on _cancellationTokenRegistration.Dispose Task blockingTask = Task.Run( () => { e.Set(); Pipe.Reader.ReadAsync(cts2.Token); }); bool completed = Task.WhenAll(cancellationTask, blockingTask).Wait(TimeSpan.FromSeconds(30)); Assert.True(completed, $"Read tasks are not completed. CancellationTask: {cancellationTask.Status} BlockingTask: {blockingTask.Status}"); }
public void ValueTask_MultipleContinuations_Throws() { ValueTaskAwaiter <int> readVt = CreateChannel().Reader.ReadAsync().GetAwaiter(); readVt.OnCompleted(() => { }); Assert.Throws <InvalidOperationException>(() => readVt.OnCompleted(() => { })); ValueTaskAwaiter <bool> waitReadVt = CreateChannel().Reader.WaitToReadAsync().GetAwaiter(); waitReadVt.OnCompleted(() => { }); Assert.Throws <InvalidOperationException>(() => waitReadVt.OnCompleted(() => { })); if (CreateFullChannel() != null) { ValueTaskAwaiter writeVt = CreateFullChannel().Writer.WriteAsync(42).GetAwaiter(); writeVt.OnCompleted(() => { }); Assert.Throws <InvalidOperationException>(() => writeVt.OnCompleted(() => { })); ValueTaskAwaiter <bool> waitWriteVt = CreateFullChannel().Writer.WaitToWriteAsync().GetAwaiter(); waitWriteVt.OnCompleted(() => { }); Assert.Throws <InvalidOperationException>(() => waitWriteVt.OnCompleted(() => { })); } }
public void ReadAsyncReturnsIsCancelOnCancelPendingReadBeforeGetResult() { ValueTaskAwaiter <ReadResult> awaitable = Pipe.Reader.ReadAsync().GetAwaiter(); Assert.False(awaitable.IsCompleted); awaitable.OnCompleted(() => { }); Pipe.Writer.WriteAsync(new byte[] { }); Pipe.Reader.CancelPendingRead(); Assert.True(awaitable.IsCompleted); ReadResult result = awaitable.GetResult(); Assert.True(result.IsCanceled); }
public void FlushAsyncReturnsIsCancelOnCancelPendingFlushBeforeGetResult() { PipeWriter writableBuffer = Pipe.Writer.WriteEmpty(MaximumSizeHigh); ValueTaskAwaiter <FlushResult> awaitable = writableBuffer.FlushAsync().GetAwaiter(); Assert.False(awaitable.IsCompleted); awaitable.OnCompleted(() => { }); Pipe.Reader.AdvanceTo(Pipe.Reader.ReadAsync().GetAwaiter().GetResult().Buffer.End); Pipe.Writer.CancelPendingFlush(); Assert.True(awaitable.IsCompleted); FlushResult result = awaitable.GetResult(); Assert.True(result.IsCanceled); }
public void GetResultThrowsIfReadAsyncCanceledBeforeOnCompleted() { var onCompletedCalled = false; var cancellationTokenSource = new CancellationTokenSource(); ValueTaskAwaiter <ReadResult> awaiter = Pipe.Reader.ReadAsync(cancellationTokenSource.Token).GetAwaiter(); bool awaiterIsCompleted = awaiter.IsCompleted; cancellationTokenSource.Cancel(); awaiter.OnCompleted( () => { onCompletedCalled = true; Assert.Throws <OperationCanceledException>(() => awaiter.GetResult()); }); Assert.False(awaiterIsCompleted); Assert.True(onCompletedCalled); }
public void GetResultThrowsIfFlushAsyncCanceledBeforeOnCompleted() { var onCompletedCalled = false; var cancellationTokenSource = new CancellationTokenSource(); PipeWriter buffer = Pipe.Writer.WriteEmpty(MaximumSizeHigh); ValueTaskAwaiter <FlushResult> awaiter = buffer.FlushAsync(cancellationTokenSource.Token).GetAwaiter(); bool awaiterIsCompleted = awaiter.IsCompleted; cancellationTokenSource.Cancel(); awaiter.OnCompleted( () => { onCompletedCalled = true; Assert.Throws <OperationCanceledException>(() => awaiter.GetResult()); }); Assert.False(awaiterIsCompleted); Assert.True(onCompletedCalled); }
public void ReadAsyncNotCompletedAfterCancellationTokenCanceled() { var onCompletedCalled = false; var cts = new CancellationTokenSource(); ValueTaskAwaiter <ReadResult> awaitable = Pipe.Reader.ReadAsync(cts.Token).GetAwaiter(); Assert.False(awaitable.IsCompleted); awaitable.OnCompleted( () => { onCompletedCalled = true; Assert.True(awaitable.IsCompleted); Assert.Throws <OperationCanceledException>(() => awaitable.GetResult()); awaitable = Pipe.Reader.ReadAsync().GetAwaiter(); Assert.False(awaitable.IsCompleted); }); cts.Cancel(); Assert.True(onCompletedCalled); }
public void ReadAsyncNotCompletedAfterCancellation() { var onCompletedCalled = false; ValueTaskAwaiter <ReadResult> awaitable = Pipe.Reader.ReadAsync().GetAwaiter(); Assert.False(awaitable.IsCompleted); awaitable.OnCompleted( () => { onCompletedCalled = true; Assert.True(awaitable.IsCompleted); ReadResult readResult = awaitable.GetResult(); Assert.True(readResult.IsCanceled); awaitable = Pipe.Reader.ReadAsync().GetAwaiter(); Assert.False(awaitable.IsCompleted); }); Pipe.Reader.CancelPendingRead(); Assert.True(onCompletedCalled); }
public async Task ReadAsyncReturnsDataAfterItWasWrittenDuringCancelledRead() { ValueTask <ReadResult> readTask = Pipe.Reader.ReadAsync(); ValueTaskAwaiter <ReadResult> awaiter = readTask.GetAwaiter(); ReadResult result = default; awaiter.OnCompleted( () => { Pipe.Writer.WriteAsync(new byte[] { 1 }).AsTask().Wait(); result = awaiter.GetResult(); }); Pipe.Reader.CancelPendingRead(); Assert.True(result.IsCanceled); result = await Pipe.Reader.ReadAsync(); Assert.False(result.IsCanceled); Assert.Equal(new byte[] { 1 }, result.Buffer.ToArray()); }
public void FlushAsyncCancellationDeadlock() { var cts = new CancellationTokenSource(); var cts2 = new CancellationTokenSource(); PipeWriter buffer = Pipe.Writer.WriteEmpty(MaximumSizeHigh); var e = new ManualResetEventSlim(); ValueTaskAwaiter <FlushResult> awaiter = buffer.FlushAsync(cts.Token).GetAwaiter(); awaiter.OnCompleted( () => { // We are on cancellation thread and need to wait untill another FlushAsync call // takes pipe state lock e.Wait(); // Make sure we had enough time to reach _cancellationTokenRegistration.Dispose Thread.Sleep(100); // Try to take pipe state lock buffer.FlushAsync(); }); // Start a thread that would run cancellation calbacks Task cancellationTask = Task.Run(() => cts.Cancel()); // Start a thread that would call FlushAsync with different token // and block on _cancellationTokenRegistration.Dispose Task blockingTask = Task.Run( () => { e.Set(); buffer.FlushAsync(cts2.Token); }); bool completed = Task.WhenAll(cancellationTask, blockingTask).Wait(TimeSpan.FromSeconds(10)); Assert.True(completed); }
public void FlushAsyncNotCompletedAfterCancellationTokenCanceled() { var onCompletedCalled = false; var cts = new CancellationTokenSource(); PipeWriter writableBuffer = Pipe.Writer.WriteEmpty(MaximumSizeHigh); ValueTaskAwaiter <FlushResult> awaitable = writableBuffer.FlushAsync(cts.Token).GetAwaiter(); Assert.False(awaitable.IsCompleted); awaitable.OnCompleted( () => { onCompletedCalled = true; Assert.True(awaitable.IsCompleted); Assert.Throws <OperationCanceledException>(() => awaitable.GetResult()); awaitable = writableBuffer.FlushAsync().GetAwaiter(); Assert.False(awaitable.IsCompleted); }); cts.Cancel(); Assert.True(onCompletedCalled); }
public void OnReaderCompletedRanBeforeFlushContinuation() { var callbackRan = false; var continuationRan = false; var pipe = new Pipe(new PipeOptions(_pool, pauseWriterThreshold: 5, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false)); pipe.Writer.OnReaderCompleted( (exception, state) => { Assert.False(continuationRan); callbackRan = true; }, null); PipeWriter buffer = pipe.Writer.WriteEmpty(10); ValueTaskAwaiter <FlushResult> awaiter = buffer.FlushAsync().GetAwaiter(); Assert.False(awaiter.IsCompleted); awaiter.OnCompleted(() => { continuationRan = true; }); pipe.Reader.Complete(); Assert.True(callbackRan); pipe.Writer.Complete(); }
public void FlushAsyncNotCompletedAfterCancellation() { var onCompletedCalled = false; PipeWriter writableBuffer = Pipe.Writer.WriteEmpty(MaximumSizeHigh); ValueTaskAwaiter <FlushResult> awaitable = writableBuffer.FlushAsync().GetAwaiter(); Assert.False(awaitable.IsCompleted); awaitable.OnCompleted( () => { onCompletedCalled = true; Assert.True(awaitable.IsCompleted); FlushResult flushResult = awaitable.GetResult(); Assert.True(flushResult.IsCanceled); awaitable = writableBuffer.FlushAsync().GetAwaiter(); Assert.False(awaitable.IsCompleted); }); Pipe.Writer.CancelPendingFlush(); Assert.True(onCompletedCalled); }
private async void SynthesizeTxtBuffer(string text, string model) { UtteranceSynthesisRequest request = MakeRequest(text, model); // request.Hints.Add(new Hints() { Voice = "kuznetsov_male" }); Metadata callHeaders = this.MakeMetadata(); callHeaders.Add("x-folder-id", this.FolderId); CancellationTokenSource cancellationSource = new CancellationTokenSource(); var call = synthesizerClient.UtteranceSynthesis(request, headers: callHeaders, deadline: DateTime.UtcNow.AddMinutes(5)); log.Information($"synthizing: {text}"); var respEnum = call.ResponseStream.ReadAllAsync(cancellationSource.Token).GetAsyncEnumerator(); try { ValueTaskAwaiter <bool> tsk = respEnum.MoveNextAsync().GetAwaiter(); tsk.OnCompleted(() => { if (respEnum.Current != null) { byte[] data = respEnum.Current.AudioChunk.Data.ToByteArray(); TextToSpeachResultsRecieved?.Invoke(this, AudioDataEventArgs.FromByateArray(data, data.Length)); log.Information($"Audio chunk {data.Length} bytes recieved."); } else { log.Warning("No data in response"); } }); while (!tsk.IsCompleted) { Thread.Sleep(200); } return; } catch (Exception ex) { log.Error(ex.Message); } finally { if (respEnum != null) { await respEnum.DisposeAsync(); } /* IAsyncEnumerator<UtteranceSynthesisResponse> respEnum = respEnumerable.GetAsyncEnumerator(); * * while (!respEnum.MoveNextAsync().GetAwaiter().IsCompleted) * { * Thread.Sleep(200); * } * * byte[] data = respEnum.Current.AudioChunk.Data.ToByteArray(); * TextToSpeachResultsRecieved?.Invoke(this, AudioDataEventArgs.FromByateArray(data, * data.Length)); * * await respEnum.DisposeAsync(); * call.Dispose();*/ } }
public void OnCompleted(Action completion) => awaiter.OnCompleted(completion);