CCR tips and tricks - part 2

Apart from the utility mentioned in part one I use another utility to make sure my unit tests report failures in a good way. When using CCR in a MSTest process unhandled exceptions may be silently swallowed by the CCR framework unless you have a handler for that case. Before I started to use causalities to deal with unhandled exceptions I would only see a timeout failure but with today's utility you see a failure with the actual message instead (if used right). This implementation is intended to be used in a using statement since it raises any encountered errors when disposed. In future tips and tricks you'll see how it's used.

  1:     class CausalityForTests : IDisposable
 2:     {
 3:         private Exception exception = null;
 4:  
 5:         public CausalityForTests(DispatcherQueue dispatcherQueue)
 6:         {
 7:             Port<Exception> exceptionPort = new Port<Exception>();
 8:             Causality causality = new Causality(
 9:                 string.Format("CausalityForTests: {0}", Guid.NewGuid()), 
 10:                 exceptionPort);
 11:             Arbiter.Activate(
 12:                 dispatcherQueue, 
 13:                 exceptionPort.Receive(e => this.exception = e));
 14:             Dispatcher.AddCausality(causality);
 15:         }
 16:  
 17:         public void Dispose()
 18:         {
 19:             if (this.exception != null)
 20:             {
 21:                 throw this.exception;
 22:             }
 23:         }
 24:     }