public void Test_CanThrowIfCancellationRequested() { Faker faker = new Faker(); Mock <IQueuedTaskToken> taskMock = new Mock <IQueuedTaskToken>(MockBehavior.Loose); using (CancellationTokenSource cts = new CancellationTokenSource()) { TaskExecutionContext ctx = new TaskExecutionContext(taskMock.Object, cts.Token); cts.Cancel(); Assert.Throws <OperationCanceledException>(() => ctx.ThrowIfCancellationRequested()); } }
private async Task<TaskExecutionResult> ExecuteTaskAsync ( TaskExecutionContext executionContext ) { ITaskExecutor taskExecutor = null; DateTimeOffset retryAt = DateTimeOffset.UtcNow; IQueuedTask dequeuedTask = executionContext .TaskToken .DequeuedTask; try { //Check for cancellation before we start execution executionContext.StartTimingExecution(); executionContext.ThrowIfCancellationRequested(); //Attempt to resolve and run task executor if ( ( taskExecutor = ResolveTaskExecutor( dequeuedTask ) ) != null ) { mLogger.DebugFormat( "Beginning task execution. Task id = {0}.", dequeuedTask.Id ); //Execute task await taskExecutor.ExecuteAsync( dequeuedTask.Payload, executionContext ); mLogger.DebugFormat( "Task execution completed. Task id = {0}.", dequeuedTask.Id ); //Ensure we have a result - since no exception was thrown // and no result explicitly set, assume success. if ( !executionContext.HasResult ) executionContext.NotifyTaskCompleted(); } } catch ( OperationCanceledException ) { //User code has observed cancellation request executionContext?.NotifyCancellationObserved(); } catch ( Exception exc ) { mLogger.Error( "Error executing queued task", exception: exc ); bool isRecoverable = mOptions.IsTaskErrorRecoverable( dequeuedTask, exc ); executionContext?.NotifyTaskErrored( new QueuedTaskError( exc ), isRecoverable: isRecoverable ); } finally { executionContext.StopTimingExecution(); } //Compute the amount of time to delay task execution // if execution failed if ( executionContext.HasResult && executionContext.ExecutionFailed ) retryAt = ComputeRetryAt( executionContext.TaskToken ); return taskExecutor != null ? new TaskExecutionResult( executionContext.ResultInfo, duration: executionContext.Duration, retryAt: retryAt, faultErrorThresholdCount: mOptions.FaultErrorThresholdCount ) : null; }