Esempio n. 1
0
        public void Should_be_able_to_autosubscribe_to_several_messages_in_one_consumer()
        {
            var interceptedSubscriptions = new List <Tuple <string, Delegate> >();
            var busFake = new BusFake
            {
                InterceptSubscribe = (s, a) => interceptedSubscriptions.Add(new Tuple <string, Delegate>(s, a))
            };
            var autoSubscriber = new AutoSubscriber(busFake, "MyAppPrefix");

            autoSubscriber.Subscribe(GetType().Assembly);

            interceptedSubscriptions.Count.ShouldEqual(4);
            interceptedSubscriptions.TrueForAll(i => i.Item2.Method.DeclaringType == typeof(DefaultMessageDispatcher)).ShouldBeTrue();

            CheckSubscriptionsContains <MessageA>(interceptedSubscriptions, "MyAppPrefix:e8afeaac27aeba31a42dea8e4d05308e");
            CheckSubscriptionsContains <MessageB>(interceptedSubscriptions, "MyExplicitId");
            CheckSubscriptionsContains <MessageC>(interceptedSubscriptions, "MyAppPrefix:cf5f54ed13478763e2da2bb3c9487baa");

            var messageADispatcher = (Action <MessageA>)interceptedSubscriptions.Single(x => x.Item2.GetType().GetGenericArguments()[0] == typeof(MessageA)).Item2;
            var message            = new MessageA {
                Text = "Hello World"
            };

            messageADispatcher(message);
        }
Esempio n. 2
0
        public void Should_be_able_to_take_control_of_subscription_id_generation()
        {
            var interceptedSubscriptions = new List <Tuple <string, Delegate> >();
            var busFake = new BusFake
            {
                InterceptSubscribe = (s, a) => interceptedSubscriptions.Add(new Tuple <string, Delegate>(s, a))
            };
            var fixedSubscriptionIds = new[] { "2f481170-8bc4-4d0f-a972-bd45191b1706", "be4ac633-ea29-4ed0-a0bf-419a01a0e9d4" };
            var callCount            = 0;
            var autoSubscriber       = new AutoSubscriber(busFake, "MyAppPrefix")
            {
                GenerateSubscriptionId = c => fixedSubscriptionIds[callCount++]
            };

            autoSubscriber.Subscribe(GetType().Assembly);

            interceptedSubscriptions.Count.ShouldEqual(3);
            interceptedSubscriptions[0].Item1.ShouldEqual(fixedSubscriptionIds[0]);
            interceptedSubscriptions[0].Item2.Method.GetParameters()[0].ParameterType.ShouldEqual(typeof(MessageA));

            interceptedSubscriptions[1].Item1.ShouldEqual("MyExplicitId");
            interceptedSubscriptions[1].Item2.Method.GetParameters()[0].ParameterType.ShouldEqual(typeof(MessageB));

            interceptedSubscriptions[2].Item1.ShouldEqual(fixedSubscriptionIds[1]);
            interceptedSubscriptions[2].Item2.Method.GetParameters()[0].ParameterType.ShouldEqual(typeof(MessageC));
        }
Esempio n. 3
0
        public void Should_be_able_to_autosubscribe_with_async_to_several_messages_in_one_consumer()
        {
            var interceptedSubscriptions = new List <Tuple <string, Delegate> >();
            var busFake = new BusFake
            {
                InterceptSubscribe = (s, a) => interceptedSubscriptions.Add(new Tuple <string, Delegate>(s, a))
            };
            var autoSubscriber = new AutoSubscriber(busFake, "MyAppPrefix");

            autoSubscriber.SubscribeAsync(GetType().Assembly);

            interceptedSubscriptions.Count.ShouldEqual(3);
            interceptedSubscriptions.TrueForAll(i => i.Item2.Method.DeclaringType == typeof(DefaultAutoSubscriberMessageDispatcher)).ShouldBeTrue();

            CheckSubscriptionsContains <MessageA>(interceptedSubscriptions, "MyAppPrefix:595a495413330ce1a7d03dd6a434b599");
            CheckSubscriptionsContains <MessageB>(interceptedSubscriptions, "MyExplicitId");
            CheckSubscriptionsContains <MessageC>(interceptedSubscriptions, "MyAppPrefix:e65118ba1611619fa7afb53dc916866e");

            var messageADispatcher = (Func <MessageA, Task>)interceptedSubscriptions.Single(x => x.Item2.GetType().GetGenericArguments()[0] == typeof(MessageA)).Item2;
            var message            = new MessageA {
                Text = "Hello World"
            };

            messageADispatcher(message);
            MyAsyncConsumer.MessageAText.ShouldEqual("Hello World");
        }
Esempio n. 4
0
        public void Should_be_able_to_take_control_of_subscription_id_generation()
        {
            var interceptedSubscriptions = new List <Tuple <string, Delegate> >();
            var busFake = new BusFake
            {
                InterceptSubscribe = (s, a) => interceptedSubscriptions.Add(new Tuple <string, Delegate>(s, a))
            };

            var autoSubscriber = new AutoSubscriber(busFake, "MyAppPrefix")
            {
                GenerateSubscriptionId = c => c.MessageType.Name.ToString(CultureInfo.InvariantCulture)
            };

            autoSubscriber.Subscribe(GetType().Assembly);

            interceptedSubscriptions.Count.ShouldEqual(4);

            CheckSubscriptionsContains <MessageA>(interceptedSubscriptions, "MessageA");
            CheckSubscriptionsContains <MessageB>(interceptedSubscriptions, "MyExplicitId");
            CheckSubscriptionsContains <MessageC>(interceptedSubscriptions, "MessageC");
        }
Esempio n. 5
0
        public void Should_be_able_to_autosubscribe_to_several_messages_in_one_consumer_with_custom_interface()
        {
            var interceptedSubscriptions = new List <Tuple <string, Delegate> >();
            var busFake = new BusFake
            {
                InterceptSubscribe = (s, a) => interceptedSubscriptions.Add(new Tuple <string, Delegate>(s, a))
            };
            var autoSubscriber = new AutoSubscriber(busFake, "MyAppPrefix");

            autoSubscriber.Subscribe(typeof(IConsumeCustom <>), GetType().Assembly);

            interceptedSubscriptions.Count.ShouldEqual(3);
            interceptedSubscriptions.TrueForAll(i => i.Item2.Method.DeclaringType == typeof(MyConsumerWithCustomInterface)).ShouldBeTrue();

            interceptedSubscriptions[0].Item1.ShouldEqual("MyAppPrefix:63c317b761366d57679a8bb0f7fa925a");
            interceptedSubscriptions[0].Item2.Method.GetParameters()[0].ParameterType.ShouldEqual(typeof(MessageA));

            interceptedSubscriptions[1].Item1.ShouldEqual("MyExplicitId");
            interceptedSubscriptions[1].Item2.Method.GetParameters()[0].ParameterType.ShouldEqual(typeof(MessageB));

            interceptedSubscriptions[2].Item1.ShouldEqual("MyAppPrefix:813fd8f08e61068e054dcff403da5ce7");
            interceptedSubscriptions[2].Item2.Method.GetParameters()[0].ParameterType.ShouldEqual(typeof(MessageC));
        }
Esempio n. 6
0
        public void Should_be_able_to_autosubscribe_to_several_messages_in_one_consumer()
        {
            var interceptedSubscriptions = new List <Tuple <string, Delegate> >();
            var busFake = new BusFake
            {
                InterceptSubscribe = (s, a) => interceptedSubscriptions.Add(new Tuple <string, Delegate>(s, a))
            };
            var autoSubscriber = new AutoSubscriber(busFake, "MyAppPrefix");

            autoSubscriber.Subscribe(GetType().Assembly);

            interceptedSubscriptions.Count.ShouldEqual(3);
            interceptedSubscriptions.TrueForAll(i => i.Item2.Method.DeclaringType == typeof(MyConsumer)).ShouldBeTrue();

            interceptedSubscriptions[0].Item1.ShouldEqual("MyAppPrefix:e8afeaac27aeba31a42dea8e4d05308e");
            interceptedSubscriptions[0].Item2.Method.GetParameters()[0].ParameterType.ShouldEqual(typeof(MessageA));

            interceptedSubscriptions[1].Item1.ShouldEqual("MyExplicitId");
            interceptedSubscriptions[1].Item2.Method.GetParameters()[0].ParameterType.ShouldEqual(typeof(MessageB));

            interceptedSubscriptions[2].Item1.ShouldEqual("MyAppPrefix:cf5f54ed13478763e2da2bb3c9487baa");
            interceptedSubscriptions[2].Item2.Method.GetParameters()[0].ParameterType.ShouldEqual(typeof(MessageC));
        }
Esempio n. 7
0
        public void Should_be_able_to_use_a_custom_message_dispatcher()
        {
            var interceptedSubscriptions = new List <Tuple <string, Delegate> >();
            var busFake = new BusFake
            {
                InterceptSubscribe = (s, a) => interceptedSubscriptions.Add(new Tuple <string, Delegate>(s, a))
            };

            var dispatcher = new CustomMessageDispatcher();

            var autoSubscriber = new AutoSubscriber(busFake, "MyAppPrefix")
            {
                MessageDispatcher = dispatcher
            };

            autoSubscriber.Subscribe(GetType().Assembly);

            var messageADispatcher = (Action <MessageA>)interceptedSubscriptions.Single(x => x.Item2.GetType().GetGenericArguments()[0] == typeof(MessageA)).Item2;
            var message            = new MessageA();

            messageADispatcher(message);

            dispatcher.DispatchedMessage.ShouldBeTheSameAs(message);
        }