Tag Archives: cars

Easily Unit Testing Event Handlers

In C#, If you need to unit test a class that fires an event in certain circumstances (perhaps even asynchronously), you need to handle a little more than just running some code and doing the assertion. You have to make sure your unit test waits for the event to be fired. Here’s one naive way of doing it, a WRONG way:

   1: private bool statsUpdated = false;
   2: private ManualResetEvent statsUpdatedEvent = new ManualResetEvent(false);
   3:
   4: [Test]
   5: public void CheckStats()
   6: {
   7:     BrickDatabase db = new BrickDatabase(tempFolder, maxCacheAge);
   8:
   9:     statsUpdated = false;
  10:     statsUpdatedEvent.Reset();
  11:
  12:     db.InventoryStatsUpdated += new EventHandler(db_InventoryStatsUpdated);
  13:     db.DoSomethingThatFiresEvent();
  14:
  15:     statsUpdatedEvent.WaitOne();
  16:
  17:     Assert.IsTrue(statsUpdated);
  18: }
  19:
  20: void db_InventoryStatsUpdated(object sender, EventArgs e)
  21: {
  22:     statsUpdated = true;
  23:     statsUpdatedEvent.Set();
  24: }

There are a number of things wrong with this:

  1. The class variables. More complex unit test class. Have to coordinate these variables across multiple functions.
  2. Since they are class variables, you will want to reuse them, but you’d better remember to reset the event and the boolean every time!
  3. Have to have two functions to do something really, really simple.
  4. The WaitOne() does not have a timeout, so if the wait is ever satisfied then statsUpdated is guaranteed to be true.

Here’s a better way of doing it, using anonymous methods in C# 2.0:

   1: [Test]
   2: public void CheckStats()
   3: {
   4:     BrickDatabase db = new BrickDatabase(tempFolder, maxCacheAge);
   5:     bool statsUpdated = false;
   6:     ManualResetEvent statsUpdatedEvent = new ManualResetEvent(false);
   7:
   8:     db.InventoryStatsUpdated += delegate
   9:     {
  10:         statsUpdated = true;
  11:         statsUpdatedEvent.Set();
  12:     };
  13:
  14:     db.DoSomethingThatFiresEvent();
  15:
  16:     statsUpdatedEvent.WaitOne(5000,false);
  17:
  18:     Assert.IsTrue(statsUpdated);
  19: }

Improvements?

  1. The event is just part of the method. Since the event handler is an anonymous delegate, it can access the enclosing method’s local variables.
  2. Added 5,000ms timeout to the WaitOne() function to prevent hanging of unit tests.

Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

Cars and the Idiots who Own Them – Part II

Well, I took it in this morning for a full checkup. I should preface this with by saying that for the last month, and especially the last week or so, I’ve been feeling strongly that I needed to take the car in for a serious look. Things just felt different–and not better.

I left it this morning at 8, and at 11 they had a diagnosis–made worse by the fact that my Honda Care extended warranty had run out a few months ago. Basically, new CV joints, new engine mounts, oil change (I just tacked that on), and something else that was fairly expensive.

The mechanic told me that otherwise the car is in great shape. The good news is that this kind of work won’t need to be done for a while.

I should be leaving to pick it up in about 45 minutes or so…


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

Cars and the Idiots who Own Them

Yes, I’m talking about me.

This morning, we had a big day planned: food shopping, checking out an international market, and having some guests this evening. We decided to go to the international market first1. We had heard they had really cheap produce and were not disappointed. We picked up quite a bit of greens and herbs and were on our way. We put them in the cooler to survive the trip to the next store, I put the keys in the ignition, and…

Nada.

The key turned all the way around the ignition, without turning on the car, and it wouldn’t come out! We tried for 15 minutes with the awful thing, every gimmick we’d ever heard of for stuck keys. Then we called our insurance company and they sent a tow truck–Estimated Time of Arrival: 2 hours.

Off and on for an hour I tried jiggling and juggling and haggling and huggling. Finally, after an hour, I took off the steering column’s plastic wrapper and looked at the ignition itself–it seemed a part was loose…just need to push it in…suddenly, it turned and locked and came out and went in and ignited! I canceled the tow truck, and drove straight to the dealership.

The thing is, I was going to take it in next Saturday for a full checkup no matter what anyway. Now I have an appointment for Monday morning to do that and fix the ignition. Skip work or rent a car? It’s hard to make up work when I have night classes. I might have to rent a car if mine needs serious work anyway.

Computers I can handle (usually!), but cars are another thing entirely. We ended up going to CostCo near where we live, instead of the much cheaper (and farther) Sam’s Club, but we made it home in one piece–and now we have yet another “theft deterrent.” 😉


1 – Grand Mart International Food, 6255 Little River Turnpike, Alexandria, VA.


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order: