The Task Parallel Library contains a very interesting set of dataflow components that for some reason they didn’t get the attention they deserve. This set is called TPL Dataflow Library and in few words, it is a robust in-process actor library that worth spending some time learning it! In this post, we will learn how to use it for concurrency testing.
What is the Task Parallel Library
The TPL is a set of types and APIs that allow developers to write parallel programs that target multi-core machines. The goal is to simplify the process of adding parallelism and concurrency to applications, by scaling the degree of concurrency dynamically and handling the partitioning of the work, the thread scheduling and other low-level details internally. This allows the developer to maximize performance while focusing on the actual work to be done.
Parallel Programming is taking advantage of multiple CPU cores to execute multiple threads simultaneously. Learn more about it here.
What is the TPL Dataflow Library
It is a set of dataflow components that promote actor-based programming by providing in-process message passing. This dataflow is extremely useful in cases of multiple operations that need to asynchronously talk to each other, for example parsing chunks of data as they arrive. In essence, the TPL Dataflow Library provides a foundation for message passing and parallelizing CPU and IO intensive applications.
The easiness of use can be demonstrated following these simple steps:
- Create an
var block = new ActionBlock<Uri>(_ => _.DoSomething(uri));
- Sent items to it
- Wait until it’s done
And that’s it!
Of course this is far away from any working solution, but it illustrates perfectly the simplicity upon the dataflow library is build.
Going a bit deeper with ActionBlock
Production code of anything useful should include more than a simple method call: TPL Dataflow supports out of the box cancellation, capacity bound that protects from memory growth, degree of parallelism capping and of course async/awaits. Following, an example that uses the most important of the
Are tests for concurrency issues possible?
It is theoretically impossible to create a test that proves there are no concurrency issues in a black-box system, and the reason is simple: Maybe the developer made it thread-unsafe on her/his birthday! Nevertheless, in a white-box system where the code that does the work and the code that does the threading are separate, concurrency tests are feasible by mocking the code that does the work. This guide on Practical Testing has C++ as codebase, but it is an interesting read.
In a more simplistic approach though, let’s assume a
WeekendCounter class with a
Count() method that returns the number of all weekend since the start of time till the
DateTime to. The code would need various improvements but it perfectly serves the reason of this post:
Using TPL Dataflow in a unit test
Essentially, a unit test is nothing more than a method that instantiates a small portion of our application and verifies its behavior independently from other parts. In the following test method one instance of the
WeekendCounter class can be tested for concurrency issues, by calling in parallel the
Count() method. The limitation here would only be the number of logical cores available, as more than one is of course needed.
From Microsoft Docs: If the current machine contains multiple processor groups, this property returns the number of logical processors that are available for use by the common language runtime (CLR).
The code that follows uses xUnit and TPL Dataflow to test the
Count() method, asserting the expected count on each block:
ActionBlock is the most useful block and alone covers almost all cases, there are more that adding together as puzzle pieces create a flow of information from one block to another, thus the name data flow. The reason behind why this library didn’t get traction it is surely puzzling, especially for the simple ActionBlock. Nevertheless, the applications are potentially unlimited, from just a few lines of code that test your app to biggest complex applications like the Microsoft Advanced Threat Analytics