Isolator trick - Unit Testing ThreadPool.QueueUserWorkItem()
Unit testing asynchronous stuff is tough. This is something I had to live with for the last few days working on one of our products. This is compounded by having to unit test jobs that go through ThreadPool.QueueUserWorkItem() - because it's using the ThreadPool it's hard to synchronize your test. If you just do this:
1: [Test]
2: public void TestAsyncStuff()
3: { 4: bool setInBackground = false;
5:
6: ThreadPool.QueueUserWorkItem(o => {setInBackground = true;}); 7:
8: Assert.IsTrue(setInBackground);
9: }
Your test will almost always succeed. But then again it just might fail - there's a race condition between the assertion and the background thread on setInBackground. This is of course a hugely simplified case – usually the call to QueueUserWorkItem() is hidden somewhere in the code under test where it does time consuming calculations and not just setting boolean values. These race conditions may cause nastier results than a simple dirty bool, and may be quite hard to debug.
Now, it's our responsibility to synchronize our tests - always make sure everything in the code under test finished running before asserting against it. But how does one synchronize the ThreadPool? We all know how to synchronize a Thread, right? Just .Join() on it and you're good. How about we turn the mighty ThreadPool into a puny Thread using Isolator?
First, we need to refactor the call to ThreadPool.QueueUserWorkItem() to a method we can fake - unfortunately, we can't fake it directly because it's in mscorlib. Let's say we did it like this:
1: public class ThreadingWrapper
2: {3: public void QueueAsyncJob(Action<object> job)
4: {5: var callback = new WaitCallback(job);
6:
7: ThreadPool.QueueUserWorkItem(callback);
8: }
9: }
Now, using DoInstead() we turn the call to QueueAsyncJob to a call to a Thread. This way we maintain the asynchronous nature of the code under test, and maintain the capability to synchronize our test without jumping through hoops. For instance:
1: [Test, Isolated]
2: public void TestAsynchStuff()
3: {4: bool setInBackground = false;
5: Thread backgroundThread = null;
6:
7: Isolate.WhenCalled(() => ThreadingWrapper.QueueAsyncJob(null))
8: .DoInstead(context =>
9: {10: var job = context.Parameters[0] as Action<object>;
11: backgroundThread = new Thread(() => job(null));
12: backgroundThread.Start();
13: });
14:
15: ThreadingWrapper.QueueAsyncJob(o => {setInBackground = true;});16:
17: backgroundThread.Join();
18:
19: Assert.IsTrue(setInBackground); // if we reached this we can be sure the asynchronous code finished running
20: }
Mission accomplished! Lets do a quick recap – we need to test code that uses ThreadPool.QueueUserWorkIteam(). This is not fun because we can’t easily wait for all worker threads to finish, and it’s hard to synchronize our tests that way. Solution – we switch from using ThreadPool to using a regular Thread! How? extract the call to ThreadPool into a method, and when that method is called, replace its implementation (using the wonderfully flexible DoInstead()) with a simple thread creation and invocation. Before you assert on the test’s result, just call Thread.Join() and you’re good!
Interview with Uncle Bob coming shortly
Last week I recorded an interview with “Uncle Bob” Martin of ObjectMentor. We talked about craftsmanship, unit testing and other cool stuff. The video is now in editing, but it will be up this week. Stay tuned…
Introducing Typemock Test Lint
Earlier today we released a new product unto the unsuspecting world – Test Lint is its name.
We believe in unit testing and test driven development, and we also know how hard it is to learn to write proper unit tests that are readable, maintainable and trustworthy. That’s why we created Test Lint – the world’s first and only coding advisor for unit tests in visual studio 2010.
What does it do?
Test Lint parses your code as you type it, and looks for common problems in your unit test code – from missing asserts to having tests depend on other tests – Test Lint will notify you on the spot about each possible issue with a visible queue right inside your editor, right next to the link where the issue appears.
The Art of (Good) Unit TestingWriting unit tests is easy. Writing good unit tests – that are readable, maintainable and trust-worthy is a bit harder if you’ve never done it before. So we took Roy Osherove (author of The art of unit testing) and asked him to write down, based on his massive experience with unit testing, common problems that people do when they first start unit testing. Then we put that knowledge inside a visual studio 2010 extension– it’s like having your own personal coach letting you know of problems as you type them, really.
What issues does it detect?Currently Test Lint finds a very short but also very common set of problems:
· Missing asserts in your tests
· Multiple asserts \verifications on different objects in the same test
· Tests that invoke other test methods
· Logic inside unit tests (loops, Ifs,Switchs..)
More rules will be added soon, and in upcoming versions, you will even be able to write your own rules based on our parsing infrastructure.
Works with all major frameworksTest Lint will detect issues in tests written with:
· Microsoft Test Framework
· NUnit
· MbUnit
· XUnit.NET
· CsUnit
Getting StartedTo get started with Test Lint:
· Download the latest installation package (in vs 2010 package form)
· Double click on the package to install into visual studio 2010 (RC and upwards)
· If Vs 2010 is already running, you should restart it
· Now that it’s installed and running, just open existing tests code, or start writing new test code.
· Since this is a public beta, we don’t want you to use a non stable version for too long. You’ll be asked to download a new version within 30 days of the first usage.
Coming real soon – Typemock Test Lint
Later today we will be releasing Typemock Test Lint – a free extension to vs 2010 that helps find common issues with unit test code – such as missing asserts, too many asserts, tests calling other tests and more.
Oh, and you’ll be able to write your own rules for it as well (not just for tests!)
hang on – just an hour or two longer!
PS – why is it called Test Lint?
This Week In Testing – Ep 14 The Google Buzz Fail
Yeah, we're late, and maybe we have some technical difficulties, but nothing will stop us! Except maybe Google who we trash a little.
Have fun!
And the final winners are…

Final drawing winners are James Raden (@sidecut) and Marcus Tagebuch (@kampenhaus). Congratulations to the winners!
Although this is the final Isolator drawing, stay tuned. There’s more to come, and even more impressive than this…
Let’s make it a double
It’s been 4 weeks in a row that we have been giving away FREE ISOLATORS every Wednesday to one happy twitter follower.
This week - Wednesday the 24th - will be the final drawing and we will give the opportunity to experience Easy Unit Testing, to 2 happy followers. Yes, to 2 HAPPY WINNERS…ONE DOUBLE DRAWING.
All you need to do is
- follow us on twitter @Typemock
- Twit from your personal twitter : just entered for a chance to win a free Isolator @typemock RT it to enter the double drawing.
Good Luck
And the winner is ….
Congratulations to Daniel May, Surry, UK that just won a free Isolator …..
Welcome to the unit testing family Daniel… and for all the rest we are having one more drawing next week so you may start RT about it..
we actually had the drawing during today's podcast and you will be able to see it on Sunday in our YouTube account and Typemock TV.
Daniel:
“I'm excited to try it out now - will really speed up my testing! Cheers - I'll be rabbiting on about it for days to my followers I'm sure :)”
See you @typemock
Last Chance To Enter Today’s Drawing
We’re back with our weekly drawing for a free Isolator 2010. Don’t forget – follow us on Twitter (@typemock), re-tweet to all your followers from your account about the upcoming drawing, and one of you will win a free license.
We will notify the happy winner first and then will publish their name here and in our twitter account.
Good luck!
@avi_kaye
Valentine's Day Presents!
In honor of Valentine's Day, and the importance of spending less time unit testing and more time living, Typemock is extending the very special promotion.
Continuing On February 16th,
we will give one free Isolator 2010 license with every purchase of four.
There is no limit, so if you order four, you get 1 free. You order 400, you get 100 free. You get the idea.
More details?! @typemock
Typemock Isolator 2010 version 6.0.1 is released
This version is a maintenance update that resolves a performance issue that crept into version 6.0. This issue mostly affected code with a lot of event handling. From pre-release user reports we are seeing x10 performance improvement in event-related code, so you should definitely check this out!
As always, you can get the new version from our download page.
Links: Being Successful with Agile and unit testing
It is always nice to hear when a fellow developer succeeds and what makes these posts even better is the fact that they also contain valuable tips as well.
Lessons learned from an Agile project (David Tchepak):Dave^2 writes about an agile project he’s been part of for the last 18 months - and he has a lot to tell you about it. They’ve been doing XP and Scrum using 2 week iterations and as far as the post goes it was great.
The post explains about several points that attributed to the project success:
- Short user stories
- Momentum
- Feedback
- Acceptance testing
- Do what works
- End-to-End as fat as possible
But don’t take my word for it – read about it in his blog.
Six Months Down The Line – Unit Testing (Isaac Abraham)Isaac project is getting close to its first release and after six months of practicing TDD (Test Driven Development) – he has some insights on unit testing and how to make them work for you.
This is the first project he’s been using TDD and unit testing all the way and as far as I can tell from the post it went great -no trivial task.
He has a few tips on how to succeed with unit testing such as “keep your test as granular as possible” and “keep your compile time down”.
The post has a nice overview of the tools used along with Isaac’s experience with them.
All in all a good post to read if you’re interested in reading the lessons learnt from a successful TDD first timer.
And the winner on Today’s Drawing Is…
Jeffrey Ferguson
Just won an Isolator 2010 in our Weekly Drawing, Congrats and welcome to the Unit Testing Family,
“Thank you very much! I am anxious to try it out.”
@jeffFerguson
You still have a chance to win, as mentioned every Wednesday this month a new @Typemock Twitter Follower will win a free license
Stay Tune …
Congrats to Jeffery we would love to hear how are you enjoying the Isolator 2010
February Newsletter – Happy Valentine!


Typemock Isolator 2010 Launch 

With the upcoming launch of Microsoft’s Visual Studio 2010, and with so many requests from our community for Visual Studio 2010 support, we’re proud to announce the launch of Isolator 2010, packed with new features and ready for .Net 4.0 action.
Unit testing doesn’t get much easier than this. Typemock continues to innovate unit testing with the latest version of Isolator 2010, which pushes the boundaries even further. With automatic code completion, the ability to fake the new .Net 4.0 dynamic types and full Visual Studio 2010 integration (yes, all Isolator features work in VS2010), it’s a must have for all professional developers.
Valentine's Day Presents!
In honor of Valentine's Day, and the importance of spending less time unit testing and more time living, Typemock is offering a very special, but very brief promotion. On February 14th and 15th,
we will give one free Isolator 2010 license with every purchase of four.
There is no limit, so if you order four, you get 1 free. You order 400, you get 100 free. You get the idea.
More details will be posted on our blog
Every Wednesday in February one of our twitter followers will win a free Isolator 2010 license.
Throughout the month, we will continue to announce new sweepstakes and promotions. Just last week we gave the first 10 people that tweeted back on our T-shirt promotion free "Legalize Unit Testing" t-shirts, including free shipping. So please check our blog regularly for details and follow us on Twitter @typemock.
A few of our customers: 

This week in unit testing – episode 12
Last Chance to Enter today’s Drawing
Hi GUYS,
We are about to to start our weekly drawing for a free Isolator 2010, Please take a minute to make sure that you are following @Typemock on twitter and that you and that you re-tweet from your account about the upcoming Isolator Drawing… We will notify the happy winner first and then will publish their name here and in our twitter account
Stay Tuned
Good Luck
“This Week In Testing” Episode 12 (Part 2): The Goals of Unit Testing
Well, if you haven’t gotten a taste of our new setup, take a look at it, this time in part 2. Don’t forget to also check out part 1 (as if you didn’t already).






