Skip to main content

Posts

Showing posts with the label Reactive Extensions

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 r...

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. H...

Using PostSharp for AOP with Reactive Extensions

I'm currently working on a project with @HamishDotNet  & @LordHanson where we make heavy use of Rx (Reactive Extensions) for processing streams of data which are generated asynchronously. We have multiple pipeline processes based around an Rx stream, they look something like this: The method Sequence returns an instance of IObservable<T>  which is then acted upon by 4 or 5 methods before the Subscriber is called, some of these methods might mutate the state along the way. The important part is the idea of the pipeline to process the Rx stream as it is generated by the asynchronous Sequence method. So what I wanted to do is log what's going on - how long each step takes (in the pipeline) as well as how long the Rx stream is alive and when the stream generates a value. The issue we have is we don't want to overly modify the code just support such logging\telemetry. I don't want to end up with something like this: Don't get me wrong, if I had only ...