var blog = new ThoughtStream(me); RSS 2.0
 Monday, September 29, 2008

At some point, you will need to put a timer into your C# code. Hopefully you're doing Test Driven Development, or at least unit testing, to cover your need for a timer with a unit test. If you are - I'm betting you ran into some really ugly problems like tests that would not pass without having 'Thread.Sleep(1000)' calls in them. Worse yet - last week, I put a 3 second timer into a class that was being unit tested. This one mistake cause my CruiseControl build to go from 2 minutes, to timing out at 90 minutes because NUnit was keeping the app domain alive while the timer was alive, so it never closed the test run. Oops.

To fix this problem, we decided we needed to eliminate the use of an actual timer and use an abstraction that could be mocked/controlled in our unit tests. Unfortunately, the .NET Framework does not have an actual abstraction for a timer. So, if you need to use a timer in your code - and at some point in time, you will - you'll want to create a very simple ITimer interface that would let you control when the timer elapses and fires its registered action.

Here's the core of the abstraction that we came up with, initially.

public interface ITimer
{
    void Start(Action timerAction);
}

With such a simple interface in place, we can now mock it out in our unit tests (I like Rhino Mocks) and set up an expectation on the Start method to capture the timerAction parameter, then simulate the timer elapsing by calling the timer action from our unit test. Here's an example test and 'system under test' to illustrate how we use this interface.

[Test]
public void DemonstratingHowToUnitTestATimerElapsing()
{
    Action timerElaspedAction;
    ITimer timer = MockRepository.GenerateMock<ITimer>();
    timer.Expect(t => t.Start(null)).IgnoreArguments().Callback(
    delegate(Action timerAction)
    {
        timerElapsedAction = timerAction;
        return true;
    });
 
    MySystemUnderTest sut = new MySystemUnderTest();
    sut.StartMonitoringStuff(timer);
 
    //here's where we simulate the timer elapsing
    timerElapsedAction();
 
    Assert.IsTrue(sut.TheTimerActionWasCalled);
}
 
public class MySystemUnderTest()
{
    public bool TheTimerActionWasCalled { get; set; }
 
    public MySystemUnderTest()
    {
        TheTimerActionWasCalled = false;
    }
 
    public StartMonitoringStuff(ITimer timer)
    {
        timer.Start(() => TheTimerActionWasCalled = true);
    }
}
 

There is a bit more detail to the actual ITimer interface, and the implementation, though. We ran into issues where we needed to stop the timer, prevent it from firing after we close the SUT, etc. So, all said and done, here's the full ITimer interface and MyTimer implementation that we ended up with.

public interface ITimer
{
   void Start(Action action);
   void Stop();
}
 
public class MyTimer : ITimer, IDisposable
{
 
   private TimeSpan _timerInterval;
   private Timer _timer;
 
   private Action _timerAction;
 
   private bool IsRunning { get; set; }
 
   public MyTimer(TimeSpan timerInterval)
   {
       _timerInterval = timerInterval;
   }
 
   public void Dispose()
   {
       StopTimer();
   }
 
   public void Start(Action action)
   {
       _timerAction = action;
       IsRunning = true;
       StartTimer();
   }
 
   public void Stop()
   {
       IsRunning = false;
       StopTimer();
   }
 
   private void StartTimer()
   {
       _timer = new Timer(o => Timer_Execute(), null, 0, Convert.ToInt32(_timerInterval.TotalMilliseconds));
   }
 
   private void StopTimer()
   {
       if (_timer != null)
       {
           _timer.Change(Timeout.Infinite, Timeout.Infinite);
           _timer.Dispose();
           _timer = null;
       }
   }
 
   private void Timer_Execute()
   {
       try
       {
           StopTimer();
           _timerAction();
       }
       finally
       {
           if (IsRunning)
               StartTimer();
       }
   }
 
}

This has worked out very well for us, so far. It lets us put a timer in place when we need one, but not worry about thread racing issues, long running tests, etc.

Monday, September 29, 2008 7:38:33 PM (Central Standard Time, UTC-06:00)  #    Comments [2]. Trackback 
Tags: .NET | Lambda Expressions | NAnt | Rhino Mocks | Test Driven Development | Unit Testing

 Friday, August 08, 2008

In addition to our 'Code Review Challenge' that I discussed recently, we have been using another retrospective game - 'Name That Standard'.

The idea is fairly simple. At the beginning of any retrospective, we go around the room and ask each team member to list out a standard that we are using in our project. This can be any standard that anyone feels the team is using or needs to be using, including coding conventions, architectural patterns, business and project management processes, communication means, etc. The intention is similar to the intent of 'The Code Review Challenge' - socialization of the the system. Only in this case, we are applying the term 'the system' at a much higher level - our organizational and project management / implementation system as a whole.

Some thoughts on running 'Name That Standard':

  • Require not only naming of the standard, but accurate description of the standard and an example of where it is used. Alternatively, you may want to list a brief description out on a projector or whiteboard and see who can name the standard being described.
  • Encourage the team to think about the team as a whole, not just their area of responsibility.
  • Encourage the team to list standards that they don't understand, so that the rest of the team can chime in with assistance and help the team member in question grow
  • Don't just let people yell out standards. go around the room, one person at a time. we got a little chaotic at first, and it was hard to know who said what.
  • Encourage people to ask questions like 'i thought we were using this standard, but i saw someone else doing that standard'
  • Encourage the standards to become more and more low level and detailed, over time (over multiple iterations)
  • Encourage the standards to evolve and change for the better, over time (over multiple iterations)

In our implementations of this game, we've managed to get a fairly good list of standards written down and also resolved some questions on what standards we actually are using. For example, we had 3 dynamic mocking frameworks in our code base - Rhino Mocks, Moq, and a home grown one that we lovingly called 'BrandoMocks' (named for our team member, Brandon, who wrote it for fun one night). The end result of that discussion was to throw out Moq and BrandoMocks, adopting Rhino Mocks as our standard for this project.

Having done 'Name That Standard' in two consecutive iteration retrospectives, I think I can say that it has been very successful. The team as a whole is becoming more and more aware of the standards that we are using, and the individual members are beginning to contribute more and more to the standards.

With each passing iteration, and each new technique for creating collective ownership and socializing the system, I see the team coming together and really forming a team vs. a bunch of individual developers working on the same system.

Friday, August 08, 2008 7:58:10 AM (Central Standard Time, UTC-06:00)  #    Comments [0]. Trackback 
Tags: Agile | Code Reviews | Community | Management | Principles and Patterns | Retrospectives | Rhino Mocks | Standardized Work

 Tuesday, July 15, 2008

In the last month or two, I have been hand coding a lot of mock and stub objects and it has become a nightmare to manage. My primary reason for doing this by hand was that Rhino Mocks 3.4 and older did not fit with the BDD style unit tests that I was writing. Yes, I made it work in a few places, but it was ugly and annoying.

Fortunately, Ayende has cleaned it all up with the new syntax and made it very easy to assert that individual expectations were met, with v3.5. I finally got around to trying out the new syntax today, and I immediately fell in love with it. For example:

[Concern("User Administration")]
public class When_accessing_the_system_as_a_non_administrator : ContextSpecification
{
    private IMainView view;
 
    private IMainView GetView()
    {
        IMainView mockView = MockRepository.GenerateMock<IMainView>();
        mockView.Expect(v => v.EnableUserManagement()).Repeat.Never();
        mockView.Expect(v => v.DisableUserManagement()).Repeat.Once();
        return mockView;
    }
 
    protected override void Context()
    {
        User administrator = new User();
        administrator.IsAdministrator = false;
        CurrentUser.User = administrator;
 
        view = GetView();
        new MainPresenter(view);
    }
 
    [Observation]
    public void Should_not_display_the_User_Management_option()
    {
        view.AssertWasNotCalled(v => v.EnableUserManagement());
    }
 
    [Observation]
    public void Should_hide_the_user_management_option()
    {
        view.AssertWasCalled(v => v.DisableUserManagement());
    }
 
}

In the "GetView" method, I am setting up two very distinct expectations on my mock object.

  1. Never Call the EnableUserManagement method.
  2. Call the DisableUserManagement method once.

With the new Rhino Mocks syntax, I can easily verify that each one of these expectations was called via my "should" observations.

The "AssertWasNotCalled" extension method verifies that an expectation of Repeat.Never was setup and that the method was not called.

mockView.AssertWasNotCalled(v => v.EnableUserManagement());

And the "AssertWasCalled" extension method verifies that the expectation was to call the method, and that the method actually was called.

mockView.AssertWasCalled(v => v.DisableUserManagement());

I like it. The new syntax has really simplified my Context Specification testing.

Tuesday, July 15, 2008 2:43:10 PM (Central Standard Time, UTC-06:00)  #    Comments [1]. Trackback 
Tags: .NET | Behavior Driven Development | Lambda Expressions | Rhino Mocks | Unit Testing | User Stories

Navigation
About Me
View Derick Bailey's profile on LinkedIn

Send mail to the author(s) Contact Me
Archive
<December 2008>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Derick Bailey
Sign In
Statistics
Total Posts: 91
This Year: 91
This Month: 0
This Week: 0
Comments: 40
Themes
Pick a theme:
All Content © 2008, Derick Bailey
DasBlog theme 'Business' created by Christoph De Baene (delarou)