I think that nunit isn't function properly when threading code is involved:
Here's the sample code:
public class multiply
{
public Thread myThread;
public int Counter
{
get;
private set;
}
public string name
{
get;
private set;
}
private Object thisLock = new Object();
public void RunConsolePrint()
{
//lock(thisLock)
//{
Console.WriteLine("Now thread " + name + " has started");
for (int i = 1; i<= Counter; i++)
{
Console.WriteLine(name + ": count has reached " + i+ ": total count is "+Counter);
}
Console.WriteLine("Thread " + name + " has finished");
// }
}
public multiply(string pname, int pCounter)
{
name = pname;
Counter = pCounter;
myThread = new Thread(new ThreadStart(RunConsolePrint));
}
}
And here's the test code:
[Test]
public void Main()
{
counter=100;
multiply m2=new multiply("Second", counter);
multiply m1 = new multiply("First", counter);
m1.myThread.Start();
m2.myThread.Start();
}
And the output is a sequential running of m1
and m2
, which means that the loop in m1
is always execute first before m2
, at least that's what my testing shows. I ran the tests a few times and I always get this.
Is this a bug? Or an expected behavior?
If I copy the above code to a console program and run, I can see the threading effect clearly.
I am using the test using TestDriven.net runner.