Whats the best way to unit test from multiple threads?
Asked Answered
L

4

25

this kind of follows on from another question of mine.

Basically, once I have the code to access the file (will review the answers there in a minute) what would be the best way to test it?

I am thinking of creating a method which just spawns lots of BackgroundWorker's or something and tells them all load/save the file, and test with varying file/object sizes. Then, get a response back from the threads to see if it failed/succeeded/made the world implode etc.

Can you guys offer any suggestions on the best way to approach this? As I said before, this is all kinda new to me :)

Edit

Following ajmastrean's post:

I am using a console app to test with Debug.Asserts :)


Update

I originally rolled with using BackgroundWorker to deal with the threading (since I am used to that from Windows dev) I soon realised that when I was performing tests where multiple ops (threads) needed to complete before continuing, I realised it was going to be a bit of a hack to get it to do this.

I then followed up on ajmastrean's post and realised I should really be using the Thread class for working with concurrent operations. I will now refactor using this method (albeit a different approach).

Lea answered 3/9, 2008 at 12:39 Comment(1)
Are you using the Visual Studio test tools? or a third party tool, like NUnit or MbUnit? There are some very different patterns for multi-threaded tests for each. And for that matter, are you unit testing or just putting a console app together for test purposes?Fishman
F
22

In .NET, ThreadPool threads won't return without setting up ManualResetEvents or AutoResetEvents. I find these overkill for a quick test method (not to mention kind of complicated to create, set, and manage). Background worker is a also a bit complex with the callbacks and such.

Something I have found that works is

  1. Create an array of threads.
  2. Setup the ThreadStart method of each thread.
  3. Start each thread.
  4. Join on all threads (blocks the current thread until all other threads complete or abort)
public static void MultiThreadedTest()
{
    Thread[] threads = new Thread[count];

    for (int i = 0; i < threads.Length; i++)
    {
        threads[i] = new Thread(DoSomeWork());
    }

    foreach(Thread thread in threads)
    {
        thread.Start();
    }

    foreach(Thread thread in threads)
    {
        thread.Join();
    }
}
Fishman answered 3/9, 2008 at 12:52 Comment(1)
Accepted because this deals with concurrency which is an issue with the testing since the main app thread terminates before BackgroundWorkers have completed their operation (and I got tired of adding Thread.Sleep!)Lea
P
1

@ajmastrean, since unit test result must be predictable we need to synchronize threads somehow. I can't see a simple way to do it without using events.

I found that ThreadPool.QueueUserWorkItem gives me an easy way to test such use cases

 ThreadPool.QueueUserWorkItem(x => { 
    File.Open(fileName, FileMode.Open);
    event1.Set(); // Start 2nd tread;
    event2.WaitOne(); // Blocking the file;
});
ThreadPool.QueueUserWorkItem(x => { 
    try
    {
        event1.WaitOne(); // Waiting until 1st thread open file
        File.Delete(fileName); // Simulating conflict
    }
    catch (IOException e)
    {
        Debug.Write("File access denied");
    }
});
Penta answered 3/9, 2008 at 13:6 Comment(0)
R
0

For those such as myself who stumble across this thread in the future - the answer given by @Anthony works fantastically, and is basically what I have been using for my (simple) multi-threaded tests. However, using System.Threading.Tasks.Parallel simplifies the following code:

public static void MultiThreadedTest()
{
    Thread[] threads = new Thread[count];

    for (int i = 0; i < threads.Length; i++)
    {
        threads[i] = new Thread(DoSomeWork());
    }

    foreach(Thread thread in threads)
    {
        thread.Start();
    }

    foreach(Thread thread in threads)
    {
        thread.Join();
    }
}

to

public static void MultiThreadedTest()
{
    Parallel.For(0, count, {
        DoSomeWork();
    });
}

What Parallel.For does is perform the loop iterations in parallel. It is also used as an example for the ConcurrentDictionary<TKey,TValue>.AddOrUpdate method (which is also where I saw it as an alternative to manually using threads). Alternatively, Parallel.Invoke can be used, as shown here.

Just be mindful of the difference between Parallel and Thread and the limitations of Parallel when it comes to performing background tasks, as discussed in this StackOverFlow post.

Russian answered 18/1 at 14:50 Comment(0)
I
-2

Your idea should work fine. Basically you just want to spawn a bunch of threads, and make sure the ones writing the file take long enough to do it to actually make the readers wait. If all of your threads return without error, and without blocking forever, then the test succeeds.

Iliad answered 3/9, 2008 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.