コード例 #1
0
        public void Test()
        {
            var streamer = EnvelopeStreamer.CreateDefault(typeof(SecondPassed));
            var builder  = new CqrsEngineBuilder(streamer);
            var store    = FileStorage.CreateConfig(GetType().Name, reset: true);


            builder.Handle(store.CreateInbox("inbox"), BuildRouter(store, streamer));
            builder.Handle(store.CreateInbox("process"), ie => Console.WriteLine("Message from past!"));

            var inboxWriter     = store.CreateQueueWriter("inbox");
            var futureContainer = store.CreateStreaming().GetContainer("future");
            var timer           = new StreamingTimerService(inboxWriter, futureContainer, streamer);

            builder.AddProcess(timer);
            builder.Handle(store.CreateInbox("timer"), timer.PutMessage);

            using (var engine = builder.Build())
            {
                var bytes = streamer.SaveEnvelopeData(new SecondPassed(), c => c.DelayBy(TimeSpan.FromSeconds(4)));
                inboxWriter.PutMessage(bytes);
                Console.WriteLine("Sent message to future...");
                engine.RunForever();
            }
        }
コード例 #2
0
        public void Dulicate_message_is_detected()
        {
            var streamer = EnvelopeStreamer.CreateDefault(typeof(Message));

            var builder = new CqrsEngineBuilder(streamer);

            var cfg = new MemoryStorageConfig();

            var sender = cfg.CreateSimpleSender(streamer, "in");

            builder.Handle(cfg.CreateInbox("in"), envelope => Console.WriteLine("Got message"));

            var env = new EnvelopeBuilder("fixed ID").Build();

            using (var token = new CancellationTokenSource())
                using (var build = builder.Build())
                    using (TestObserver.When <EnvelopeDuplicateDiscarded>(discarded => token.Cancel()))
                    {
                        sender.SendBatch(new object[] { new Message() }, IdGeneration.HashContent);
                        sender.SendBatch(new object[] { new Message() }, IdGeneration.HashContent);
                        build.Start(token.Token);

                        if (Debugger.IsAttached)
                        {
                            token.Token.WaitHandle.WaitOne();
                        }
                        else
                        {
                            token.Token.WaitHandle.WaitOne(10000);
                        }

                        Assert.IsTrue(token.IsCancellationRequested);
                    }
        }
コード例 #3
0
        public void Run_in_test()
        {
            var streamer = EnvelopeStreamer.CreateDefault(typeof(CreateCustomer), typeof(CustomerCreated));

            var builder = new CqrsEngineBuilder(streamer);
            var account = new MemoryAccount();

            var nuclear = account.CreateNuclear();
            var inbox   = account.CreateInbox("input");
            var sender  = account.CreateSimpleSender(streamer, "input");

            var handler = new MessageHandler();

            handler.WireToLambda <CreateCustomer>(customer => Consume(customer, nuclear, sender));
            handler.WireToLambda <CustomerCreated>(m => Console.WriteLine("Created!"));
            builder.Handle(inbox, handler.HandleEnvelope);

            using (var engine = builder.Build())
            {
                sender.SendOne(new CreateCustomer
                {
                    CustomerId   = 1,
                    CustomerName = "Rinat Abdullin"
                });
                engine.RunForever();
            }
        }
コード例 #4
0
        public void Run_in_test()
        {
            var streamer = EnvelopeStreamer.CreateDefault(typeof(CreateCustomer), typeof(CustomerCreated));

            var builder = new CqrsEngineBuilder(streamer);
            var account = new MemoryStorageConfig();

            var nuclear = account.CreateNuclear(new TestStrategy());
            var inbox   = account.CreateInbox("input");
            var sender  = account.CreateSimpleSender(streamer, "input");



            var handler = new RedirectToCommand();

            handler.WireToLambda <CreateCustomer>(customer => Consume(customer, nuclear, sender));
            handler.WireToLambda <CustomerCreated>(m => Console.WriteLine("Created!"));
            builder.Handle(inbox, envelope => handler.InvokeMany(envelope.SelectContents()));

            using (var engine = builder.Build())
            {
                sender.SendOne(new CreateCustomer
                {
                    CustomerId   = 1,
                    CustomerName = "Rinat Abdullin"
                });
                engine.RunForever();
            }
        }
コード例 #5
0
        public void Test()
        {
            var types = Assembly.GetExecutingAssembly().GetExportedTypes()
                        .Where(t => typeof(IPS_SampleMessage).IsAssignableFrom(t));
            var streamer = EnvelopeStreamer.CreateDefault(types);
            var builder  = new CqrsEngineBuilder(streamer);
            // only message contracts within this class

            // configure in memory:
            //                            -> sub 1
            //  inbox -> [PubSubRouter] <
            //                            -> sub 2
            //
            var store   = new MemoryAccount();
            var nuclear = store.CreateNuclear();

            var router = new PubSubRouter(nuclear, store.CreateWriteQueueFactory(), streamer);

            router.Init();

            builder.Dispatch(store.CreateInbox("sub1"), b => Console.WriteLine("sub1 hit"));
            builder.Dispatch(store.CreateInbox("sub2"), b => Console.WriteLine("sub2 hit"));
            builder.Handle(store.CreateInbox("inbox"), router.DispatchMessage);


            var sender = store.CreateSimpleSender(streamer, "inbox");

            using (var engine = builder.Build())
                using (var cts = new CancellationTokenSource())
                {
                    var task = engine.Start(cts.Token);

                    // no handler should get these.
                    sender.SendOne(new SomethingHappened());
                    sender.SendOne(new OtherHappened());

                    // subscribe sub1 to all messages and sub2 to specific message
                    sender.SendControl(eb =>
                    {
                        eb.AddString("router-subscribe:sub1", ".*");
                        eb.AddString("router-subscribe:sub2", "SomethingHappened");
                    });
                    sender.SendOne(new SomethingHappened());
                    sender.SendOne(new OtherHappened());

                    // unsubscribe all
                    sender.SendControl(eb =>
                    {
                        eb.AddString("router-unsubscribe:sub1", ".*");
                        eb.AddString("router-unsubscribe:sub2", "SomethingHappened");
                    });
                    sender.SendOne(new SomethingHappened());
                    sender.SendOne(new OtherHappened());


                    task.Wait(5000);
                }
        }
コード例 #6
0
        public void Direct()
        {
            var config   = new MemoryStorageConfig();
            var raw      = new CqrsEngineBuilder(EnvelopeStreamer.CreateDefault());
            var doWriter = config.CreateQueueWriter("do");

            // forwarder do => do
            raw.Dispatch(config.CreateInbox("do"), doWriter.PutMessage);
            TestConfiguration(doWriter, raw);
        }
コード例 #7
0
        public void RouterChain()
        {
            var config   = new MemoryStorageConfig();
            var raw      = new CqrsEngineBuilder(EnvelopeStreamer.CreateDefault());
            var doWriter = config.CreateQueueWriter("do");

            var route1 = config.CreateQueueWriter("route1");
            var route2 = config.CreateQueueWriter("route2");

            // in => (route1 OR route2)
            raw.Dispatch(config.CreateInbox("in"), bytes => LoadBalance(bytes, route1, route2));
            // forwarder (route1,route2) => do
            raw.Dispatch(config.CreateInbox("route1", "route2"), doWriter.PutMessage);

            raw.Dispatch(config.CreateInbox("do"), bytes => LoadBalance(bytes, route1, route2));
            TestConfiguration(config.CreateQueueWriter("in"), raw);
        }
コード例 #8
0
        public void Then_transactional_support_is_provided()
        {
            var streamer = EnvelopeStreamer.CreateDefault(typeof(Act));
            var builder  = new CqrsEngineBuilder(streamer);
            var setup    = ComposeComponents(streamer);

            var handler = new CommandHandler();

            handler.WireToLambda <Act>(act => Consume(act, setup.Storage));
            builder.Handle(setup.Inbox, envelope =>
            {
                using (var tx = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    handler.HandleAll(envelope);
                    tx.Complete();
                }
            });


            using (var source = new CancellationTokenSource())
                using (TestObserver.When <EnvelopeDispatched>(e => source.Cancel()))
                    using (var engine = builder.Build())
                    {
                        setup.Sender.SendBatch(new[] { new Act(), new Act(), new Act {
                                                           Fail = true
                                                       } });
                        setup.Sender.SendBatch(new[] { new Act(), new Act(), new Act() });


                        var task = engine.Start(source.Token);
                        //    Trace.WriteLine("Started");
                        if (!task.Wait(Debugger.IsAttached ? int.MaxValue : TestSpeed))
                        {
                            source.Cancel();
                            Assert.Fail("System should be stopped by now");
                        }

                        var storage = setup.Storage;
                        var count   = storage.GetSingletonOrNew <int>();
                        Assert.AreEqual(3, count, "Three acts are expected");
                    }
        }