Skip to main content

Posts

Showing posts from November, 2012

Unit testing Rx methods Timeout & Retry with moq

Earlier this week I was trying to unit test an asynchronous service (Foo) which used another asynchronous service (Bar) internally and ran into an issue trying to mock out the Bar service so that it would cause the retry & timeout schedules to fire. Bar is defined as follows, the implementation is irrelevant as it being mocked for the tests: 1: public interface IBarService 2: { 3: IObservable<Unit> Generate(); 4: } Foo is similarly defined: 1: public interface IFooService 2: { 3: IObservable<Unit> Generate(); 4: } The implementation of the Foo service is the important part, it uses the Boo service to generate a value, it's expected to generate the value or Timeout, if it fails to generate a value (for what ever reason) it's expected to to Retry: 1: public class FooService : IFooService 2: { 3: private readonly IBarService _barService; 4: private readonly IS

Observable.Timer throws ArgumentOutOfRangeException

We found this one out during testing in different time zones earlier this - if your app is going used in different time zones avoid using DateTime with  Observable.Timer . In fact I guess the general message should be avoid DateTime all together in any apps use DateTimeOffset instead... This code works from here in London but when run in Hong Kong throws an exception: 1: static void Main( string [] args) 2: { 3: var now = DateTimeOffset.Now; 4: Console.WriteLine( "Time zone offset: " + now.Offset); 5:   6: var scheduler = NewThreadScheduler.Default; 7:   8: using (Observable.Timer(DateTime.MinValue, TimeSpan.FromSeconds(1), scheduler) 9: .Subscribe(Console.WriteLine)) 10: { 11: Console.ReadLine(); 12: } 13: } London time zone shows output as expected: Where as when run for a time zone set to anything positive of London (offset = 0), e.g. Hong Kong