public async Task TryWrite_AfterComplete() { IWritableChannel <int> c = Channel.WriteToStream <int>(new MemoryStream()); Assert.True(c.TryComplete()); Assert.False(c.TryWrite(42)); AssertSynchronousFalse(c.WaitToWriteAsync()); await Assert.ThrowsAnyAsync <InvalidOperationException>(() => c.WriteAsync(42)); Assert.False(c.TryComplete()); }
public static void Complete <T>(this IWritableChannel <T> channel) { if (!channel.TryComplete()) { throw new OperationCanceledException(); } }
/// <summary>Mark the channel as being complete, meaning no more items will be written to it.</summary> /// <param name="channel">The channel to mark as complete.</param> /// <param name="error">Optional Exception indicating a failure that's causing the channel to complete.</param> /// <exception cref="InvalidOperationException">The channel has already been marked as complete.</exception> public static void Complete <T>(this IWritableChannel <T> channel, Exception error = null) { if (channel == null) { throw new ArgumentNullException("channel"); } if (!channel.TryComplete(error)) { throw CreateInvalidCompletionException(); } }
private async Task ProcessAsync() { try { if (_beginConsumeAsync != null) { await _beginConsumeAsync(_out).ConfigureAwait(false); } await _buffer.ConsumeBufferAsync(PropagateAsync, _disposeRejected).ConfigureAwait(false); if (_completeConsumeAsync != null) { await _completeConsumeAsync(_out).ConfigureAwait(false); } } catch (Exception ex) { _buffer.Out.TryTerminate(ex); if (_terminateConsumeAsync != null) { await _terminateConsumeAsync(ex, _out).IgnoreExceptions().ConfigureAwait(false); } throw; } finally { try { await _buffer.In.Completion.ConfigureAwait(false); _out.TryComplete(); } catch (Exception ex) { _out.TryTerminate(ex); throw; } finally { await _out.Completion.ConfigureAwait(false); } } }
private static async Task PropagateAsync(IReadableChannel <TransportMessageFrame> channel1, IWritableChannel <TransportMessageFrame> channel2) { try { while (true) { var result = await channel1.TryReadAsync().ConfigureAwait(false); if (!result.HasValue) { break; } await channel2.WriteAsync(result.Value).ConfigureAwait(false); } channel2.TryComplete(); } catch (Exception ex) { channel2.TryTerminate(ex); } }
public static async Task CompleteAsync <T>(this IWritableChannel <T> channel) { channel.TryComplete(); await channel.Completion.ConfigureAwait(false); }