public async Task Complete(WebSocketCloseStatus closeStatus, string statusDescription) { if (_socket.State != WebSocketState.Closed && _socket.State != WebSocketState.CloseSent && _socket.State != WebSocketState.Aborted) { try { if (closeStatus == WebSocketCloseStatus.NormalClosure) { await _socket.CloseAsync( closeStatus, statusDescription, CancellationToken.None); } else { await _socket.CloseOutputAsync( closeStatus, statusDescription, CancellationToken.None); } } finally { _startBlock.Complete(); } } }
private async Task BlockWillCompleteTarget(Func <ISourceBlock <int> > BlockFactory) { ISourceBlock <int> block = BlockFactory(); TestTargetBlock <int> testTarget = new TestTargetBlock <int>(); testTarget.ConsumptionMode = DataflowMessageStatus.Accepted; block.LinkTo(testTarget, PropagateCompletion); // Rapidly send 50 messages Task.WaitAll(Enumerable.Range(0, 50).Select((i) => ((ITargetBlock <int>)block).SendAsync(i)).ToArray(), BurstTimeout); block.Complete(); // Completion should run to successful conclusion await Task.WhenAny(block.Completion, Task.Delay(CompletionTimeout)); Assert.Equal(TaskStatus.RanToCompletion, block.Completion.Status); // Assumption: the block should also have completed its target await Task.WhenAny(testTarget.Completion, Task.Delay(CompletionTimeout)); Assert.Equal(TaskStatus.RanToCompletion, testTarget.Completion.Status); // Assumption: we should have gotten 50 messages bool allMessagesReceived = await TaskUtils.PollWaitAsync(() => testTarget.MessagesConsumed.Count == 50, MessageArrivalTimeout); Assert.True(allMessagesReceived); }
private async Task StuckCompletionCannotBeCancelled(Func <CancellationToken, ISourceBlock <int> > BlockFactory, Func <ISourceBlock <int>, int> OutputCount) { CancellationTokenSource cts = new CancellationTokenSource(); ISourceBlock <int> block = BlockFactory(cts.Token); ITargetBlock <int> blockT = (ITargetBlock <int>)block; TestTargetBlock <int> testTarget = new TestTargetBlock <int>(); testTarget.ConsumptionMode = DataflowMessageStatus.Declined; block.LinkTo(testTarget, PropagateCompletion); Assert.True(blockT.Post(1)); Assert.True(blockT.Post(2)); block.Complete(); // Completion task should still be running // Also, we assume that BufferBlock will not start target's completion until it itself completes await Task.WhenAny(block.Completion, testTarget.Completion, Task.Delay(CompletionInProgressVerificationTimeout)); Assert.False(block.Completion.IsCompleted); Assert.True(testTarget.Completion.IsNotStarted()); // Assumption: cancellation does not affect the completion of the block (unfortunately!) cts.Cancel(); await Task.WhenAny(block.Completion, testTarget.Completion, Task.Delay(CompletionInProgressVerificationTimeout)); Assert.False(block.Completion.IsCompleted); Assert.True(testTarget.Completion.IsNotStarted()); Assert.Equal(2, OutputCount(block)); }
public PingStoringBlock(Func <string[], Task> storeFunc) { _source = new BatchBlock <string>(25); var storageBlock = new ActionBlock <string[]>(storeFunc, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded }); _source.LinkTo(storageBlock, new DataflowLinkOptions { PropagateCompletion = true }); _target = new ActionBlock <string>(async(message) => { await((BatchBlock <string>)_source).SendAsync(message); }); _target.Completion.ContinueWith(t => { if (t.IsFaulted) { _source.Fault(t.Exception); } else { _source.Complete(); } }); }
public virtual void Dispose() { if (_configSource == null) { return; } _configSource.Complete(); }
protected virtual void StartProcessingSource(ISourceBlock sourceBlock) { var task = Task.Factory.StartNew(() => { sourceBlock.Start(); sourceBlock.Complete(); }); //Add the task to be completed PendingTasks.Add(task); }
static void Dump <T>(ISourceBlock <T> source, ITargetBlock <T> target) { using (source.LinkTo(target, new DataflowLinkOptions { PropagateCompletion = true })) { source.Complete(); source.Completion.Wait(); } target.Completion.Wait(); }
public TelemetryReaderNode(IEnumerable <ITelemetrySourceFactory> telemetrySourceFactories) { var createSource = TelemetrySourceSelector(telemetrySourceFactories); var externalGameTelemetrySource = new BufferBlock <V0.IGameTelemetry>(); GameTelemetrySource = externalGameTelemetrySource; RunningGameTarget = new ActionBlock <IRunningGame>(runningGame => { _currentGameTelemetrySource?.Complete(); _currentGameTelemetrySource = createSource(runningGame.Name); _currentGameTelemetrySource?.LinkTo(externalGameTelemetrySource, new DataflowLinkOptions()); }); }
private async Task BlockKeepsDeclinedMessages(Func <ISourceBlock <int> > BlockFactory, Func <ISourceBlock <int>, int> OutputCount) { ISourceBlock <int> block = BlockFactory(); ITargetBlock <int> blockT = (ITargetBlock <int>)block; TestTargetBlock <int> testTarget = new TestTargetBlock <int>(); testTarget.ConsumptionMode = DataflowMessageStatus.Declined; block.LinkTo(testTarget, PropagateCompletion); // Assumption: block will keep incoming messages even when its target is declining them Assert.True(blockT.Post(1)); Assert.True(blockT.Post(2)); Assert.True(blockT.Post(3)); // The block has run out of capacity Assert.False(blockT.Post(4)); // This message will be postponed (and, in fact, released when we ask the block to complete) blockT.SendAsync(5).Forget(); // Wait till the block offers a message // Assumption: only one message will be offered, the block will not offer more messages if the target declines bool oneMessageOffered = await TaskUtils.PollWaitAsync(() => testTarget.MessagesDeclined.Count == 1, MessageArrivalTimeout); Assert.True(oneMessageOffered); Assert.True(testTarget.MessagesConsumed.Count == 0); Assert.True(testTarget.MessagesPostponed.Count == 0); // Use poll waiting because the item count is not always immediately updated by the block bool threeMessagesBuffered = await TaskUtils.PollWaitAsync(() => OutputCount(block) == 3, MessageArrivalTimeout); Assert.True(threeMessagesBuffered); // Assumption: the block will try NOT to deliver declined messages again when asked to complete. testTarget.ConsumptionMode = DataflowMessageStatus.Accepted; block.Complete(); bool someMessagesDelivered = await TaskUtils.PollWaitAsync(() => testTarget.MessagesConsumed.Count > 0, MessageArrivalTimeout); Assert.False(someMessagesDelivered); // Completion task should still be running await Task.WhenAny(block.Completion, Task.Delay(CompletionTimeout)); Assert.False(block.Completion.IsCompleted); // Assumption: block will not start target's completion until it itself completes await Task.WhenAny(testTarget.Completion, Task.Delay(CompletionTimeout)); Assert.True(testTarget.Completion.IsNotStarted()); }
public PingParsingBlock(params BasePingParser[] parsers) { _deadLetter = new BufferBlock <string>(); _source = new BufferBlock <Ping>(); _buffer = new BufferBlock <string>(); _target = new ActionBlock <string>(async(s) => { await((BufferBlock <string>)_buffer).SendAsync(s); }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded }); for (int i = 0; i < parsers.Length; i++) { var parser = parsers[i]; _buffer.LinkTo(parser, new DataflowLinkOptions { PropagateCompletion = true }, parser.Filter); parser.LinkTo(_source, new DataflowLinkOptions { PropagateCompletion = true }); _pendingTasks.Add(parser.Completion); } _buffer.LinkTo(_deadLetter, new DataflowLinkOptions { PropagateCompletion = true }); _target.Completion.ContinueWith((t) => { if (t.IsFaulted) { _buffer.Fault(t.Exception); } else { _buffer.Complete(); } }); _pendingTasks.Add(_source.Completion); _pendingTasks.Add(_target.Completion); _pendingTasks.Add(_buffer.Completion); }
public BasePingParser(Func <string, Ping> parse) { _source = new BufferBlock <Ping>(); _target = new ActionBlock <string>(async msg => await((BufferBlock <Ping>)_source).SendAsync(parse(msg)), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded }); _target.Completion.ContinueWith((t) => { if (t.IsFaulted) { _source.Fault(t.Exception); } else { _source.Complete(); } }); }
public async Task Complete(WebSocketCloseStatus closeStatus, string statusDescription) { if (_socket.State != WebSocketState.Closed && _socket.State != WebSocketState.CloseSent) { if (closeStatus == WebSocketCloseStatus.NormalClosure) { // If nothing went wrong, close connection with handshakes. await _socket.CloseOutputAsync( closeStatus, statusDescription, CancellationToken.None); } else { // Something went wrong, so don't wait for answer from the other side, just close the connection. await _socket.CloseAsync( closeStatus, statusDescription, CancellationToken.None); } _startBlock.Complete(); } }
public void Dispose() { metricQueue.Complete(); backgroundTask.Dispose(); }
public void Complete() { actualSource.Complete(); }
public void Complete() { compHelper.Complete(); source.Complete(); target.Complete(); }
public static async Task CompleteAfterTargetsFinishAsync <T>(this ISourceBlock <T> source, IEnumerable <ITargetBlock <T> > targetBlocks) { await Task.WhenAll(targetBlocks.Select(tb => tb.Completion)).ContinueWith(_ => source.Complete()).ConfigureAwait(false); await source.Completion.ConfigureAwait(false); }
public static async Task CompleteAfterTargetFinishAsync <T>(this ISourceBlock <T> source, ITargetBlock <T> targetBlock) { await targetBlock.Completion.ContinueWith(_ => source.Complete()).ConfigureAwait(false); await source.Completion.ConfigureAwait(false); }
public static async Task CompleteAsync <T>(this ISourceBlock <T> source, IEnumerable <ITargetBlock <T> > targetBlocks) { source.Complete(); await Task.WhenAll(targetBlocks.Select(tb => tb.Completion)).ConfigureAwait(false); }
public Task Complete() { _startBlock.Complete(); return(Task.CompletedTask); }
public void Complete() { orderCapturer.Complete(); }
public void Complete() { _timeoutBlock.Complete(); _batchBlock.Complete(); }
public static async Task CompleteAsync <T>(this ISourceBlock <T> source, ITargetBlock <T> targetBlock) { source.Complete(); await Task.WhenAll(source.Completion, targetBlock.Completion).ConfigureAwait(false); }