开发者

Simple task library question

开发者 https://www.devze.com 2023-02-18 03:59 出处:网络
I\'m new to task library. I want to run开发者_开发知识库 some of my unit tests in parallel and test the concurrency issues using task library in .NET 4.0.

I'm new to task library. I want to run开发者_开发知识库 some of my unit tests in parallel and test the concurrency issues using task library in .NET 4.0.

As you know TestMethods are parameter-less and return nothing (void) so I need just run one of my tests by N threads concurrently.

[TestMethod()]
void MyTest()
{
    // Do Something
}

It can be done by traditional threads, but wanna use a more robust and managed technique, so:

How to run N number of MyTest() simultaneously using TaskLibrary?

I'm wondering why there's not a built-in attribute for concurrent unit-testing.


[TestMethod()]
void MyTest()
{
    // Do Something
}

[TestMethod()]
void MyTest_4_Times()
{
    Parallel.Invoke(MyTest, MyTest, MyTest, MyTest);
}

or if you want to change the number of concurrent tests by a parameter.

[TestMethod()]
void MyTest_4_Times()
{
    var n = 4;
    Task.WaitAll(Enumerable.Range(0, n).Select(_ => Task.Factory.StartNew(MyTest)).ToArray());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消