예제 #1
0
        public void MutatorPreservesPreviousMessageRoutingProperty()
        {
            var          timestamp              = DateTime.Now.UnixNano();
            string       originalTopicName      = string.Format("test_message_router_mutator_{0}", timestamp);
            string       topicName1             = string.Format("{0}_1", originalTopicName);
            string       topicName2             = string.Format("{0}_2", originalTopicName);
            string       childOriginalTopicName = string.Format("test_message_router_mutator_child_{0}", timestamp);
            string       childTopicName1        = string.Format("{0}_1", childOriginalTopicName);
            string       childTopicName2        = string.Format("{0}_2", childOriginalTopicName);
            const string channelName            = "test_message_router_mutator";

            var container = new Container();

            Console.WriteLine(originalTopicName);
            Console.WriteLine(topicName1);
            Console.WriteLine(topicName2);
            Console.WriteLine(childOriginalTopicName);
            Console.WriteLine(childTopicName1);
            Console.WriteLine(childTopicName2);

            _nsqLookupdHttpClient.CreateTopic(originalTopicName);
            _nsqLookupdHttpClient.CreateChannel(originalTopicName, channelName);

            _nsqLookupdHttpClient.CreateTopic(topicName1);
            _nsqLookupdHttpClient.CreateChannel(topicName1, channelName);

            _nsqLookupdHttpClient.CreateTopic(topicName2);
            _nsqLookupdHttpClient.CreateChannel(topicName2, channelName);

            _nsqLookupdHttpClient.CreateTopic(childOriginalTopicName);
            _nsqLookupdHttpClient.CreateChannel(childOriginalTopicName, channelName);

            _nsqLookupdHttpClient.CreateTopic(childTopicName1);
            _nsqLookupdHttpClient.CreateChannel(childTopicName1, channelName);

            _nsqLookupdHttpClient.CreateTopic(childTopicName2);
            _nsqLookupdHttpClient.CreateChannel(childTopicName2, channelName);

            try
            {
                var messageTypeToTopicProvider = new MessageTypeToTopicDictionary(new Dictionary <Type, string> {
                    { typeof(MyRoutedMessage), originalTopicName },
                    { typeof(MyMutatedRoutedMessage), childOriginalTopicName },
                });

                BusService.Start(new BusConfiguration(
                                     new StructureMapObjectBuilder(container),
                                     new NewtonsoftJsonSerializer(typeof(JsonConverter).Assembly),
                                     new MessageAuditorStub(),
                                     messageTypeToTopicProvider,
                                     new HandlerTypeToChannelDictionary(new Dictionary <Type, string> {
                    { typeof(MyRoutedMessageHandler), channelName },
                    { typeof(MyMutatedRoutedMessageHandler), channelName },
                }),
                                     defaultNsqLookupdHttpEndpoints: new[] { "127.0.0.1:4161" },
                                     defaultThreadsPerHandler: 1,
                                     nsqConfig: new Config
                {
                    MaxRequeueDelay     = TimeSpan.Zero,
                    LookupdPollJitter   = 0,
                    LookupdPollInterval = TimeSpan.FromSeconds(1)
                },
                                     preCreateTopicsAndChannels: true,
                                     messageTopicRouter: new MessageTopicRouter(messageTypeToTopicProvider),
                                     messageMutator: new MessageMutator()
                                     ));

                var bus = container.GetInstance <IBus>();

                bus.Send(new MyRoutedMessage {
                    Text = "One"
                });
                bus.Send(new MyRoutedMessage {
                    RouteIndex = 1, Text = "Two"
                });
                bus.Send(new MyRoutedMessage {
                    RouteIndex = 2, Text = "Three"
                });

                var dict1 = MyRoutedMessageHandler.GetReceived();
                var dict2 = MyMutatedRoutedMessageHandler.GetReceived();

                Assert.AreEqual(3, dict1.Count, "dict.Count");
                Assert.AreEqual(3, dict2.Count, "dict2.Count");

                var firstMessage  = dict1.Single(p => p.Value.Text == "One");
                var secondMessage = dict1.Single(p => p.Value.Text == "Two");
                var thirdMessage  = dict1.Single(p => p.Value.Text == "Three");

                var childFirstMessage  = dict2.Single(p => p.Value.Text == "One Child");
                var childSecondMessage = dict2.Single(p => p.Value.Text == "Two Child");
                var childThirdMessage  = dict2.Single(p => p.Value.Text == "Three Child");

                Console.WriteLine(firstMessage.Key.UniqueIdentifier);
                Console.WriteLine(firstMessage.Value.Text);
                Console.WriteLine(secondMessage.Key.UniqueIdentifier);
                Console.WriteLine(secondMessage.Value.Text);
                Console.WriteLine(thirdMessage.Key.UniqueIdentifier);
                Console.WriteLine(thirdMessage.Value.Text);

                Console.WriteLine(childFirstMessage.Value.ParentId);
                Console.WriteLine(childFirstMessage.Value.Text);
                Console.WriteLine(childSecondMessage.Value.ParentId);
                Console.WriteLine(childSecondMessage.Value.Text);
                Console.WriteLine(childThirdMessage.Value.ParentId);
                Console.WriteLine(childThirdMessage.Value.Text);

                Assert.AreEqual("One", firstMessage.Value.Text, "firstMessage.Value.Text");
                Assert.AreEqual("Two", secondMessage.Value.Text, "secondMessage.Value.Text");
                Assert.AreEqual("Three", thirdMessage.Value.Text, "thirdMessage.Value.Text");

                Assert.AreEqual(firstMessage.Key.UniqueIdentifier.ToString(), childFirstMessage.Value.ParentId,
                                "childFirstMessage.Value.ParentId");
                Assert.AreEqual("One Child", childFirstMessage.Value.Text, "childFirstMessage.Value.Text");
                Assert.AreEqual(secondMessage.Key.UniqueIdentifier.ToString(), childSecondMessage.Value.ParentId,
                                "childSecondMessage.Value.ParentId");
                Assert.AreEqual("Two Child", childSecondMessage.Value.Text, "childSecondMessage.Value.Text");
                Assert.AreEqual(thirdMessage.Key.UniqueIdentifier.ToString(), childThirdMessage.Value.ParentId,
                                "childThirdMessage.Value.ParentId");
                Assert.AreEqual("Three Child", childThirdMessage.Value.Text, "childThirdMessage.Value.Text");

                // get stats from http server
                var stats = _nsqdHttpClient.GetStats();

                foreach (var topicName in new[] {
                    originalTopicName, topicName1, topicName2,
                    childOriginalTopicName, childTopicName1, childTopicName2
                })
                {
                    // assert received message topic/message match expectations
                    IMessageWithRouteIndex receivedMessage;
                    if (dict1.Any(p => p.Key.Topic == topicName))
                    {
                        receivedMessage = dict1.Single(p => p.Key.Topic == topicName).Value;
                    }
                    else
                    {
                        receivedMessage = dict2.Single(p => p.Key.Topic == topicName).Value;
                    }

                    int?expectedRouteIndex;
                    if (topicName == topicName1 || topicName == childTopicName1)
                    {
                        expectedRouteIndex = 1;
                    }
                    else if (topicName == topicName2 || topicName == childTopicName2)
                    {
                        expectedRouteIndex = 2;
                    }
                    else
                    {
                        expectedRouteIndex = null;
                    }

                    Assert.AreEqual(expectedRouteIndex, receivedMessage.RouteIndex, "expectedRouteIndex");

                    // assert stats from http server
                    var topic   = stats.Topics.Single(p => p.TopicName == topicName);
                    var channel = topic.Channels.Single(p => p.ChannelName == channelName);

                    Assert.AreEqual(1, topic.MessageCount, "topic.MessageCount");
                    Assert.AreEqual(0, topic.Depth, "topic.Depth");
                    Assert.AreEqual(0, topic.BackendDepth, "topic.BackendDepth");

                    Assert.AreEqual(1, channel.MessageCount, "channel.MessageCount");
                    Assert.AreEqual(0, channel.DeferredCount, "channel.DeferredCount");
                    Assert.AreEqual(0, channel.Depth, "channel.Depth");
                    Assert.AreEqual(0, channel.BackendDepth, "channel.BackendDepth");
                    Assert.AreEqual(0, channel.InFlightCount, "channel.InFlightCount");
                    Assert.AreEqual(0, channel.TimeoutCount, "channel.TimeoutCount");
                    Assert.AreEqual(0, channel.RequeueCount, "channel.RequeueCount");
                }
            }
            finally
            {
                BusService.Stop();

                _nsqdHttpClient.DeleteTopic(originalTopicName);
                _nsqLookupdHttpClient.DeleteTopic(originalTopicName);

                _nsqdHttpClient.DeleteTopic(topicName1);
                _nsqLookupdHttpClient.DeleteTopic(topicName1);

                _nsqdHttpClient.DeleteTopic(topicName2);
                _nsqLookupdHttpClient.DeleteTopic(topicName2);

                _nsqdHttpClient.DeleteTopic(childOriginalTopicName);
                _nsqLookupdHttpClient.DeleteTopic(childOriginalTopicName);

                _nsqdHttpClient.DeleteTopic(childTopicName1);
                _nsqLookupdHttpClient.DeleteTopic(childTopicName1);

                _nsqdHttpClient.DeleteTopic(childTopicName2);
                _nsqLookupdHttpClient.DeleteTopic(childTopicName2);
            }
        }
예제 #2
0
        public void RoutingByProperty()
        {
            var          timestamp         = DateTime.Now.UnixNano();
            string       originalTopicName = string.Format("test_message_router_{0}", timestamp);
            string       topicName1        = string.Format("{0}_1", originalTopicName);
            string       topicName2        = string.Format("{0}_2", originalTopicName);
            const string channelName       = "test_message_router";

            var container = new Container();

            _nsqLookupdHttpClient.CreateTopic(originalTopicName);
            _nsqLookupdHttpClient.CreateChannel(originalTopicName, channelName);

            _nsqLookupdHttpClient.CreateTopic(topicName1);
            _nsqLookupdHttpClient.CreateChannel(topicName1, channelName);

            _nsqLookupdHttpClient.CreateTopic(topicName2);
            _nsqLookupdHttpClient.CreateChannel(topicName2, channelName);

            try
            {
                var messageTypeToTopicProvider = new MessageTypeToTopicDictionary(new Dictionary <Type, string> {
                    { typeof(MyRoutedMessage), originalTopicName }
                });

                BusService.Start(new BusConfiguration(
                                     new StructureMapObjectBuilder(container),
                                     new NewtonsoftJsonSerializer(typeof(JsonConverter).Assembly),
                                     new MessageAuditorStub(),
                                     messageTypeToTopicProvider,
                                     new HandlerTypeToChannelDictionary(new Dictionary <Type, string> {
                    { typeof(MyRoutedMessageHandler), channelName }
                }),
                                     defaultNsqLookupdHttpEndpoints: new[] { "127.0.0.1:4161" },
                                     defaultThreadsPerHandler: 1,
                                     nsqConfig: new Config
                {
                    MaxRequeueDelay     = TimeSpan.Zero,
                    LookupdPollJitter   = 0,
                    LookupdPollInterval = TimeSpan.FromSeconds(1)
                },
                                     preCreateTopicsAndChannels: true,
                                     messageTopicRouter: new MessageTopicRouter(messageTypeToTopicProvider)
                                     ));

                var bus = container.GetInstance <IBus>();

                bus.Send(new MyRoutedMessage());
                bus.Send(new MyRoutedMessage {
                    RouteIndex = 1
                });
                bus.Send(new MyRoutedMessage {
                    RouteIndex = 2
                });

                var dict = MyRoutedMessageHandler.GetReceived();

                Assert.AreEqual(3, dict.Count, "dict.Count");

                // get stats from http server
                var stats = _nsqdHttpClient.GetStats();

                foreach (var topicName in new[] { originalTopicName, topicName1, topicName2 })
                {
                    // assert received message topic/message match expectations
                    var receivedMessage = dict.Single(p => p.Key.Topic == topicName);

                    int expectedRouteIndex;
                    if (topicName == topicName1)
                    {
                        expectedRouteIndex = 1;
                    }
                    else if (topicName == topicName2)
                    {
                        expectedRouteIndex = 2;
                    }
                    else
                    {
                        expectedRouteIndex = 0;
                    }

                    Assert.AreEqual(expectedRouteIndex, receivedMessage.Value.RouteIndex, "expectedRouteIndex");

                    // assert stats from http server
                    var topic   = stats.Topics.Single(p => p.TopicName == topicName);
                    var channel = topic.Channels.Single(p => p.ChannelName == channelName);

                    Assert.AreEqual(1, topic.MessageCount, "topic.MessageCount");
                    Assert.AreEqual(0, topic.Depth, "topic.Depth");
                    Assert.AreEqual(0, topic.BackendDepth, "topic.BackendDepth");

                    Assert.AreEqual(1, channel.MessageCount, "channel.MessageCount");
                    Assert.AreEqual(0, channel.DeferredCount, "channel.DeferredCount");
                    Assert.AreEqual(0, channel.Depth, "channel.Depth");
                    Assert.AreEqual(0, channel.BackendDepth, "channel.BackendDepth");
                    Assert.AreEqual(0, channel.InFlightCount, "channel.InFlightCount");
                    Assert.AreEqual(0, channel.TimeoutCount, "channel.TimeoutCount");
                    Assert.AreEqual(0, channel.RequeueCount, "channel.RequeueCount");
                }
            }
            finally
            {
                BusService.Stop();

                _nsqdHttpClient.DeleteTopic(originalTopicName);
                _nsqLookupdHttpClient.DeleteTopic(originalTopicName);

                _nsqdHttpClient.DeleteTopic(topicName1);
                _nsqLookupdHttpClient.DeleteTopic(topicName1);

                _nsqdHttpClient.DeleteTopic(topicName2);
                _nsqLookupdHttpClient.DeleteTopic(topicName2);
            }
        }