Exemple #1
0
        public void SubscriptionWithExistingCursor()
        {
            var sp = ServiceProviderHelper.CreateServiceProvider();

            using (var bus = ActivatorUtilities.CreateInstance <TestScaleoutBus>(sp, 2))
            {
                var         subscriber   = new TestSubscriber(new[] { "key" });
                var         cd           = new CountDownRange <int>(Enumerable.Range(2, 4));
                IDisposable subscription = null;

                bus.Publish(0, 0, new[] {
                    new Message("test", "key", "1"),
                    new Message("test", "key", "50")
                });

                bus.Publish(1, 0, new[] {
                    new Message("test1", "key", "51")
                });

                bus.Publish(1, 2, new[] {
                    new Message("test2", "key", "2"),
                    new Message("test3", "key", "3"),
                    new Message("test2", "key", "4"),
                });

                try
                {
                    subscription = bus.Subscribe(subscriber, "s-0,00000000|1,00000000", (result, state) =>
                    {
                        foreach (var m in result.GetMessages())
                        {
                            int n = Int32.Parse(m.GetString());
                            Assert.True(cd.Mark(n));
                        }

                        return(TaskAsyncHelper.True);
                    }, 10, null);

                    bus.Publish(0, 2, new[] { new Message("test", "key", "5") });

                    Assert.True(cd.Wait(TimeSpan.FromSeconds(10)));
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }
        public void SubscriptionWithExistingCursor()
        {
            var sp = ServiceProviderHelper.CreateServiceProvider(services =>
            {
                var passThroughMinfier = new PassThroughStringMinifier();
                services.AddSingleton <IStringMinifier>(passThroughMinfier);
            });

            using (var bus = (MessageBus)sp.GetRequiredService <IMessageBus>())
            {
                Func <TestSubscriber> subscriberFactory = () => new TestSubscriber(new[] { "key" });
                var         cd           = new CountDownRange <int>(Enumerable.Range(2, 4));
                IDisposable subscription = null;
                string      prefix       = DefaultSubscription._defaultCursorPrefix;

                // Pretend like we had an initial subscription
                bus.Subscribe(subscriberFactory(), null, (result, state) => TaskAsyncHelper.True, 10, null)
                .Dispose();

                bus.Publish("test", "key", "1").Wait();
                bus.Publish("test", "key", "2").Wait();
                bus.Publish("test", "key", "3").Wait();
                bus.Publish("test", "key", "4").Wait();

                try
                {
                    subscription = bus.Subscribe(subscriberFactory(), prefix + "key,00000001", (result, state) =>
                    {
                        foreach (var m in result.GetMessages())
                        {
                            int n = Int32.Parse(m.GetString());
                            Assert.True(cd.Mark(n));
                        }

                        return(TaskAsyncHelper.True);
                    }, 10, null);

                    bus.Publish("test", "key", "5");

                    Assert.True(cd.Wait(TimeSpan.FromSeconds(5)));
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }
        public void AddingEventAndSendingMessages()
        {
            var sp = ServiceProviderHelper.CreateServiceProvider();

            using (var bus = (MessageBus)sp.GetRequiredService <IMessageBus>())
            {
                var         subscriber   = new TestSubscriber(new[] { "a" });
                int         max          = 100;
                var         cd           = new CountDownRange <int>(Enumerable.Range(0, max));
                int         prev         = -1;
                IDisposable subscription = null;

                try
                {
                    subscription = bus.Subscribe(subscriber, null, (result, state) =>
                    {
                        foreach (var m in result.GetMessages())
                        {
                            int n = Int32.Parse(m.GetString());
                            Assert.True(n == prev + 1, "Expected " + (prev + 1));
                            prev = n;
                            Assert.True(cd.Mark(n));
                        }

                        return(TaskAsyncHelper.True);
                    },
                                                 maxMessages: 10,
                                                 state: null);

                    for (int i = 0; i < max; i++)
                    {
                        subscriber.AddEvent("b");
                        bus.Publish("test", "b", i.ToString()).Wait();
                    }

                    Assert.True(cd.Wait(TimeSpan.FromSeconds(10)));
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }