Search The Blog
About this site

@RoyOsherove

Subscribe!

This site aims to connect all the dots of my online activities - from tools, books blogs and twitter accounts, to upcoming conferences, engagements and user group talks.

from 5whys.com
Twitter: @RoyOsherove
My Book: The Art of Unit Testing
Latest Posts
« Unit Test Tip: Encode test data as part of your test xml comments | Main | Cool site: Sound Search »
Monday
Jun252007

ThreadTester with new Ability: StopWhenTrue() and thread polling

I've updated The ThreadTester Library with a new ability: StopWhenTrue().

It allows you to periodically poll while all the threads are running (and the test is blocking) and either assert stuff or stop the run midway when some condition is true. 

Here's a test that shows how this is possible with the new API:

[Test]
public void StopWhenTrue_ReturnTrueWhenCountOver1000_AllThreadsStopImmediately()
{
 Counter c = new Counter();
 ThreadTester tt = new ThreadTester();
 tt.RunBehavior=ThreadRunBehavior.RunForSpecificTime;
 tt.AddThreadAction(delegate
             {
                for (int j = 0; j < 103; j++)
                  {
                                               c.Increment();
                                           }
                                           Thread.Sleep(50);
                                       });
 
 tt.StopWhenTrue(delegate
                  {
                    // you can also assert stuff here
                    Console.WriteLine("currently at " + c.Count);
                     return c.Count > 1000;
                  },100);// check every 100ms
 
          //this test will end in less than a second because of our added functionality
tt.StartAllThreads(10000); 
 Assert.Greater(c.Count,1000);
 Assert.Less(c.Count,1050);
}

PrintView Printer Friendly Version

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>