Skip to content

Instantly share code, notes, and snippets.

@chivandikwa
Last active September 16, 2019 10:14
Show Gist options
  • Save chivandikwa/53d15282cba1a3b2ae6b72dec3c9ac2c to your computer and use it in GitHub Desktop.
Save chivandikwa/53d15282cba1a3b2ae6b72dec3c9ac2c to your computer and use it in GitHub Desktop.
Unit testing Task.Delay
public interface IDelayService
{
Task Delay(TimeSpan delayDuration, Action continueWith);
}
internal class DelayService : IDelayService
{
public virtual async Task Delay(TimeSpan delayDuration, Action continueWith)
{
await Task.Run(async () => { await Task.Delay(delayDuration); })
.ContinueWith(_ => continueWith())
.ConfigureAwait(false);
}
}
internal class DelayServiceStub : DelayService
{
public AutoResetEvent DelayComplete = new AutoResetEvent(false);
public override async Task Delay(TimeSpan delayDuration, Action continueWith)
{
await base.Delay(TimeSpan.Zero, continueWith);
DelayComplete.Set();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment