Jaa


Saying no to a sleep statement in our automation

Last week I had a code review come across my plate that looked like this:

  Section.AddPage();

And this line was being added below the previous line:

  System.Threading.Sleep(5000);

  //more code here to add content to the page

Pretty simple code, really. We use the Section class our test code has to add a page to the section. Then the new line was being added due to some instability. Our tester had investigated and found that in some cases, our call to New Page would not be completely done before she tried to add content to the page, and in those cases, the test would fail.

So her fix was to give the test 5 seconds (5000 milliseconds) to sleep while the new page was created in OneNote.

I did not approve the code review for 2 reasons.

  1. I'm pretty sure that 5 seconds would almost always work, let's say 99.5% of the time. But we want 100% reliabilty and if the tester who wrote this has luck like mine, the machine the test runs on next will need 5.1 seconds. Or 6, or 9, or some amount of time greater than the time coded here.
  2. There was no check to see that the new page was created. This kind of goes hand in hand with my first reason.

So what I am pushing for here is a routine like this:

  1. Get a count of the number of pages in the current section
  2. Add the new page
  3. Get the count of the pages again
  4. If the count has not gone up by 1
    1. Log it (always log what state the test is in)
    2. Then you can sleep for a small interval, say 500 milliseconds
    3. If the total time here is greater than some threshold, say 10 seconds, you can think about failing the test. If OneNote can't create a page in 10 seconds, something is likely wrong elsewhere and needs to be investigated…
    4. Finally, repeat this loop at step 4
  5. If the count has gone up by 1, log and and then move on to the rest of the test

Since we do not have an event driven object model, little loops like this can help our tests remain stable.

Questions, comments, concerns and criticims always welcome,

John

Comments

  • Anonymous
    August 07, 2012
    How about trying Event driven synchronization? Is that not an option in your case?

  • Anonymous
    August 07, 2012
    That is exactly what I want.  But since we use our own OM based on our xml API (this www.codeplex.com/onom) we have to add the events ourselves and have not had time to do that yet.

  • Anonymous
    August 08, 2012
    Even is that case a polling mechanism for synchronization is not much better than a static sleep.