简体   繁体   English

什么是从多个线程进行单元测试的最佳方法?

[英]Whats the best way to unit test from multiple threads?

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. 我正在考虑创建一个方法,它只生成大量的BackgroundWorker或者其他东西,告诉他们所有加载/保存文件,并测试不同的文件/对象大小。 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: 关注ajmastrean的帖子:

I am using a console app to test with Debug.Asserts :) 我正在使用控制台应用程序来测试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. 我最初使用BackgroundWorker来处理线程(因为我已经习惯了来自Windows开发)我很快就意识到当我执行测试时需要完成多个操作(线程)才能继续,我意识到它将会是让它做到这一点有点破解。

I then followed up on ajmastrean 's post and realised I should really be using the Thread class for working with concurrent operations. 然后我跟进了ajmastrean的帖子并意识到我应该使用Thread类来处理并发操作。 I will now refactor using this method (albeit a different approach). 我现在将使用这种方法进行重构(虽然是一种不同的方法)。

In .NET, ThreadPool threads won't return without setting up ManualResetEvent s or AutoResetEvent s. 在.NET中,如果不设置ManualResetEventAutoResetEventThreadPool线程将不会返回。 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. 设置每个线程的ThreadStart方法。
  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();
    }
}

@ajmastrean, since unit test result must be predictable we need to synchronize threads somehow. @ajmastrean,因为单元测试结果必须是可预测的,我们需要以某种方式同步线程。 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为我提供了一种测试此类用例的简便方法

 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");
    }
});

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. 如果所有线程都没有错误地返回,并且没有永久阻塞,则测试成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM