public void should_create_subscription_by_example()
        {
            var subscription = Subscription.ByExample(x => new FakeRoutableCommand(12, "name"));

            subscription.MessageTypeId.ShouldEqual(new MessageTypeId(typeof(FakeRoutableCommand)));
            subscription.BindingKey.ShouldEqual(BindingKeyHelper.CreateFromString("12.name.*", '.'));
        }
        public void should_create_subscription_by_example_with_placeholder()
        {
            var subscription = Subscription.ByExample(x => new FakeRoutableCommand(x.Any <decimal>(), "name"));

            subscription.MessageTypeId.ShouldEqual(new MessageTypeId(typeof(FakeRoutableCommand)));
            subscription.BindingKey.ShouldEqual(BindingKeyHelper.CreateFromString("*.name.*", '.'));
        }
        public void MeasurePerformance(string routingKey, string bindingKey)
        {
            var subscription = CreateSubscription(bindingKey);
            var key          = BindingKeyHelper.CreateFromString(routingKey, '.');

            Measure.Execution(x =>
            {
                x.Iteration       = 1000000;
                x.WarmUpIteration = 1000;
                x.Action          = _ => subscription.Matches(key);
            });
        }
        public void empty_bindingkey_should_always_match(string routingKey)
        {
            // Arrange
            var peerSubscriptionTree = new PeerSubscriptionTree();
            var peer = new Peer(new PeerId("jesuistonpeer"), "endpoint");

            peerSubscriptionTree.Add(peer, BindingKey.Empty);

            // Act
            var matchingPeers = peerSubscriptionTree.GetPeers(BindingKeyHelper.CreateFromString(routingKey, '.'));

            // Assert
            matchingPeers.Single().ShouldEqual(peer);
        }
        public void invalid_subscription_should_not_match_routing_key(string subscriptionKey, string routingKey)
        {
            // Arrange
            var peerSubscriptionTree = new PeerSubscriptionTree();
            var peer         = new Peer(new PeerId("jesuistonpeer"), "endpoint");
            var subscription = BindingKeyHelper.CreateFromString(subscriptionKey, '.');

            peerSubscriptionTree.Add(peer, subscription);

            // Act
            var matchingPeers = peerSubscriptionTree.GetPeers(BindingKeyHelper.CreateFromString(routingKey, '.'));

            // Assert
            matchingPeers.ShouldBeEmpty();
        }
        public void stars_should_always_match_if_same_number_of_parts(string routingKey)
        {
            // Arrange
            var peerSubscriptionTree = new PeerSubscriptionTree();
            var peer         = new Peer(new PeerId("jesuistonpeer"), "endpoint");
            var subscription = BindingKeyHelper.CreateFromString("*.*.*", '.');

            peerSubscriptionTree.Add(peer, subscription);

            // Act
            var matchingPeers = peerSubscriptionTree.GetPeers(BindingKeyHelper.CreateFromString(routingKey, '.'));

            // Assert
            matchingPeers.Single().ShouldEqual(peer);
        }
        public void binding_key_with_dashr_should_match_routing_key(string subscriptionKey)
        {
            // Arrange
            var peerSubscriptionTree = new PeerSubscriptionTree();
            var peer         = new Peer(new PeerId("jesuistonpeer"), "endpoint");
            var subscription = BindingKeyHelper.CreateFromString(subscriptionKey, '.');

            peerSubscriptionTree.Add(peer, subscription);

            // Act
            var matchingPeers = peerSubscriptionTree.GetPeers(new BindingKey("a", "b", "c"));

            // Assert
            matchingPeers.Single().ShouldEqual(peer);
        }
        public void should_check_for_emptyness(string subscriptionKey)
        {
            var peerSubscriptionTree = new PeerSubscriptionTree();
            var peer = new Peer(new PeerId("1"), "endpoint");

            peerSubscriptionTree.IsEmpty.ShouldBeTrue();
            peerSubscriptionTree.Add(peer, BindingKeyHelper.CreateFromString(subscriptionKey, '.'));
            var subscription = BindingKeyHelper.CreateFromString("lol", '.');

            peerSubscriptionTree.Add(peer, subscription);
            peerSubscriptionTree.IsEmpty.ShouldBeFalse();
            peerSubscriptionTree.Remove(peer, BindingKeyHelper.CreateFromString(subscriptionKey, '.'));
            peerSubscriptionTree.IsEmpty.ShouldBeFalse();
            peerSubscriptionTree.Remove(peer, subscription);
            peerSubscriptionTree.IsEmpty.ShouldBeTrue();
        }
        public void empty_bindingkey_should_return_all_subscriptions(string routingKey)
        {
            // Arrange
            var peerSubscriptionTree = new PeerSubscriptionTree();
            var peerA = new Peer(new PeerId("a"), "endpoint");
            var peerB = new Peer(new PeerId("b"), "endpoint");

            peerSubscriptionTree.Add(peerA, BindingKeyHelper.CreateFromString(routingKey, '.'));
            peerSubscriptionTree.Add(peerB, BindingKeyHelper.CreateFromString("foo.bar", '.'));

            // Act
            var matchingPeers = peerSubscriptionTree.GetPeers(BindingKey.Empty);

            // Assert
            matchingPeers.ShouldBeEquivalentTo(peerA, peerB);
        }
        public void roundtrip_test()
        {
            // Arrange
            var peerSubscriptionTree = new PeerSubscriptionTree();
            var peer1 = new Peer(new PeerId("1"), "endpoint");
            var peer2 = new Peer(new PeerId("2"), "endpoint");
            var peer3 = new Peer(new PeerId("3"), "endpoint");
            var peer4 = new Peer(new PeerId("4"), "endpoint");
            var peer5 = new Peer(new PeerId("5"), "endpoint");
            var peer6 = new Peer(new PeerId("6"), "endpoint");
            var peer7 = new Peer(new PeerId("7"), "endpoint");
            var peer8 = new Peer(new PeerId("8"), "endpoint");
            var peer9 = new Peer(new PeerId("9"), "endpoint");
            var peer0 = new Peer(new PeerId("0"), "endpoint");

            peerSubscriptionTree.Add(peer1, BindingKeyHelper.CreateFromString("#", '.'));
            peerSubscriptionTree.Add(peer2, BindingKeyHelper.CreateFromString("a.b", '.'));
            peerSubscriptionTree.Add(peer3, BindingKeyHelper.CreateFromString("a.*", '.'));
            peerSubscriptionTree.Add(peer4, BindingKeyHelper.CreateFromString("b.*.c", '.'));
            peerSubscriptionTree.Add(peer5, BindingKeyHelper.CreateFromString("b.*.f", '.'));
            peerSubscriptionTree.Add(peer6, BindingKeyHelper.CreateFromString("d.*.c", '.'));
            peerSubscriptionTree.Add(peer7, BindingKeyHelper.CreateFromString("a", '.'));
            peerSubscriptionTree.Add(peer8, BindingKeyHelper.CreateFromString("*.*", '.'));
            peerSubscriptionTree.Add(peer9, BindingKeyHelper.CreateFromString("a.#", '.'));
            peerSubscriptionTree.Add(peer0, BindingKeyHelper.CreateFromString("*", '.'));

            // Act - Assert
            var peers = peerSubscriptionTree.GetPeers(BindingKeyHelper.CreateFromString("b.1.c", '.'));

            peers.Count.ShouldEqual(2);
            peers.ShouldContain(peer1);
            peers.ShouldContain(peer4);

            peers = peerSubscriptionTree.GetPeers(BindingKeyHelper.CreateFromString("a.1", '.'));
            peers.Count.ShouldEqual(4);
            peers.ShouldContain(peer1);
            peers.ShouldContain(peer3);
            peers.ShouldContain(peer8);
            peers.ShouldContain(peer9);

            peers = peerSubscriptionTree.GetPeers(BindingKeyHelper.CreateFromString("a", '.'));
            peers.Count.ShouldEqual(3);
            peers.ShouldContain(peer1);
            peers.ShouldContain(peer7);
            peers.ShouldContain(peer0);
        }
        public void exact_same_routing_key_should_match_binding_key()
        {
            var subscription = CreateSubscription("a.b.c");

            subscription.Matches(BindingKeyHelper.CreateFromString("a.b.c", '.')).ShouldBeTrue();
        }
        public void should_not_match_binding_key(string routingKey, string bindingKey)
        {
            var subscription = CreateSubscription(bindingKey);

            subscription.Matches(BindingKeyHelper.CreateFromString(routingKey, '.')).ShouldBeFalse();
        }
        public void binding_key_with_dash_should_match_routing_key(string bindingKey)
        {
            var subscription = CreateSubscription(bindingKey);

            subscription.Matches(BindingKeyHelper.CreateFromString("a.b.c", '.')).ShouldBeTrue();
        }
        public void stars_should_always_match_if_same_number_of_parts(string routingKey)
        {
            var subscription = CreateSubscription("*.*.*");

            subscription.Matches(BindingKeyHelper.CreateFromString(routingKey, '.')).ShouldBeTrue();
        }
        public void empty_bindingKey_should_always_match(string routingKey)
        {
            var subscription = new Subscription(new MessageTypeId(typeof(FakeCommand)), BindingKey.Empty);

            subscription.Matches(BindingKeyHelper.CreateFromString(routingKey, '.')).ShouldBeTrue();
        }
        public void single_dash_should_always_match(string routingKey)
        {
            var subscription = CreateSubscription("#");

            subscription.Matches(BindingKeyHelper.CreateFromString(routingKey, '.')).ShouldBeTrue();
        }
 private Subscription CreateSubscription(string bindingKey)
 {
     return(new Subscription(new MessageTypeId(typeof(FakeCommand)), BindingKeyHelper.CreateFromString(bindingKey, '.')));
 }