DevLabs > DevLabs Forums > CHESS – Find and Reproduce Concurrency Heisenbugs > Why does this test with an instance int pass but this test with a static int fail?
Ask a questionAsk a question
 

AnswerWhy does this test with an instance int pass but this test with a static int fail?

Answers

  • Monday, August 31, 2009 5:28 AMTom BallMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Because, CHESS runs the test method multiple times and each test method executes in the context of a new instance of the class. So, when you use a static variable, (default) initialization is performed just once.  With an instance variable, (default) initialization takes place for each execution of the test method. Make sense?  To make the assert pass in the case of the static variable, you should record the value of the static variable at the beginning of the test method in another variable (oldValue), and asset that the final value of _staticTotal is equal to 2+oldValue.  


    -- Tom

All Replies

  • Monday, August 31, 2009 5:28 AMTom BallMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Because, CHESS runs the test method multiple times and each test method executes in the context of a new instance of the class. So, when you use a static variable, (default) initialization is performed just once.  With an instance variable, (default) initialization takes place for each execution of the test method. Make sense?  To make the assert pass in the case of the static variable, you should record the value of the static variable at the beginning of the test method in another variable (oldValue), and asset that the final value of _staticTotal is equal to 2+oldValue.  


    -- Tom

  • Monday, August 31, 2009 6:55 AMNeil_J Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Tom. That makes sense.

    Here it is working: