Hello All,
For unit testing purposes, instead of forcing the current executing thread to sleep or wait for a message, I wrap the message retrieving code into the Polly. You can find a sample code to check the published messages below:
var messages = await RetryAndWait.SelectAsync(
_ => _busTestHarness.Published.SelectAsync<TMessage>(
BusCancellationToken)
.Select(m => m.Context.Message), expectedPublishedMessageCount, delayBeforeRead, retryCount)
.ToListAsync();
return messages.ToArray();
This is working perfectly most of the time but sometimes, test process crashing and ending whole testing pipeline (and if it is deploying, whole process). Here is the error I am getting:
The active test run was aborted. Reason: Test host process crashed : Unhandled exception. System.InvalidOperationException: An attempt was made to transition a task to a final state when it had already completed.
at MassTransit.Testing.AsyncMessageListExtensions.Select[TElement,TResult](IAsyncEnumerable`1 elements)+MoveNext()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext(Thread threadPoolThread)
at System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore`1.InvokeContinuation()
at MassTransit.Testing.MessageObservers.AsyncElementList`1.SelectAsync(FilterDelegate`1 filter, CancellationToken cancellationToken)+MoveNext()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext(Thread threadPoolThread)
at System.Threading.ThreadPoolWorkQueue.Dispatch()
What is wrong here? Logic seems OK but for some reason it sometimes crashing.
Note:
* BusCancellation token is basically a cancellation token with 1 second timeout.
* RetryAndWait is a wrapper for Polly
* I do not know if it is directly related but it is happening more frequently on Jenkins pipeline compared to local test runs.
I’d really appreciate any help or suggestions.
Best,
Barish