Esempio n. 1
0
        public void SubscriptionWithMultipleExistingCursors()
        {
            var dr = new DefaultDependencyResolver();
            var passThroughMinfier = new PassThroughStringMinifier();

            dr.Register(typeof(IStringMinifier), () => passThroughMinfier);
            using (var bus = new MessageBus(dr))
            {
                Func <ISubscriber> subscriberFactory = () => new TestSubscriber(new[] { "key", "key2" });
                var         cdKey        = new CountDownRange <int>(Enumerable.Range(2, 4));
                var         cdKey2       = new CountDownRange <int>(new[] { 1, 2, 10 });
                IDisposable subscription = null;

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

                // This simulates a reconnect
                bus.Publish("test", "key", "1").Wait();
                bus.Publish("test", "key", "2").Wait();
                bus.Publish("test", "key", "3").Wait();
                bus.Publish("test", "key", "4").Wait();
                bus.Publish("test", "key2", "1").Wait();
                bus.Publish("test", "key2", "2").Wait();

                try
                {
                    subscription = bus.Subscribe(subscriberFactory(), "key,00000001|key2,00000000", (result, state) =>
                    {
                        foreach (var m in result.GetMessages())
                        {
                            int n = Int32.Parse(m.GetString());
                            if (m.Key == "key")
                            {
                                Assert.True(cdKey.Mark(n));
                            }
                            else
                            {
                                Assert.True(cdKey2.Mark(n));
                            }
                        }

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

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

                    Assert.True(cdKey.Wait(TimeSpan.FromSeconds(5)));
                    Assert.True(cdKey2.Wait(TimeSpan.FromSeconds(5)));
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }
Esempio n. 2
0
        public async Task SubscriptionWithExistingCursor()
        {
            var dr = new DefaultDependencyResolver();
            var passThroughMinfier = new PassThroughStringMinifier();

            dr.Register(typeof(IStringMinifier), () => passThroughMinfier);
            using (var bus = new MessageBus(dr))
            {
                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();

                await bus.Publish("test", "key", "1");

                await bus.Publish("test", "key", "2");

                await bus.Publish("test", "key", "3");

                await bus.Publish("test", "key", "4");

                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);

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

                    await cd.WaitAsync().OrTimeout();
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }
Esempio n. 3
0
        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();
                    }
                }
            }
        }
Esempio n. 4
0
        public void SubscriptionWithScaleoutCursorGetsOnlyNewMessages()
        {
            var sp = ServiceProviderHelper.CreateServiceProvider(services =>
            {
                var passThroughMinfier = new PassThroughStringMinifier();
                services.AddSingleton <IStringMinifier>(passThroughMinfier);
            });

            using (var bus = (MessageBus)sp.GetRequiredService <IMessageBus>())
            {
                Func <ISubscriber> subscriberFactory = () => new TestSubscriber(new[] { "key" });
                var         tcs          = new TaskCompletionSource <Message[]>();
                IDisposable subscription = null;

                try
                {
                    // Set-up dummy subscription so the first Publish doesn't noop
                    bus.Subscribe(subscriberFactory(), null, (result, state) => TaskAsyncHelper.True, 10, null).Dispose();

                    bus.Publish("test", "key", "badvalue").Wait();

                    subscription = bus.Subscribe(subscriberFactory(), "s-key,00000000", (result, state) =>
                    {
                        tcs.TrySetResult(result.GetMessages().ToArray());
                        return(TaskAsyncHelper.True);
                    }, 10, null);

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

                    Assert.True(tcs.Task.Wait(TimeSpan.FromSeconds(5)));

                    foreach (var m in tcs.Task.Result)
                    {
                        Assert.Equal("key", m.Key);
                        Assert.Equal("value", m.GetString());
                    }
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }
Esempio n. 5
0
        public void SubscriptionWithScaleoutCursorGetsOnlyNewMessages()
        {
            var dr = new DefaultDependencyResolver();
            var passThroughMinfier = new PassThroughStringMinifier();

            dr.Register(typeof(IStringMinifier), () => passThroughMinfier);
            using (var bus = new MessageBus(dr))
            {
                Func <ISubscriber> subscriberFactory = () => new TestSubscriber(new[] { "key" });
                var         tcs          = new TaskCompletionSource <MessageResult>();
                IDisposable subscription = null;

                try
                {
                    // Set-up dummy subscription so the first Publish doesn't noop
                    bus.Subscribe(subscriberFactory(), null, (result, state) => TaskAsyncHelper.True, 10, null).Dispose();

                    bus.Publish("test", "key", "badvalue").Wait();

                    subscription = bus.Subscribe(subscriberFactory(), "s-key,00000000", (result, state) =>
                    {
                        tcs.TrySetResult(result);
                        return(TaskAsyncHelper.True);
                    }, 10, null);

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

                    Assert.True(tcs.Task.Wait(TimeSpan.FromSeconds(5)));

                    foreach (var m in tcs.Task.Result.GetMessages())
                    {
                        Assert.Equal("key", m.Key);
                        Assert.Equal("value", m.GetString());
                    }
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }
Esempio n. 6
0
        public void SubscriptionWithExistingCursor()
        {
            var dr = new DefaultDependencyResolver();
            var passThroughMinfier = new PassThroughStringMinifier();

            dr.Register(typeof(IStringMinifier), () => passThroughMinfier);
            using (var bus = new MessageBus(dr))
            {
                var         subscriber   = new TestSubscriber(new[] { "key" });
                var         cd           = new CountDownRange <int>(Enumerable.Range(2, 4));
                IDisposable subscription = null;

                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(subscriber, "key,00000001", result =>
                    {
                        foreach (var m in result.GetMessages())
                        {
                            int n = Int32.Parse(m.Value);
                            Assert.True(cd.Mark(n));
                        }

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

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

                    Assert.True(cd.Wait(TimeSpan.FromSeconds(5)));
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }
        public void SubscriptionWithScaleoutCursorGetsOnlyNewMessages()
        {
            var dr = new DefaultDependencyResolver();
            var passThroughMinfier = new PassThroughStringMinifier();
            dr.Register(typeof(IStringMinifier), () => passThroughMinfier);
            using (var bus = new MessageBus(dr))
            {
                Func<ISubscriber> subscriberFactory = () => new TestSubscriber(new[] { "key" });
                var tcs = new TaskCompletionSource<Message[]>();
                IDisposable subscription = null;

                try
                {
                    // Set-up dummy subscription so the first Publish doesn't noop
                    bus.Subscribe(subscriberFactory(), null, (result, state) => TaskAsyncHelper.True, 10, null).Dispose();

                    bus.Publish("test", "key", "badvalue").Wait();

                    subscription = bus.Subscribe(subscriberFactory(), "s-key,00000000", (result, state) =>
                    {
                        tcs.TrySetResult(result.GetMessages().ToArray());
                        return TaskAsyncHelper.True;
                    }, 10, null);

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

                    Assert.True(tcs.Task.Wait(TimeSpan.FromSeconds(5)));

                    foreach (var m in tcs.Task.Result)
                    {
                        Assert.Equal("key", m.Key);
                        Assert.Equal("value", m.GetString());
                    }
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }
        public void SubscriptionWithMultipleExistingCursors()
        {
            var dr = new DefaultDependencyResolver();
            var passThroughMinfier = new PassThroughStringMinifier();
            dr.Register(typeof(IStringMinifier), () => passThroughMinfier);
            using (var bus = new MessageBus(dr))
            {
                Func<ISubscriber> subscriberFactory = () => new TestSubscriber(new[] { "key", "key2" });
                var cdKey = new CountDownRange<int>(Enumerable.Range(2, 4));
                var cdKey2 = new CountDownRange<int>(new[] { 1, 2, 10 });
                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();

                // This simulates a reconnect
                bus.Publish("test", "key", "1").Wait();
                bus.Publish("test", "key", "2").Wait();
                bus.Publish("test", "key", "3").Wait();
                bus.Publish("test", "key", "4").Wait();
                bus.Publish("test", "key2", "1").Wait();
                bus.Publish("test", "key2", "2").Wait();

                try
                {
                    subscription = bus.Subscribe(subscriberFactory(), prefix + "key,00000001|key2,00000000", (result, state) =>
                    {
                        foreach (var m in result.GetMessages())
                        {
                            int n = Int32.Parse(m.GetString());
                            if (m.Key == "key")
                            {
                                Assert.True(cdKey.Mark(n));
                            }
                            else
                            {
                                Assert.True(cdKey2.Mark(n));
                            }
                        }

                        return TaskAsyncHelper.True;

                    }, 10, null);

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

                    Assert.True(cdKey.Wait(TimeSpan.FromSeconds(5)));
                    Assert.True(cdKey2.Wait(TimeSpan.FromSeconds(5)));
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }
Esempio n. 9
0
        public void SubscriptionWithMultipleExistingCursors()
        {
            var dr = new DefaultDependencyResolver();
            var passThroughMinfier = new PassThroughStringMinifier();
            dr.Register(typeof(IStringMinifier), () => passThroughMinfier);
            using (var bus = new MessageBus(dr))
            {
                var subscriber = new TestSubscriber(new[] { "key", "key2" });
                var cdKey = new CountDownRange<int>(Enumerable.Range(2, 4));
                var cdKey2 = new CountDownRange<int>(new[] { 1, 2, 10 });
                IDisposable subscription = null;

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

                try
                {
                    subscription = bus.Subscribe(subscriber, "key,00000001|key2,00000000", result =>
                    {
                        foreach (var m in result.GetMessages())
                        {
                            int n = Int32.Parse(m.Value);
                            if (m.Key == "key")
                            {
                                Assert.True(cdKey.Mark(n));
                            }
                            else
                            {
                                Assert.True(cdKey2.Mark(n));
                            }
                        }

                        return TaskAsyncHelper.True;

                    }, 10);

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

                    Assert.True(cdKey.Wait(TimeSpan.FromSeconds(5)));
                    Assert.True(cdKey2.Wait(TimeSpan.FromSeconds(5)));
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }
Esempio n. 10
0
        public void SubscriptionWithExistingCursor()
        {
            var dr = new DefaultDependencyResolver();
            var passThroughMinfier = new PassThroughStringMinifier();
            dr.Register(typeof(IStringMinifier), () => passThroughMinfier);
            using (var bus = new MessageBus(dr))
            {
                var subscriber = new TestSubscriber(new[] { "key" });
                var cd = new CountDownRange<int>(Enumerable.Range(2, 4));
                IDisposable subscription = null;

                // Pretend like we had an initial subscription
                bus.Subscribe(subscriber, null, _ => TaskAsyncHelper.True, 10)
                   .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(subscriber, "key,00000001", result =>
                    {
                        foreach (var m in result.GetMessages())
                        {
                            int n = Int32.Parse(m.Value);
                            Assert.True(cd.Mark(n));
                        }

                        return TaskAsyncHelper.True;

                    }, 10);

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

                    Assert.True(cd.Wait(TimeSpan.FromSeconds(5)));
                }
                finally
                {
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                }
            }
        }
Esempio n. 11
0
        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();
                    }
                }
            }
        }