Freigeben über


CCR tips and tricks - part 4

Sometimes when working with causalities you want to clear all causalities. One common case where you want to do this is when you use a throttling port (more on that in a later post). Here is a simple test showing how to clear causalities (line 30).

  1: [TestClass]
 2: public class 
 3: Given_a_causality_when_unhandled_exception_is_thrown_after_clearing_causalities
 4: {
 5:     private DispatcherQueue dispatcherQueue = new DispatcherQueue();
 6:     private Exception causalityException = null;
 7:     private string exceptionMessage = "My unhandled exception";
 8:  
 9:     [TestInitialize]
 10:     public void Setup()
 11:     {
 12:         var mre = new ManualResetEvent(false);
 13:         Port<Exception> exceptionPort = new Port<Exception>();
 14:         Causality causality = new Causality(
 15:             string.Format("CausalityForTests: {0}", Guid.NewGuid()), 
 16:             exceptionPort);
 17:         Arbiter.Activate(dispatcherQueue, exceptionPort.Receive(
 18:             (e) =>
 19:             {
 20:                 causalityException = e;
 21:                 mre.Set();
 22:             }));
 23:         Dispatcher.AddCausality(causality);
 24:  
 25:         Arbiter.Activate(
 26:             dispatcherQueue,
 27:             Arbiter.FromHandler(
 28:                 () =>
 29:                     {
 30:                         Dispatcher.ClearCausalities();
 31:                         throw new Exception(exceptionMessage);
 32:                     }));
 33:         Assert.IsFalse(
 34:             mre.WaitOne(TimeSpan.FromSeconds(3)), 
 35:             "Causality should not get exception");
 36:     }
 37:  
 38:     [TestMethod]
 39:     public void Then_exception_is_handled_by_causality()
 40:     {
 41:         Assert.IsNull(causalityException);
 42:     }
 43: }