Saturday, May 18, 2019

TDD: How are you testing your callbacks in a pub/sub pattern?

I'm ramping up on TDD and created a basic test first. The issue I need help with is creating a test that verifies that a callback occurred and there is a specific data type in the callback.

The test I created looks like it should work, but when I run the test in debug mode the Test thread exits after setting the event handler. The WaitOne() call is never reached.

I was expecting the thread to remain alive in the Test method and wait at the WaitOne() call until the program under test fires the event using a Threadpool thread. How are you testing your callbacks? Any ideas on where I went wrong?

[TestFixture] public class Harvester { [Test] public void HarvestJobSites() { var waitTime = (int)TimeSpan.FromHours(2).TotalMilliseconds; var callbackEvent = new ManualResetEvent(false); bool isUpdated = false; var indeedHarvester = new IndeedJobHarvester(); indeedHarvester.Start(); indeedHarvester.OnCompletion += delegate(JobHarvestCompletionArgs args) { isUpdated = true; callbackEvent.Set(); Assert.IsNotNull(args.Jobs); Assert.That(args.Jobs.Count, Is.GreaterThan(0)); Assert.IsTrue(args.Jobs[0] is IndeedJob); }; if(!isUpdated) callbackEvent.WaitOne(waitTime); } } 
TDD: How are you testing your callbacks in a pub/sub pattern? Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team