Unit testing concurrent code
In this section, we will adapt a sample from Chapter 9, to add unit test coverage. When your code uses async
and await
, adding reliable test coverage is very simple. At the end of the example, we will examine an alternative method of waiting to perform your assertions by using the SpinLock
struct.
Let’s create an xUnit.net unit test project for the ConcurrentOrderQueue
project and add several tests:
- Start by copying the ConcurrentOrderQueue project from Chapter 9. You can get the source code from the GitHub repository if you do not already have a copy of it: https://github.com/PacktPublishing/Parallel-Programming-and-Concurrency-with-C-sharp-10-and-.NET-6/tree/main/chapter09/ConcurrentOrderQueue.
- Open the ConcurrentOrderQueue solution in Visual Studio.
- Right-click the solution file in Solution Explorer and click on Add | New Project. Add an xUnit Unit Test project named
ConcurrentOrderQueue.Tests
. Make sure to add the new project...