Skip to content

Be careful using ThreadStatic in Visual Studio unit tests

Given is a ThreadStatic field which is set to empty in the static constructor, and set by a public instance method:

    public class TestClass
    {
        [ThreadStatic] 
        private static string threadStaticValue;
        static TestClass()
        {
            threadStaticValue = "";
        }
        public void SetThreadStaticValue(string value)
        {
            threadStaticValue = value;
        }
        public string GetThreadStaticValue()
        {
            return threadStaticValue;
        }
    }

Now two unit tests check whether the threadStaticValue is set correct.

    [TestClass]
    public class ThreadStaticTest
    {
        [TestMethod]
        public void WithThreadStaticValueSetTest()
        {
            Debug.WriteLine("Thread id: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
            TestClass tc = new TestClass();
            tc.SetThreadStaticValue("foo");
            Assert.AreSame(tc.GetThreadStaticValue(), "foo");
        }
        [TestMethod]
        public void WithThreadStaticValueNotSetTest()
        {
            Debug.WriteLine("Thread id: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
            TestClass tc = new TestClass();
            Assert.AreSame(tc.GetThreadStaticValue(), "");
        }
    }

Those tests will fail(good) or pass(bad) depending of whether the test engine will reuse the same thread.

Executed a second time, the result could be different.

Solution:

  1. Don't use ThreadStatic at all
  2. Make sure the ThreadStatic fields are initialized correctly before using them

Trackbacks

Comments

Display comments as Linear | Threaded

Do My Essay on :

Wonderful content! This is precisely what I was searching for, and it will be valuable later on. Much obliged!

SI kaise bane on :

Thanks to this article I can learn more. Expand your knowledge and abilities.

pay for essay writing on :

To read this blog and some other blogs in the category of music we can make it a creative and ideal blog for the readers. There are some people who are interested to not reads this blog. But blog is so good.

7.49 godaddy domain renewals coupon on :

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!

samsung s3 mini on :

Get the best deal for Samsung Battery for Galaxy S III Mini

topic for education essay on :

Thanks for this valuable ThreadStatic field. It has been something that I have been looking for a few hours. Thanks!

1998yes on :

is one of the leading and famous womens online shopping store

Essay Writing Services on :

Read this weblog and some other blogs in the category of music we will make it a creative and best weblog for the readers. There are some people who are interested in no longer reads this web publication. However, the blog is so excellent.

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Markdown format allowed
Form options