Skip to content
Browse files

Tidy up and make tests more robust.

  • Loading branch information...
1 parent 7116ede commit b798e1545c3f8171c2c2fbece2d8954b7e505b9c @danbarua committed
Showing with 9 additions and 23 deletions.
  1. +8 −11 Source/EasyNetQ.Tests/AutoSubscriberTests.cs
  2. +1 −12 Source/EasyNetQ/AutoSubscriber.cs
View
19 Source/EasyNetQ.Tests/AutoSubscriberTests.cs
@@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
+using System.Linq.Expressions;
using System.Threading.Tasks;
using EasyNetQ.FluentConfiguration;
using NUnit.Framework;
@@ -108,10 +109,10 @@ public void Should_be_able_to_use_a_custom_message_dispatcher()
[Test]
public void Should_be_able_to_subscribe_asynchronously()
{
- var interceptedSubscriptions = new List<Tuple<string, Delegate>>();
+ var interceptedSubscriptions = new List<Tuple<string, Type>>();
var busFake = new BusFake
{
- InterceptSubscribeAsync = (s, a) => interceptedSubscriptions.Add(new Tuple<string, Delegate>(s, a))
+ InterceptSubscribeAsync = (s, a) => interceptedSubscriptions.Add(new Tuple<string, Type>(s, a))
};
var autoSubscriber = new AutoSubscriber(busFake, "MyAppPrefix");
@@ -122,10 +123,6 @@ public void Should_be_able_to_subscribe_asynchronously()
CheckAsyncSubscriptionsContains<MessageA>(interceptedSubscriptions, "MyAppPrefix:e8afeaac27aeba31a42dea8e4d05308e");
CheckAsyncSubscriptionsContains<MessageB>(interceptedSubscriptions, "MyExplicitId");
CheckAsyncSubscriptionsContains<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);
}
/// <summary>
@@ -135,11 +132,11 @@ public void Should_be_able_to_subscribe_asynchronously()
/// <typeparam name="MessageType"></typeparam>
/// <param name="subscriptions"></param>
/// <param name="subscriptionId"></param>
- private void CheckAsyncSubscriptionsContains<MessageType>(IEnumerable<Tuple<string, Delegate>> subscriptions, string subscriptionId)
+ private void CheckAsyncSubscriptionsContains<MessageType>(IEnumerable<Tuple<string, Type>> subscriptions, string subscriptionId)
{
var contains = subscriptions.Any(x =>
- x.Item1 == subscriptionId);
- //&& x.Item2.Method.GetGenericArguments()[0] == typeof(MessageType));
+ x.Item1 == subscriptionId
+ && x.Item2 == typeof (MessageType));
contains.ShouldBeTrue(string.Format(
"Subscription '{0}' of type {1} not found.", subscriptionId, typeof(MessageType).Name));
@@ -178,7 +175,7 @@ private class BusFake : IBus
{
public Action<string, Delegate> InterceptSubscribe;
- public Action<string, Delegate> InterceptSubscribeAsync;
+ public Action<string, Type> InterceptSubscribeAsync;
public void Dispose()
{
@@ -209,7 +206,7 @@ public void Subscribe<T>(string subscriptionId, Action<T> onMessage, Action<ISub
public void SubscribeAsync<T>(string subscriptionId, Func<T, Task> onMessage)
{
if (InterceptSubscribeAsync != null)
- InterceptSubscribeAsync(subscriptionId, onMessage);
+ InterceptSubscribeAsync(subscriptionId, typeof(T));
}
public void SubscribeAsync<T>(string subscriptionId, Func<T, Task> onMessage, Action<ISubscriptionConfiguration<T>> configure)
View
13 Source/EasyNetQ/AutoSubscriber.cs
@@ -157,18 +157,9 @@ protected MethodInfo GetSubscribeAsyncMethodOfBus()
.Select(m => new { Method = m, Params = m.GetParameters() })
.Single(m => m.Params.Length == 2
&& m.Params[0].ParameterType == typeof(string)
- //&& m.Params[1].ParameterType.GetGenericTypeDefinition() == typeof(Func<>) //<-- not sure how to pull this off?
+ && m.Params[1].ParameterType.GetGenericArguments()[1] == typeof(Task)
).Method;
return info;
-
- /////https://gist.github.com/jonnii/4714812 this doesn't work - we don't know TMessage at compile-time for Func<TMessage,Task>
- //return GetMethod<IBus>(b => b.SubscribeAsync((string)null, (Func<int, Task>)null));
- }
-
- private static MethodInfo GetMethod<T>(Expression<Action<T>> methodSelector)
- {
- var body = (MethodCallExpression)methodSelector.Body;
- return body.Method;
}
protected virtual ConsumerAttribute GetSubscriptionAttribute(ConsumerInfo consumerInfo)
@@ -191,7 +182,5 @@ protected virtual ConsumerAttribute GetSubscriptionAttribute(ConsumerInfo consum
yield return new KeyValuePair<Type, ConsumerInfo[]>(concreteType, subscriptionInfos);
}
}
-
-
}
}

0 comments on commit b798e15

Please sign in to comment.
Something went wrong with that request. Please try again.