public async Task CanSendCommandsToMultipleHandlersOnDifferentContexts(int nContexts)
        {
            // arrange
            var subscriptionStore  = new LocalSubscriptionStore();  // shared store
            var subscriptionBroker = new LocalSubscriptionBroker(); // shared broker
            var transportFactory   = new LocalTransportFactory();   // shared transport

            var syncs    = new CountdownEvent[nContexts];
            var contexts = new IDomainContext[nContexts];

            for (var i = 0; i < nContexts; ++i)
            {
                var idx = i;
                syncs[i]    = new CountdownEvent(2);
                contexts[i] = DomainContext.Configure()
                              .Bus(b =>
                                   b.WithCustomSubscriptionStore(subscriptionStore)
                                   .WithCustomSubscriptionBroker(subscriptionBroker)
                                   .WithCustomTransportFactory(transportFactory)
                                   .WithProcessor(p =>
                                                  p.Endpoint("p" + idx)
                                                  .RegisterHandler(new TestCommandHandler(cmd => syncs[idx].Signal())
                                                                   ))
                                   )
                              .Start();
            }

            try
            {
                // doesnt really matter which ctx sends the messages, they know about eachother subscriptions
                var ctx1 = contexts.First();
                var ctx2 = contexts.Last();

                await ctx1.CommandBus.Send(new Command <DoSimple>("cmd1", new DoSimple {
                }));

                await ctx2.CommandBus.Send(new Command <DoComplex>("cmd2", new DoComplex {
                }));

                foreach (var sync in syncs)
                {
                    sync.Wait(TimeSpan.FromSeconds(2));
                }
            }
            finally
            {
                foreach (var context in contexts)
                {
                    context.Dispose();
                }
            }

            // assert
            foreach (var sync in syncs)
            {
                // asserts all messages were received by each processor
                Assert.AreEqual(0, sync.CurrentCount);
            }
        }
Exemple #2
0
        public async Task CanSendCommandsToMultipleHandlersOnDifferentContexts(int nContexts)
        {
            // arrange
            var subscriptionStore = new LocalSubscriptionStore(); // shared store
            var subscriptionBroker = new LocalSubscriptionBroker(); // shared broker
            var transportFactory = new LocalTransportFactory(); // shared transport

            var syncs = new CountdownEvent[nContexts];
            var contexts = new IDomainContext[nContexts];

            for (var i = 0; i < nContexts; ++i)
            {
                var idx = i;
                syncs[i] = new CountdownEvent(2);
                contexts[i] = DomainContext.Configure()
                                         .Bus(b =>
                                             b.WithCustomSubscriptionStore(subscriptionStore)
                                              .WithCustomSubscriptionBroker(subscriptionBroker)
                                              .WithCustomTransportFactory(transportFactory)
                                              .WithProcessor(p =>
                                                 p.Endpoint("p" + idx)
                                                  .RegisterHandler(new TestCommandHandler(cmd => syncs[idx].Signal())
                                              ))
                                          )
                                          .Start();
            }

            try
            {
                // doesnt really matter which ctx sends the messages, they know about eachother subscriptions
                var ctx1 = contexts.First();
                var ctx2 = contexts.Last();

                await ctx1.CommandBus.Send(new Command<DoSimple>("cmd1", new DoSimple { }));
                await ctx2.CommandBus.Send(new Command<DoComplex>("cmd2", new DoComplex { }));
                
                foreach (var sync in syncs)
                {
                    sync.Wait(TimeSpan.FromSeconds(2));
                }
            }
            finally
            {
                foreach (var context in contexts)
                {
                    context.Dispose();
                }
            }

            // assert
            foreach (var sync in syncs)
            {
                // asserts all messages were received by each processor
                Assert.AreEqual(0, sync.CurrentCount);
            }
        }