Re: Threadstatic Problem

"Charlie Poole" <[email protected]>
Newsgroups gmane.comp.windows.dotnet.nunit.user
Message-ID <012401c89b3c$3351c8c0$6401a8c0@ferrari>
Hi Robbie,
 
NUnit creates a two threads. One runs all the tests, including setup and
teardown.
The other thread pumps events from your tests to the gui and doesn't execute
any of your code.
 
The only thing NUnit does on the main thread is to load your tests. That is
the point where any static initialization would occur, which is why you
should
not use static initialization on your ThreadStatic fields.
 
If you have anything like this:
 
[ThreadStatic]
static int mystring = "abcd";
 
change it to 
 
[TreadStatic]
static int mystring;
 
public MyClass()
{
   mystring = "abcd";
}
 
Note that MyClass is an instance constructor, not a static constructor.
 
Hope this helps.
 
Charlie


  _____  

From: [email protected] [mailto:[email protected]]

Sent: Thursday, April 10, 2008 1:05 AM
To: [email protected]; [email protected]
Subject: RE: [Nunit-users] Threadstatic Problem



Charlie,

 

Thanks for getting back to me so quickly.  No I don't think I explained
myself properly.  We have a lot of tests so when we run NUnit seems to spawn
off thread's (maybe from a system threadpool I guess), is this correct.

 

My problem is then when those threads get created but the test itself is
using a class with a ThreadStatic variable and either the [Setup] or
[TearDown] methods make a change to that threadstatic variable then they are
doing it in the main thread and not the spawned threads from nunit because
it's threadstatic.  This in turn gives problems for the tests.

 

Perhaps I'm not making myself clear, I'll attempt to draw it out or write a
bit of test code which replicates the problem if this is unclear.

 

Thanks,

 

Robbie

 

From: [email protected]
[mailto:[email protected]] On Behalf Of Charlie
Poole
Sent: 08 April 2008 17:29
To: [email protected]
Subject: Re: [Nunit-users] Threadstatic Problem

 

Hi Robbie,

 

If I'm understanding correcty, you are creating a thread in your test and
running some code on it. Since

NUnit has already run the setup at that point, there's not much to do about
it. If I'm misunderstanding,

please follow up and I'll try again.

 

Charlie


  _____  


From: [email protected]
[mailto:[email protected]] On Behalf Of
[email protected]
Sent: Tuesday, April 08, 2008 8:20 AM
To: [email protected]
Subject: [Nunit-users] Threadstatic Problem

We're seeing a strange problem with some unit tests to do with NUnit's Setup
and Teardown methods.  The global setup and teardown (TestFixtureSetUp,
TestFixtureTearDown) gets executed on the primary thread.  However if the
test case is run on a secondary thread the function level setup/teardown
(Setup, Teardown) are called on the primary thread. 

 

This is resulting (in what looks like) a problem with a [ThreadStatic]
attribute as the function level teardown can't touch it correctly because
it's on a separate thread.

 

Should the function level setup/teardown methods be called on the functions
thread and not the primary?  Or am I not understanding something properly
(likely J)

 

Thanks,

 


____________________________________________________________________
DISCLAIMER

This message and any attachments contain privileged and confidential
information intended for the use of the addressee named above. If you are
not the intended recipient of this message, you are hereby notified that any
use, dissemination, distribution or reproduction of this message is
prohibited. Please note that we cannot guarantee that this message or any
attachment is virus free or that it has not been intercepted and amended.
The views of the author may not necessarily reflect those of Realtime Worlds
Ltd.



Realtime Worlds Ltd is registered in Scotland, number 225628. Registered
Office: 152 West Marketgait, Dundee, DD1 1NJ.


____________________________________________________________________
This email has been scanned by the MessageLabs Email Security System


____________________________________________________________________
DISCLAIMER

This message and any attachments contain privileged and confidential
information intended for the use of the addressee named above. If you are
not the intended recipient of this message, you are hereby notified that any
use, dissemination, distribution or reproduction of this message is
prohibited. Please note that we cannot guarantee that this message or any
attachment is virus free or that it has not been intercepted and amended.
The views of the author may not necessarily reflect those of Realtime Worlds
Ltd.



Realtime Worlds Ltd is registered in Scotland, number 225628. Registered
Office: 152 West Marketgait, Dundee, DD1 1NJ.

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone

_______________________________________________
Nunit-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nunit-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.