Last active
September 16, 2019 10:14
-
-
Save chivandikwa/53d15282cba1a3b2ae6b72dec3c9ac2c to your computer and use it in GitHub Desktop.
Unit testing Task.Delay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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