I need to write a service with some complex behaviour like simultanious tasks and I'm facing an issue with.
I've written a sample in xUnit to show you the problem.
1 want to execute a task on a background, eventually start some child tasks. At a moment in time the task needs to be cancelled.
Therefore, I have the following in place:
[Fact]
public void ShouldWaitUnitTaskCompleted()
{
Task.Factory.StartNew(() =>
{
while (!cancellationTokenSource.IsCancellationRequested)
{
Task.Delay(10000).Wait();
TaskIsCompleted = true;
}
}, cancellationTokenSource.Token);
Thread.Sleep(3000);
cancellationTokenSource.Cancel();
Assert.True(TaskIsCompleted);
}
}
However, the xUnit completes after 3 seconds (my thread sleep). In my task, I'm having a loop that say, as long as it's not a cancellation request delay if for 10 second.
So the behaviour I expect would be:
- Start the application.
- Start the task (since it's no cancellation request, a delay of 10 seconds will start).
- Wait for 3 seconds and then cancel the token.
- Wait for the task with the 10 second delay to complete and then exit.
Why doesn't my code wait for the 10 seconds frame to pass?