Faking a Long Running Asynchronous Call
Join the DZone community and get the full member experience.
Join For FreeA few days ago I needed to make sure that a specific method would only get called once no matter how many times it’s caller is invoked. The simplified code looked something like this:
public async Task CallLongOperation() { var result = await _client.LongWebCall(); if (result.Failed) { // log failure and exit } }
I needed to make sure that the client was invoked just once no matter how many times CallLongOperationgets called – and to make things interesting the client’s method is asynchronous. Usually when I want to fake a long running call I use WaitHandle but in this case I had something even better…
During previous work I’ve used TaskCompletionSource in order to enable async calls where needed and it seemed only logical that the same method would work for my unit tests:
[TestMethod] public void CallLongOperation_DuringOperationCallAgain_CalledOnlyOnce() { var fakeClient = A.Fake<IClient>(); var completionSource = new TaskCompletionSource<Result>(); A.CallTo(() => fakeClient.LongWebCall()).Returns(completionSource.Task); var cut = new ClassUnderTest(fakeClient); cut.CallLongOperation(); cut.CallLongOperation(); completionSource.SetResult(new Result()); A.CallTo(() => fakeClient.LongWebCall()).MustHaveHappened(Repeated.Exactly.Once); }
Using the TaskCompletionSource(line 6) help me simulate an async call that won’t return until I callSetResult on it (line 14) and since I do not muse await on the actual call I write the whole test without creating any additional threads – creating a simple and deterministic unit test.
It amazed me on how simple it become testing methods that uses async/await - just because the code uses asynchronous calls does not mean that its unit tests need to be complicated or unpredictable.
Happy coding...
Published at DZone with permission of Dror Helper, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications
-
Azure Virtual Machines
-
Decoding ChatGPT: The Concerns We All Should Be Aware Of
-
The Role of Automation in Streamlining DevOps Processes
Comments