コード例 #1
0
        public void AutofacContainerTest()
        {
            string       topicName   = string.Format("test_autofac_{0}", DateTime.Now.UnixNano());
            const string channelName = "test_autofac";

            var builder = new ContainerBuilder();

            builder.RegisterType <TestDependency>().As <ITestDependency>();
            var container = builder.Build();

            try
            {
                var objectBuilder = new AutofacObjectBuilder(container);

                var busConfiguration =
                    new BusConfiguration(
                        objectBuilder,
                        new NewtonsoftJsonSerializer(typeof(JsonConverter).Assembly),
                        new MessageAuditorStub(),
                        new MessageTypeToTopicDictionary(new Dictionary <Type, string>
                {
                    { typeof(TestMessage), topicName }
                }),
                        new HandlerTypeToChannelDictionary(new Dictionary <Type, string>
                {
                    { typeof(TestMessageHandler), channelName }
                }),
                        defaultNsqLookupdHttpEndpoints: new[] { "127.0.0.1:4161" },
                        defaultThreadsPerHandler: 1,
                        nsqConfig: new Config
                {
                    LookupdPollJitter   = 0,
                    LookupdPollInterval = TimeSpan.FromSeconds(1)
                },
                        preCreateTopicsAndChannels: true);

                BusService.Start(busConfiguration);

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

                var testMessage = new TestMessage {
                    Message = "Hello!"
                };
                bus.Send(testMessage);

                var json      = JsonConvert.SerializeObject(testMessage);
                var jsonBytes = Encoding.UTF8.GetBytes(json);

                var tuple              = TestMessageHandler.GetMessageInfo();
                var message            = tuple.Item1;
                var messageInformation = tuple.Item2;

                Assert.IsNotNull(message, "message");
                Assert.IsNotNull(messageInformation, "messageInformation");

                Assert.AreSame(message, messageInformation.Message);

                Assert.IsNotNull(message.Body, "message.Body");
                Assert.AreEqual(jsonBytes, message.Body, "message.Body");

                Assert.IsInstanceOf <TestMessage>(messageInformation.DeserializedMessageBody);
                Assert.AreEqual(testMessage.Message, ((TestMessage)messageInformation.DeserializedMessageBody).Message);
                Assert.IsNull(testMessage.ModifiedMessage);

                Assert.AreEqual(topicName, messageInformation.Topic, "messageInformation.Topic");
                Assert.AreEqual(channelName, messageInformation.Channel, "messageInformation.Channel");
                Assert.AreNotEqual(Guid.Empty,
                                   messageInformation.UniqueIdentifier,
                                   "messageInformation.UniqueIdentifier");
            }
            finally
            {
                BusService.Stop();
                _nsqdHttpClient.DeleteTopic(topicName);
                _nsqLookupdHttpClient.DeleteTopic(topicName);
            }
        }
コード例 #2
0
        public void Given_A_Handler_When_GetCurrentMessageInformation_Called_The_Result_Should_Be_Populated()
        {
            string       topicName   = string.Format("test_getcurrentmessageinformation_{0}", DateTime.Now.UnixNano());
            const string channelName = "test_getcurrentmessageinformation";

            var container = new Container();

            _nsqdHttpClient.CreateTopic(topicName);
            _nsqLookupdHttpClient.CreateTopic(topicName);

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

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

                var testMessage = new TestMessage {
                    Message = "Hello!"
                };
                bus.Send(testMessage);

                var json      = JsonConvert.SerializeObject(testMessage);
                var jsonBytes = Encoding.UTF8.GetBytes(json);

                var tuple              = TestMessageHandler.GetMessageInfo();
                var message            = tuple.Item1;
                var messageInformation = tuple.Item2;

                Assert.IsNotNull(message, "message");
                Assert.IsNotNull(messageInformation, "messageInformation");

                Assert.AreSame(message, messageInformation.Message);

                Assert.IsNotNull(message.Body, "message.Body");
                Assert.AreEqual(jsonBytes, message.Body, "message.Body");

                Assert.AreEqual(topicName, messageInformation.Topic, "messageInformation.Topic");
                Assert.AreEqual(channelName, messageInformation.Channel, "messageInformation.Channel");
                Assert.AreNotEqual(Guid.Empty, messageInformation.UniqueIdentifier, "messageInformation.UniqueIdentifier");
            }
            finally
            {
                BusService.Stop();
                _nsqdHttpClient.DeleteTopic(topicName);
                _nsqLookupdHttpClient.DeleteTopic(topicName);
            }
        }