Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS UnitTest - Thread was Being Aborted

I have a set of Unit Tests run in parallel (this is important, it works fine if the tests are run single-threaded) which makes calls to WebClient to download a resource that consistently takes over 30 seconds to return, and this causes the Unit Test to forcefully exit with one of the following two messages:

Thread was being aborted.

The application domain in which the thread was running has been unloaded.

I have tried setting the [Timeout] attribute, various app.config settings, including even creating an EventWaitHandle to make the unit test thread wait on the WebClient thread, with no luck. I checked the Test Timeouts settings under the Test Settings, and it is set to the default of 30 minutes.


Edit 2:

As indicated by @peer, this is a known bug in the VS.Net Test Framework: http://connect.microsoft.com/VisualStudio/feedback/details/587390/threadabortexception-when-running-two-tests-in-parallel-one-taking-40-seconds

Edit:

Here is the simplest scenario which will reproduce the problem. Can you make these Unit Tests run to completion, and if so, how? These tests must be run in parallel! Start a new Visual Studio Unit Test project, and use the following settings and Test Method code. When you run, make sure they are actually running in parallel (i.e., you will need a processor with multiple cores, and check that they are running simultaneously. I have found that to get them to run in parallel, I have to apply all the settings, then close the project and re-open it again before the parallelization will apply).

Local.testsettings (add parallelTestCount="#", whatever applies to your processor):

<Description>These are default test settings for a local test run.</Description>
<Deployment enabled="false" />
<Execution parallelTestCount="4">
  <TestTypeSpecific />
  <AgentRule name="Execution Agents">
  </AgentRule>
</Execution>

TraceAndTestImpact.testsettings (comment out the DataCollectors):

<Description>These are test settings for Trace and Test Impact.</Description>
<Execution parallelTestCount="0">
  <TestTypeSpecific />
  <AgentRule name="Execution Agents">
    <!--<DataCollectors>
      <DataCollector uri="datacollector://microsoft/SystemInfo/1.0" assemblyQualifiedName="Microsoft.VisualStudio.TestTools.DataCollection.SystemInfo.SystemInfoDataCollector, Microsoft.VisualStudio.TestTools.DataCollection.SystemInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" friendlyName="System Information">
      </DataCollector>
      <DataCollector uri="datacollector://microsoft/HttpProxy/1.0" assemblyQualifiedName="Microsoft.VisualStudio.TraceCollector.HttpProxyCollector, Microsoft.VisualStudio.TraceCollector, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" friendlyName="ASP.NET Client Proxy for IntelliTrace and Test Impact">
      </DataCollector>
      <DataCollector uri="datacollector://microsoft/TestImpact/1.0" assemblyQualifiedName="Microsoft.VisualStudio.TraceCollector.TestImpactDataCollector, Microsoft.VisualStudio.TraceCollector, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" friendlyName="Test Impact">
      </DataCollector>
      <DataCollector uri="datacollector://microsoft/TraceDebugger/1.0" assemblyQualifiedName="Microsoft.VisualStudio.TraceCollector.TraceDebuggerDataCollector, Microsoft.VisualStudio.TraceCollector, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" friendlyName="IntelliTrace">
      </DataCollector>
    </DataCollectors>-->
  </AgentRule>
</Execution>

UnitTest1.cs (first unit test, sleeps 35 seconds):

[TestMethod]
public void TestMethod1()
{
    Thread.Sleep(35000);
    // in default unit test settings, this line will never be reached
    Console.WriteLine("TestMethod1");
}

UnitTest2.cs (second unit test, sleeps 35 seconds):

[TestMethod]
public void TestMethod2()
{
    Thread.Sleep(35000);
    // in default unit test settings, this line will never be reached
    Console.WriteLine("TestMethod2");
}

If you have the parallelization correctly setup, you will find that both tests will fail, with a ThreadAbortException, with either of the two messages presented at the top. How can I tell these methods to run longer than 30 seconds??

like image 593
mellamokb Avatar asked Jun 30 '11 16:06

mellamokb


1 Answers

You should go to menu: Test -> Edit Test Setting -> current test config

Go to the tab: Test Timeouts

Change the timeout to any time you like.

like image 71
Peter Avatar answered Sep 19 '22 05:09

Peter