Sometimes while running a test plan, manually clicking on the stop button to abort it does not immediately abort the plan. It takes a very long time, and sometimes doesn’t abort even after a long time. User is forced to use the Task manager to kill the program. Is there any workaround for this?
Hallo,
Does this happen during the execution of custom test steps, or also with basic/default steps?
If it is during custom test steps, then it sounds to me like a loop that does not break/stop.
with the line below you can check if an abort is requested:
!TapThread.Current.AbortToken.IsCancellationRequested
Note:
This does nothing when there is a loop in used packages/dlls.
You mean I should add this piece of code inside any loops? And if requested, abort from the code?
!TapThread.Current.AbortToken.IsCancellationRequested
Also, what does your last line mean - “This does nothing when there is a loop in used packages/dlls.”
Yes, that is what we do.
Or at least in loops/code that can take a while to complete. In our cases: receive in serial communication, web api calls, etc.
Since it needs to be added in a loop, any existing code in packages/dlls will not break on abort.
At least that is to my knowledge.
In most code you just need to call this:
// this will throw a OperationCanceledException when the test plan is aborted.
TapThread.ThrowIfAborted();
in most cases this is all you need, you can just let the plan stop with this exception. If you need to do something in case of an abort you can use TapThread.Current.AbortToken.IsCancellationRequested
, but generally it’s fine to just let the exception break the execution.
If you are using C# APIs that are blocking, you can often add the cancellation token like this:
using var client = new HttpClient();
var result = client.GetAsync("http://localhost:8888/test", TapThread.Current.AbortToken).Result;
This will cause the request to get aborted and the test plan to end without waiting for the request to complete.