private static void Pipes_WaitForReadThenTryReadValues() { using (AnonymousPipeServerStream serverPipe = new AnonymousPipeServerStream(PipeDirection.Out)) using (AnonymousPipeClientStream clientPipe = new AnonymousPipeClientStream(PipeDirection.In, serverPipe.ClientSafePipeHandle)) { IWritableChannel <int> writer = Channel.WriteToStream <int>(serverPipe); IReadableChannel <int> reader = Channel.ReadFromStream <int>(clientPipe); Task.WaitAll( Task.Run(async() => { for (int i = 0; i < 100; i++) { await writer.WriteAsync(i); } writer.Complete(); Assert.False(writer.TryWrite(100)); }), Task.Run(async() => { int result; int i = 0; while (await reader.WaitToReadAsync()) { if (reader.TryRead(out result)) { Assert.Equal(i++, result); } } Assert.False(reader.TryRead(out result)); })); } }
public async Task Range_AllDataReadable_Success(int mode) { const int Start = 42, Count = 99; IReadableChannel <int> c = Channel.CreateFromEnumerable <int>(Enumerable.Range(Start, Count)); Assert.False(c.Completion.IsCompleted); int result; for (int i = Start; i < Start + Count; i++) { switch (mode) { case 0: // TryRead Assert.True(c.TryRead(out result)); Assert.Equal(i, result); break; case 1: // WaitToReadAsync then TryRead Assert.True(await c.WaitToReadAsync()); Assert.True(c.TryRead(out result)); Assert.Equal(i, result); break; case 2: // ReadAsync Assert.Equal(i, await c.ReadAsync()); break; case 3: // WaitToReadAsync then ReadAsync Assert.True(await c.WaitToReadAsync()); Assert.Equal(i, await c.ReadAsync()); break; case 4: // Multiple WaitToReadAsync then ReadAsync Assert.True(await c.WaitToReadAsync()); Assert.True(await c.WaitToReadAsync()); Assert.True(await c.WaitToReadAsync()); Assert.True(await c.WaitToReadAsync()); Assert.Equal(i, await c.ReadAsync()); break; } } Assert.False(await c.WaitToReadAsync()); Assert.False(await c.WaitToReadAsync()); Assert.False(c.TryRead(out result)); Assert.False(c.TryRead(out result)); await Assert.ThrowsAnyAsync <InvalidOperationException>(() => c.ReadAsync().AsTask()); await Assert.ThrowsAnyAsync <InvalidOperationException>(() => c.ReadAsync().AsTask()); await c.Completion; }
public void SuccessTask_BeforeTryRead_Success() { IReadableChannel <int> c = Channel.CreateFromTask(Task.FromResult(42)); AssertSynchronousTrue(c.WaitToReadAsync()); Assert.False(c.Completion.IsCompleted); int result; Assert.True(c.TryRead(out result)); Assert.Equal(42, result); Assert.True(c.Completion.IsCompleted); AssertSynchronousFalse(c.WaitToReadAsync()); Assert.False(c.TryRead(out result)); Assert.Equal(0, result); }
public async Task Completion_NotEmpty_Idempotent() { IReadableChannel <int> c = Channel.CreateFromEnumerable(Enumerable.Range(0, 10)); Assert.Same(c.Completion, c.Completion); while (await c.WaitToReadAsync()) { int result; Assert.True(c.TryRead(out result)); } await c.Completion; }
public async Task CanceledTask_BeforeCreation() { Task <int> t = Task.FromCanceled <int>(new CancellationToken(true)); IReadableChannel <int> c = Channel.CreateFromTask(t); Assert.Equal(TaskStatus.Canceled, c.Completion.Status); AssertSynchronousFalse(c.WaitToReadAsync()); await Assert.ThrowsAnyAsync <InvalidOperationException>(() => c.ReadAsync().AsTask()); int result; Assert.False(c.TryRead(out result)); Assert.Equal(0, result); }
public async Task FaultedTask_BeforeCreation() { Task <int> t = Task.FromException <int>(new FormatException()); IReadableChannel <int> c = Channel.CreateFromTask(t); Assert.Equal(TaskStatus.Faulted, c.Completion.Status); Assert.Same(t.Exception.InnerException, c.Completion.Exception.InnerException); AssertSynchronousFalse(c.WaitToReadAsync()); await Assert.ThrowsAsync <FormatException>(() => c.ReadAsync().AsTask()); int result; Assert.False(c.TryRead(out result)); Assert.Equal(0, result); }
public override Task <bool> TryInvokeAsync() { T item; if (_channel.TryRead(out item)) { try { _action(item); return(s_trueTask); } catch (Exception exc) { return(Task.FromException <bool>(exc)); } } return(s_falseTask); }
public async Task SuccessTask_AfterReadAsync_Success() { var tcs = new TaskCompletionSource <int>(TaskCreationOptions.RunContinuationsAsynchronously); IReadableChannel <int> c = Channel.CreateFromTask(tcs.Task); int result; Assert.False(c.TryRead(out result)); Task <int> read = c.ReadAsync().AsTask(); Assert.False(read.IsCompleted); tcs.SetResult(42); Assert.Equal(42, await read); await Assert.ThrowsAnyAsync <InvalidOperationException>(() => c.ReadAsync().AsTask()); }
public async Task CanceledTask_AfterCreation() { var tcs = new TaskCompletionSource <int>(); Task <int> t = tcs.Task; IReadableChannel <int> c = Channel.CreateFromTask(t); tcs.SetCanceled(); await Assert.ThrowsAnyAsync <InvalidOperationException>(() => c.Completion); AssertSynchronousFalse(c.WaitToReadAsync()); await Assert.ThrowsAnyAsync <InvalidOperationException>(() => c.ReadAsync().AsTask()); int result; Assert.False(c.TryRead(out result)); Assert.Equal(0, result); }
public async Task FaultedTask_AfterCreation() { var tcs = new TaskCompletionSource <int>(); Task <int> t = tcs.Task; IReadableChannel <int> c = Channel.CreateFromTask(t); tcs.SetException(new FormatException()); Assert.Equal(t.Exception.InnerException, await Assert.ThrowsAsync <FormatException>(() => c.Completion)); AssertSynchronousFalse(c.WaitToReadAsync()); await Assert.ThrowsAsync <FormatException>(() => c.ReadAsync().AsTask()); int result; Assert.False(c.TryRead(out result)); Assert.Equal(0, result); }
public void Enumerator_DisposedWhenCompleted() { bool disposeCalled = false; IReadableChannel <int> c = Channel.CreateFromEnumerable(new DelegateEnumerable <int> { GetEnumeratorDelegate = () => new DelegateEnumerator <int> { DisposeDelegate = () => disposeCalled = true } }); int i; while (c.TryRead(out i)) { Assert.False(disposeCalled); } Assert.True(disposeCalled); }
public override Task <bool> TryInvokeAsync() { T item; if (_channel.TryRead(out item)) { try { return(_action(item).ContinueWith(t => { t.GetAwaiter().GetResult(); return true; }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)); } catch (Exception exc) { return(Task.FromException <bool>(exc)); } } return(s_falseTask); }