Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have to test the behavior of components when executing parallel operations. I haven't found a library / toolbox / unit test extension helping me doing this.

I want to produce exact execution sequences in the parallel code fragments, so I could do this with events manually. But this is very time consuming and annoying.
Posted

Do you try this?
PNUnit[^]
 
Share this answer
 
Comments
Thomas Maierhofer (Tom) 29-Dec-14 11:04am    
PNunit is good for the described Client/Server scenarios, but I want to test one component (e.g. a thread save collection) with multiple threads. It doesn't help much in this case.
As it turned out, that no solution exists which matches my needs, I used the remaining days of the year to write a concurrency test helper. This is from the unit tests and it shows three parallel code blocks that are executed, synchronized and analyzed using the test helper class:

C#
[Test]
public void SetWaitMultipleEventsTest()
{
    CTestHelper.Run(
        c =>
            {
                Thread.Sleep(1000);
                CTestHelper.AddSequenceStep("s1", "Provide1");
                CTestHelper.SetEvent("e1");
            },
        c =>
            {
                Thread.Sleep(1000);
                CTestHelper.AddSequenceStep("s2", "Provide2");
                CTestHelper.SetEvent("e2");
            },
        c =>
            {
                CTestHelper.WaitAllEvents(new[] { "e1", "e2" });
                CTestHelper.AddSequenceStep("s1", "Consume1");
                CTestHelper.AddSequenceStep("s2", "Consume2");
            },
        TimeSpan.FromSeconds(10));

    Expect(CTestHelper.GetSequence("s1"), Is.EquivalentTo(new[] { "Provide1", "Consume1" }));
    Expect(CTestHelper.GetSequence("s2"), Is.EquivalentTo(new[] { "Provide2", "Consume2" }));
}


The CTestHelper.Run() method runs the provided code blocks (Actions) in parallel and waits until they are finished or a timeout has occurred. Synchronization mechanisms,... are provided too. Samples will follow soon.

Resources (so far):
Crawler-Lib Concurrency Testing Helper Home
Crawler-Lib Concurrency Testing Helper NuGet Package
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900