public async Task Should_ignore_message_version()
    {
        var subscriberAddress_v1 = "v1@localhost";
        var subscriberAddress_v2 = "v2@localhost";
        var messageType_v1       = new MessageType("SomeMessageType", "1.0.0.0");
        var messageType_v2       = new MessageType("SomeMessageType", "2.0.0.0");
        var subscriber_v1        = new Subscriber(subscriberAddress_v1, "some_endpoint_name");
        var subscriber_v2        = new Subscriber(subscriberAddress_v2, "another_endpoint_name");

        var storage = new SubscriptionPersister(store)
        {
            DisableAggressiveCaching = true
        };

        await storage.Subscribe(subscriber_v1, messageType_v1, new ContextBag());

        await storage.Subscribe(subscriber_v2, messageType_v2, new ContextBag());

        var subscribers_looked_up_by_v1 = (await storage.GetSubscriberAddressesForMessage(new[]
        {
            messageType_v1
        }, new ContextBag())).ToArray();

        var subscribers_looked_up_by_v2 = (await storage.GetSubscriberAddressesForMessage(new[]
        {
            messageType_v2
        }, new ContextBag())).ToArray();

        Assert.AreEqual(2, subscribers_looked_up_by_v1.Length);
        Assert.AreEqual(2, subscribers_looked_up_by_v2.Length);
    }
        public void GetSubscriberAddressesForMessage_EmptyMessages_ReturnsEmpty()
        {
            var result = _persister.GetSubscriberAddressesForMessage(new List <MessageType>());

            result.Should().NotBeNull();
            result.Count().Should().Be(0);
        }
    public async Task At_unsubscribe_time_should_ignore_message_version()
    {
        var subscriberAddress = "subscriber@localhost";
        var endpointName      = "endpoint_name";
        var messageType_v1    = new MessageType("SomeMessageType", "1.0.0.0");
        var messageType_v2    = new MessageType("SomeMessageType", "2.0.0.0");
        var subscriber_v1     = new Subscriber(subscriberAddress, endpointName);
        var subscriber_v2     = new Subscriber(subscriberAddress, endpointName);

        var storage = new SubscriptionPersister(store)
        {
            DisableAggressiveCaching = true
        };

        await storage.Subscribe(subscriber_v1, messageType_v1, new ContextBag());

        var subscribers = (await storage.GetSubscriberAddressesForMessage(new[]
        {
            messageType_v1
        }, new ContextBag())).ToArray();

        Assert.AreEqual(1, subscribers.Length);

        await storage.Unsubscribe(subscriber_v2, messageType_v2, new ContextBag());

        var subscribers_looked_up_by_v1 = (await storage.GetSubscriberAddressesForMessage(new[]
        {
            messageType_v1
        }, new ContextBag())).ToArray();

        Assert.AreEqual(0, subscribers_looked_up_by_v1.Length);
    }
    public void Duplicates_should_not_be_generated_for_interface_inheritance_chains()
    {
        var storage = new SubscriptionPersister
        {
            DocumentStore = store
        };

        storage.Init();
        storage.Subscribe(TestClients.ClientA, new[]
                {
                    new MessageType(typeof(ISomeInterface))
                });
        storage.Subscribe(TestClients.ClientA, new[]
                {
                    new MessageType(typeof(ISomeInterface2))
                });
        storage.Subscribe(TestClients.ClientA, new[]
                {
                    new MessageType(typeof(ISomeInterface3))
                });

        var subscriptionsForMessageType = storage.GetSubscriberAddressesForMessage(new[]
                {
                    new MessageType(typeof(ISomeInterface)),
                    new MessageType(typeof(ISomeInterface2)),
                    new MessageType(typeof(ISomeInterface3))
                });

        Assert.AreEqual(1, subscriptionsForMessageType.Count());
    }
    public async Task No_subscribers_should_be_returned()
    {
        var persister = new SubscriptionPersister(store);
        var subscriptionsForMessageType = await persister.GetSubscriberAddressesForMessage(new []{ MessageTypes.MessageA }, new ContextBag());

        Assert.AreEqual(0, subscriptionsForMessageType.Count());
    }
예제 #6
0
    public void Duplicates_should_not_be_generated_for_interface_inheritance_chains()
    {
        using (var store = DocumentStoreBuilder.Build())
        {
            var storage = new SubscriptionPersister
            {
                DocumentStore = store
            };

            storage.Init();
            storage.Subscribe(TestClients.ClientA, new[]
            {
                new MessageType(typeof(ISomeInterface))
            });
            storage.Subscribe(TestClients.ClientA, new[]
            {
                new MessageType(typeof(ISomeInterface2))
            });
            storage.Subscribe(TestClients.ClientA, new[]
            {
                new MessageType(typeof(ISomeInterface3))
            });

            var subscriptionsForMessageType = storage.GetSubscriberAddressesForMessage(new[]
            {
                new MessageType(typeof(ISomeInterface)),
                new MessageType(typeof(ISomeInterface2)),
                new MessageType(typeof(ISomeInterface3))
            });

            Assert.AreEqual(1, subscriptionsForMessageType.Count());
        }
    }
    public async Task No_subscribers_should_be_returned()
    {
        var persister = new SubscriptionPersister(store);
        var subscriptionsForMessageType = await persister.GetSubscriberAddressesForMessage(new[] { MessageTypes.MessageA }, new ContextBag());

        Assert.AreEqual(0, subscriptionsForMessageType.Count());
    }
    public void No_subscribers_should_be_returned()
    {
        var storage = new SubscriptionPersister
        {
            DocumentStore = store
        };

        storage.Init();
        var subscriptionsForMessageType = storage.GetSubscriberAddressesForMessage(MessageTypes.MessageA);

        Assert.AreEqual(0, subscriptionsForMessageType.Count());
    }
    public void All_subscription_entries_for_specified_message_types_should_be_removed()
    {
        var storage = new SubscriptionPersister
        {
            DocumentStore = store
        };

        storage.Subscribe(TestClients.ClientA, MessageTypes.All);

        storage.Unsubscribe(TestClients.ClientA, MessageTypes.All);

        var clients = storage.GetSubscriberAddressesForMessage(MessageTypes.All);
        Assert.IsFalse(clients.Any(a => a == TestClients.ClientA));
    }
    public void No_subscribers_should_be_returned()
    {
        using (var store = DocumentStoreBuilder.Build())
        {
            var storage = new SubscriptionPersister
            {
                DocumentStore = store
            };

            storage.Init();
            var subscriptionsForMessageType = storage.GetSubscriberAddressesForMessage(MessageTypes.MessageA);

            Assert.AreEqual(0, subscriptionsForMessageType.Count());
        }
    }
    public void All_subscription_entries_for_specified_message_types_should_be_removed()
    {
        using (var store = DocumentStoreBuilder.Build())
        {
            var storage = new SubscriptionPersister
            {
                DocumentStore = store
            };

            storage.Subscribe(TestClients.ClientA, MessageTypes.All);

            storage.Unsubscribe(TestClients.ClientA, MessageTypes.All);

            var clients = storage.GetSubscriberAddressesForMessage(MessageTypes.All);
            Assert.IsFalse(clients.Any(a => a == TestClients.ClientA));
        }
    }
    public async Task All_subscription_entries_for_specified_message_types_should_be_removed()
    {
        var storage = new SubscriptionPersister(store);
        var context = new ContextBag();

        await storage.Subscribe(TestClients.ClientA, MessageTypes.MessageA, context);

        await storage.Subscribe(TestClients.ClientA, MessageTypes.MessageB, context);

        await storage.Unsubscribe(TestClients.ClientA, MessageTypes.MessageA, context);

        await storage.Unsubscribe(TestClients.ClientA, MessageTypes.MessageB, context);

        var clients = await storage.GetSubscriberAddressesForMessage(new [] { MessageTypes.MessageA, MessageTypes.MessageB }, context);

        Assert.IsEmpty(clients);
    }
    public void The_names_of_all_subscribers_should_be_returned()
    {
        var storage = new SubscriptionPersister
        {
            DocumentStore = store
        };

        storage.Subscribe(TestClients.ClientA, MessageTypes.MessageA);
        storage.Subscribe(TestClients.ClientA, MessageTypes.MessageB);
        storage.Subscribe(TestClients.ClientB, MessageTypes.MessageA);
        storage.Subscribe(TestClients.ClientA, MessageTypes.MessageAv2);

        var subscriptionsForMessageType = storage.GetSubscriberAddressesForMessage(MessageTypes.MessageA);

        Assert.AreEqual(2, subscriptionsForMessageType.Count());
        Assert.AreEqual(TestClients.ClientA, subscriptionsForMessageType.First());
    }
    public async Task Duplicates_should_not_be_generated_for_interface_inheritance_chains()
    {
        var storage = new SubscriptionPersister(store);
        var context = new ContextBag();

        await storage.Subscribe(TestClients.ClientA, new MessageType(typeof(ISomeInterface)), context);
        await storage.Subscribe(TestClients.ClientA, new MessageType(typeof(ISomeInterface2)), context);
        await storage.Subscribe(TestClients.ClientA, new MessageType(typeof(ISomeInterface3)), context);

        var subscriptionsForMessageType = await storage.GetSubscriberAddressesForMessage(new[]
                {
                    new MessageType(typeof(ISomeInterface)),
                    new MessageType(typeof(ISomeInterface2)),
                    new MessageType(typeof(ISomeInterface3))
                }, context);

        Assert.AreEqual(1, subscriptionsForMessageType.Count());
    }
예제 #15
0
        public async Task ShouldReturnSubscriptionsForNewerVersionsOfSameMessageType()
        {
            var settings = new SettingsHolder();
            var subscriptionPersister = new SubscriptionPersister(documentStore, settings, "NServiceBus.Routing.EndpointName", "TestEndpoint", new MessageType[0]);

            var v1MessageType = new MessageType(typeof(SampleMessageType).FullName, new Version(1, 0, 0));
            var v2MessageType = new MessageType(typeof(SampleMessageType).FullName, new Version(2, 0, 0));
            var v2Subscriber  = new Subscriber("V2SubscriberAddress", "V2Subscriber");

            await subscriptionPersister.Subscribe(v2Subscriber, v2MessageType, new ContextBag());

            var foundSubscriptions = await subscriptionPersister.GetSubscriberAddressesForMessage(new[] { v1MessageType }, new ContextBag());

            var foundSubscriber = foundSubscriptions.Single();

            Assert.AreEqual(v2Subscriber.Endpoint, foundSubscriber.Endpoint);
            Assert.AreEqual(v2Subscriber.TransportAddress, foundSubscriber.TransportAddress);
        }
    public async Task The_names_of_all_subscribers_should_be_returned()
    {
        var storage = new SubscriptionPersister(store);
        var context = new ContextBag();

        await storage.Subscribe(TestClients.ClientA, MessageTypes.MessageA, context);
        await storage.Subscribe(TestClients.ClientA, MessageTypes.MessageB, context);
        await storage.Subscribe(TestClients.ClientB, MessageTypes.MessageA, context);
        await storage.Subscribe(TestClients.ClientA, MessageTypes.MessageAv2, context);

        var subscriptionsForMessageType = await storage.GetSubscriberAddressesForMessage(new []{ MessageTypes.MessageA }, context);

        Assert.AreEqual(2, subscriptionsForMessageType.Count());

        Assert.AreEqual(TestClients.ClientA.TransportAddress, subscriptionsForMessageType.ElementAt(0).TransportAddress);
        Assert.AreEqual(TestClients.ClientA.Endpoint, subscriptionsForMessageType.ElementAt(0).Endpoint);

        Assert.AreEqual(TestClients.ClientB.TransportAddress, subscriptionsForMessageType.ElementAt(1).TransportAddress);
        Assert.AreEqual(TestClients.ClientB.Endpoint, subscriptionsForMessageType.ElementAt(1).Endpoint);
    }
예제 #17
0
    public void The_names_of_all_subscribers_should_be_returned()
    {
        using (var store = DocumentStoreBuilder.Build())
        {
            var storage = new SubscriptionPersister
            {
                DocumentStore = store
            };

            storage.Subscribe(TestClients.ClientA, MessageTypes.MessageA);
            storage.Subscribe(TestClients.ClientA, MessageTypes.MessageB);
            storage.Subscribe(TestClients.ClientB, MessageTypes.MessageA);
            storage.Subscribe(TestClients.ClientA, MessageTypes.MessageAv2);

            var subscriptionsForMessageType = storage.GetSubscriberAddressesForMessage(MessageTypes.MessageA);

            Assert.AreEqual(2, subscriptionsForMessageType.Count());
            Assert.AreEqual(TestClients.ClientA, subscriptionsForMessageType.First());
        }
    }
    public async Task should_overwrite_existing_subscription()
    {
        const string subscriberAddress = "testEndPoint@localhost";
        var messageType = new MessageType("SomeMessageType", "1.0.0.0");
        var subscriber_v6 = new Subscriber(subscriberAddress, "endpoint_name");
        var subscriber_v6_2 = new Subscriber(subscriberAddress, "new_endpoint_name");

        var storage = new SubscriptionPersister(store);
        await storage.Subscribe(subscriber_v6, messageType, new ContextBag());
        await storage.Subscribe(subscriber_v6_2, messageType, new ContextBag());
 
        var subscriber = (await storage.GetSubscriberAddressesForMessage(new[]
        {
            messageType
        }, new ContextBag())).ToArray();
 
        Assert.AreEqual(1, subscriber.Length);
        Assert.AreEqual(subscriberAddress, subscriber[0].TransportAddress);
        Assert.AreEqual("new_endpoint_name", subscriber[0].Endpoint);
    }
예제 #19
0
    public async Task Duplicates_should_not_be_generated_for_interface_inheritance_chains()
    {
        var storage = new SubscriptionPersister(store);
        var context = new ContextBag();

        await storage.Subscribe(TestClients.ClientA, new MessageType(typeof(ISomeInterface)), context);

        await storage.Subscribe(TestClients.ClientA, new MessageType(typeof(ISomeInterface2)), context);

        await storage.Subscribe(TestClients.ClientA, new MessageType(typeof(ISomeInterface3)), context);

        var subscriptionsForMessageType = await storage.GetSubscriberAddressesForMessage(new[]
        {
            new MessageType(typeof(ISomeInterface)),
            new MessageType(typeof(ISomeInterface2)),
            new MessageType(typeof(ISomeInterface3))
        }, context);

        Assert.AreEqual(1, subscriptionsForMessageType.Count());
    }
예제 #20
0
    public async Task should_overwrite_existing_subscription()
    {
        const string subscriberAddress = "testEndPoint@localhost";
        var          messageType       = new MessageType("SomeMessageType", "1.0.0.0");
        var          subscriber_v6     = new Subscriber(subscriberAddress, "endpoint_name");
        var          subscriber_v6_2   = new Subscriber(subscriberAddress, "new_endpoint_name");

        var storage = new SubscriptionPersister(store);
        await storage.Subscribe(subscriber_v6, messageType, new ContextBag());

        await storage.Subscribe(subscriber_v6_2, messageType, new ContextBag());

        var subscriber = (await storage.GetSubscriberAddressesForMessage(new[]
        {
            messageType
        }, new ContextBag())).ToArray();

        Assert.AreEqual(1, subscriber.Length);
        Assert.AreEqual(subscriberAddress, subscriber[0].TransportAddress);
        Assert.AreEqual("new_endpoint_name", subscriber[0].Endpoint);
    }
예제 #21
0
    public async Task The_names_of_all_subscribers_should_be_returned()
    {
        var storage = new SubscriptionPersister(store);
        var context = new ContextBag();

        await storage.Subscribe(TestClients.ClientA, MessageTypes.MessageA, context);

        await storage.Subscribe(TestClients.ClientA, MessageTypes.MessageB, context);

        await storage.Subscribe(TestClients.ClientB, MessageTypes.MessageA, context);

        await storage.Subscribe(TestClients.ClientA, MessageTypes.MessageAv2, context);

        var subscriptionsForMessageType = await storage.GetSubscriberAddressesForMessage(new [] { MessageTypes.MessageA }, context);

        Assert.AreEqual(2, subscriptionsForMessageType.Count());

        Assert.AreEqual(TestClients.ClientA.TransportAddress, subscriptionsForMessageType.ElementAt(0).TransportAddress);
        Assert.AreEqual(TestClients.ClientA.Endpoint, subscriptionsForMessageType.ElementAt(0).Endpoint);

        Assert.AreEqual(TestClients.ClientB.TransportAddress, subscriptionsForMessageType.ElementAt(1).TransportAddress);
        Assert.AreEqual(TestClients.ClientB.Endpoint, subscriptionsForMessageType.ElementAt(1).Endpoint);
    }
        public async Task Subscription_queries_should_be_cached(bool disableAggressiveCache, RequestStatus expectedResultOnSubscriptionQueries)
        {
            persister = new SubscriptionPersister(store);
            persister.DisableAggressiveCaching = disableAggressiveCache;

            await persister.Subscribe(new Subscriber("TransportAddress1", "Endpoint1"), MessageTypes.MessageA, new ContextBag());

            await persister.Subscribe(new Subscriber("TransportAddress2", "Endpoint2"), MessageTypes.MessageA, new ContextBag());

            using (var session = store.OpenAsyncSession())
            {
                await session.StoreAsync(new RandomDoc(), "RandomDoc/test").ConfigureAwait(false);

                await session.SaveChangesAsync().ConfigureAwait(false);
            }


            var messageTypes = new[]
            {
                MessageTypes.MessageA
            };

            Console.WriteLine("-- First subscriber query...");
            requests.Clear();
            var subscribers = await persister.GetSubscriberAddressesForMessage(messageTypes, new ContextBag()).ConfigureAwait(false);

            Assert.AreEqual(2, subscribers.Count());
            Assert.AreEqual(1, requests.Count);
            Assert.AreEqual(RequestStatus.SentToServer, requests[0].Status);

            Console.WriteLine($"-- Subsequent subscription queries, should be {expectedResultOnSubscriptionQueries}");
            for (var i = 0; i < 5; i++)
            {
                requests.Clear();
                var cachedSubs = await persister.GetSubscriberAddressesForMessage(messageTypes, new ContextBag()).ConfigureAwait(false);

                Assert.AreEqual(2, cachedSubs.Count());
                Assert.AreEqual(1, requests.Count);
                Assert.AreEqual(expectedResultOnSubscriptionQueries, requests[0].Status);
            }

            Console.WriteLine("-- Random doc first query");
            using (var session = store.OpenAsyncSession())
            {
                requests.Clear();
                await session.LoadAsync <RandomDoc>("RandomDoc/test").ConfigureAwait(false);

                Assert.AreEqual(1, requests.Count);
                Assert.AreEqual(RequestStatus.SentToServer, requests[0].Status);
            }

            Console.WriteLine("-- Random doc, subsequent loads should be Cached, not AggressivelyCached");
            for (var i = 0; i < 5; i++)
            {
                using (var session = store.OpenAsyncSession())
                {
                    requests.Clear();
                    await session.LoadAsync <RandomDoc>("RandomDoc/test").ConfigureAwait(false);

                    Assert.AreEqual(1, requests.Count);
                    Assert.AreEqual(RequestStatus.Cached, requests[0].Status);
                }
            }
        }
 public static Task <IEnumerable <Subscriber> > GetSubscribers(this SubscriptionPersister persister, params MessageType[] messageHierarchy)
 {
     return(persister.GetSubscriberAddressesForMessage(messageHierarchy, null));
 }
예제 #24
0
 public static Task <IEnumerable <Subscriber> > GetSubscribers(this SubscriptionPersister persister, MessageType[] messageHierarchy, CancellationToken cancellationToken = default) =>
 persister.GetSubscriberAddressesForMessage(messageHierarchy, null, cancellationToken);