Using xUnit.net AutoRollback and Multi Threading
I am trying to write a test case using xUnit.net and the AutoRollback attribute provided by the xunit extensions.
My test case goes like this
[Fact,开发者_StackOverflow中文版 AutoRollback]<br>
public void TestCase()<br>
{
// insert into data table
repository.Insert(data);
// spawn a new thread and read data which you just inserted in the data table
Task.Factory.StartNew(action_to_read_data);
}
This test case fails and throws a connection timeout exception while reading the data in the new thread. The problem I have found is that the test case starts a transaction because of the AutoRollback attribute and while Inserting the data it locks the table for rollback at the end. The new thread spawned by the test case cannot read data from the same table because it is locked by the parent thread. I can read the data in the same thread though.
Anyone have a solution? I want to run multiple threads reading the data inserted above.
What I have found so far is that you can't use another thread which interacts with the same database in your unit test while using the AutoRollback feature. I ended up removing that attribute from my test case and keeping track of all objects inserted by the test case myself.
精彩评论