Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

Check for null when resolving handlers #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/projects/Routemeister/Dispatchers/AsyncDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public async Task SendAsync(object message)
try
{
var handler = _messageHandlerCreator(action.HandlerType, envelope);
if (handler == null)
throw new InvalidOperationException(
$"Message handler of type {action.HandlerType.FullName} created for message type {action.MessageType.FullName} was null.");
var resultingTask = (Task)action.Invoke(handler, envelope.Message);

await resultingTask.ConfigureAwait(false);
Expand All @@ -59,9 +62,19 @@ public async Task PublishAsync(object message)

try
{
foreach (var action in route.Actions)
var routeActions = route.Actions.Select(a => Tuple.Create(a, _messageHandlerCreator(a.HandlerType, envelope))).ToList();
foreach (var routeAction in routeActions)
{
var handler = _messageHandlerCreator(action.HandlerType, envelope);
var action = routeAction.Item1;
var handler = routeAction.Item2;
if (handler == null)
throw new InvalidOperationException(
$"Message handler of type {action.HandlerType.FullName} created for message type {action.MessageType.FullName} was null.");
}
foreach (var routeAction in routeActions)
{
var action = routeAction.Item1;
var handler = routeAction.Item2;
var resultingTask = (Task)action.Invoke(handler, envelope.Message);

await resultingTask.ConfigureAwait(false);
Expand Down Expand Up @@ -92,6 +105,9 @@ public async Task<TResponse> RequestAsync<TResponse>(IRequest<TResponse> request
try
{
var handler = _messageHandlerCreator(action.HandlerType, envelope);
if (handler == null)
throw new InvalidOperationException(
$"Message handler of type {action.HandlerType.FullName} created for message type {action.MessageType.FullName} was null.");
var resultingTask = (Task<TResponse>)action.Invoke(handler, envelope.Message);

return await resultingTask.ConfigureAwait(false);
Expand Down
20 changes: 18 additions & 2 deletions src/projects/Routemeister/Dispatchers/SyncDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public void Send(object message)
try
{
var handler = _messageHandlerCreator(action.HandlerType, envelope);
if (handler == null)
throw new InvalidOperationException(
$"Message handler of type {action.HandlerType.FullName} created for message type {action.MessageType.FullName} was null.");
action.Invoke(handler, envelope.Message);
}
finally
Expand All @@ -56,9 +59,19 @@ public void Publish(object message)

try
{
foreach (var action in route.Actions)
var routeActions = route.Actions.Select(a => Tuple.Create(a, _messageHandlerCreator(a.HandlerType, envelope))).ToList();
foreach (var routeAction in routeActions)
{
var handler = _messageHandlerCreator(action.HandlerType, envelope);
var action = routeAction.Item1;
var handler = routeAction.Item2;
if (handler == null)
throw new InvalidOperationException(
$"Message handler of type {action.HandlerType.FullName} created for message type {action.MessageType.FullName} was null.");
}
foreach (var routeAction in routeActions)
{
var action = routeAction.Item1;
var handler = routeAction.Item2;
action.Invoke(handler, envelope.Message);
}
}
Expand Down Expand Up @@ -87,6 +100,9 @@ public TResponse Request<TResponse>(IRequest<TResponse> request)
try
{
var handler = _messageHandlerCreator(action.HandlerType, envelope);
if (handler == null)
throw new InvalidOperationException(
$"Message handler of type {action.HandlerType.FullName} created for message type {action.MessageType.FullName} was null.");
var result = (TResponse)action.Invoke(handler, envelope.Message);

return result;
Expand Down
29 changes: 21 additions & 8 deletions src/projects/Routemeister/Routers/MiddlewareEnabledAsyncRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,41 @@ public async Task RouteAsync<T>(T message)
var route = _messageRoutes.GetRoute(message.GetType());
var envelope = new MessageEnvelope(message, route.MessageType);

var routeActions = route.Actions.Select(a => Tuple.Create(a, _messageHandlerCreator(a.HandlerType, envelope))).ToList();
foreach (var routeAction in routeActions)
{
var action = routeAction.Item1;
var handler = routeAction.Item2;
if (handler == null)
throw new InvalidOperationException(
$"Message handler of type {action.HandlerType.FullName} created for message type {action.MessageType.FullName} was null.");
}
if (!_middlewares.Any())
foreach (var action in route.Actions)
{
foreach (var routeAction in routeActions)
{
var handler = _messageHandlerCreator(action.HandlerType, envelope);
var resultingTask = (Task)action.Invoke(handler, envelope.Message);

var action = routeAction.Item1;
var handler = routeAction.Item2;
var resultingTask = (Task) action.Invoke(handler, envelope.Message);
await resultingTask.ConfigureAwait(false);
}
}
else
foreach (var action in route.Actions)
{
foreach (var routeAction in routeActions)
{
var action = routeAction.Item1;
var handler = routeAction.Item2;
await ProcessAsync(
envelope,
async e =>
{
var handler = _messageHandlerCreator(action.HandlerType, envelope);
var resultingTask = (Task)action.Invoke(handler, envelope.Message);

var resultingTask = (Task) action.Invoke(handler, envelope.Message);
await resultingTask.ConfigureAwait(false);
}
).ConfigureAwait(false);
}
}
}

private async Task ProcessAsync(MessageEnvelope envelope, Func<MessageEnvelope, Task> root)
Expand Down
15 changes: 13 additions & 2 deletions src/projects/Routemeister/Routers/SequentialAsyncRouter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Routemeister.Routers
Expand Down Expand Up @@ -32,9 +33,19 @@ public async Task RouteAsync<T>(T message)

try
{
foreach (var action in route.Actions)
var routeActions = route.Actions.Select(a => Tuple.Create(a, _messageHandlerCreator(a.HandlerType, envelope))).ToList();
foreach (var routeAction in routeActions)
{
var handler = _messageHandlerCreator(action.HandlerType, envelope);
var action = routeAction.Item1;
var handler = routeAction.Item2;
if (handler == null)
throw new InvalidOperationException(
$"Message handler of type {action.HandlerType.FullName} created for message type {action.MessageType.FullName} was null.");
}
foreach (var routeAction in routeActions)
{
var action = routeAction.Item1;
var handler = routeAction.Item2;
var resultingTask = (Task)action.Invoke(handler, envelope.Message);

await resultingTask.ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,70 @@ public async Task RequestAsync_Should_invoke_OnBeforeRouting_and_OnAfterRouter_a
interceptedMatchingState.Should().BeTrue();
}

[Fact]
public async Task SendAsync_Should_throw_if_created_handler_is_null()
{
var factory = new MessageRouteFactory();
var routes = new MessageRoutes
{
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IAsyncMessageHandler<>)),
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IAsyncRequestHandler<,>))
};
UnitUnderTest = new AsyncDispatcher((t, e) => null, routes);

var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => UnitUnderTest.SendAsync(new ConcreteMessageA()));

exception.Message.Should().Be($"Message handler of type {typeof(HandlerA).FullName} created for message type {typeof(ConcreteMessageA).FullName} was null.");
}

[Fact]
public async Task PublishAsync_Should_throw_if_created_handler_is_null()
{
var factory = new MessageRouteFactory();
var routes = new MessageRoutes
{
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IAsyncMessageHandler<>)),
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IAsyncRequestHandler<,>))
};
UnitUnderTest = new AsyncDispatcher((t, e) => t == typeof(HandlerB) ? null : Activator.CreateInstance(t), routes);

var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => UnitUnderTest.PublishAsync(new ConcreteMessageB()));

exception.Message.Should().Be($"Message handler of type {typeof(HandlerB).FullName} created for message type {typeof(ConcreteMessageB).FullName} was null.");
}

[Fact]
public async Task PublishAsync_Should_throw_if_other_created_handler_is_null()
{
var factory = new MessageRouteFactory();
var routes = new MessageRoutes
{
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IAsyncMessageHandler<>)),
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IAsyncRequestHandler<,>))
};
UnitUnderTest = new AsyncDispatcher((t, e) => t == typeof(HandlerA) ? null : Activator.CreateInstance(t), routes);

var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => UnitUnderTest.PublishAsync(new ConcreteMessageA()));

exception.Message.Should().Be($"Message handler of type {typeof(HandlerA).FullName} created for message type {typeof(ConcreteMessageA).FullName} was null.");
}

[Fact]
public async Task RequestAsync_Should_throw_if_created_handler_is_null()
{
var factory = new MessageRouteFactory();
var routes = new MessageRoutes
{
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IAsyncMessageHandler<>)),
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IAsyncRequestHandler<,>))
};
UnitUnderTest = new AsyncDispatcher((t, e) => null, routes);

var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => UnitUnderTest.RequestAsync(new RequestMessage()));

exception.Message.Should().Be($"Message handler of type {typeof(HandlerA).FullName} created for message type {typeof(RequestMessage).FullName} was null.");
}

public class ConcreteMessageA
{
public ConcurrentBag<string> Data { get; } = new ConcurrentBag<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,70 @@ public void Request_Should_invoke_OnBeforeRouting_and_OnAfterRouter_and_pass_sta
interceptedMatchingState.Should().BeTrue();
}

[Fact]
public void SendAsync_Should_throw_if_created_handler_is_null()
{
var factory = new MessageRouteFactory();
var routes = new MessageRoutes
{
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IMessageHandler<>)),
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IRequestHandler<,>))
};
UnitUnderTest = new SyncDispatcher((t, e) => null, routes);

var exception = Assert.Throws<InvalidOperationException>(() => UnitUnderTest.Send(new ConcreteMessageA()));

exception.Message.Should().Be($"Message handler of type {typeof(HandlerA).FullName} created for message type {typeof(ConcreteMessageA).FullName} was null.");
}

[Fact]
public void PublishAsync_Should_throw_if_created_handler_is_null()
{
var factory = new MessageRouteFactory();
var routes = new MessageRoutes
{
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IMessageHandler<>)),
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IRequestHandler<,>))
};
UnitUnderTest = new SyncDispatcher((t, e) => t == typeof(HandlerB) ? null : Activator.CreateInstance(t), routes);

var exception = Assert.Throws<InvalidOperationException>(() => UnitUnderTest.Publish(new ConcreteMessageB()));

exception.Message.Should().Be($"Message handler of type {typeof(HandlerB).FullName} created for message type {typeof(ConcreteMessageB).FullName} was null.");
}

[Fact]
public void PublishAsync_Should_throw_if_other_created_handler_is_null()
{
var factory = new MessageRouteFactory();
var routes = new MessageRoutes
{
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IMessageHandler<>)),
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IRequestHandler<,>))
};
UnitUnderTest = new SyncDispatcher((t, e) => t == typeof(HandlerA) ? null : Activator.CreateInstance(t), routes);

var exception = Assert.Throws<InvalidOperationException>(() => UnitUnderTest.Publish(new ConcreteMessageA()));

exception.Message.Should().Be($"Message handler of type {typeof(HandlerA).FullName} created for message type {typeof(ConcreteMessageA).FullName} was null.");
}

[Fact]
public void RequestAsync_Should_throw_if_created_handler_is_null()
{
var factory = new MessageRouteFactory();
var routes = new MessageRoutes
{
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IMessageHandler<>)),
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IRequestHandler<,>))
};
UnitUnderTest = new SyncDispatcher((t, e) => null, routes);

var exception = Assert.Throws<InvalidOperationException>(() => UnitUnderTest.Request(new RequestMessage()));

exception.Message.Should().Be($"Message handler of type {typeof(HandlerA).FullName} created for message type {typeof(RequestMessage).FullName} was null.");
}

public class ConcreteMessageA
{
public ConcurrentBag<string> Data { get; } = new ConcurrentBag<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ public async Task Should_be_able_to_pass_envelope_state()
"MW2 in MW1 is 'MW2 of ConcreteMessageB'");
}

[Fact]
public async Task Should_throw_if_created_handler_is_null()
{
var factory = new MessageRouteFactory();
var routes = new MessageRoutes
{
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IHandle<>))
};
UnitUnderTest = new MiddlewareEnabledAsyncRouter((t, e) => null, routes);

var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => UnitUnderTest.RouteAsync(new ConcreteMessageA()));

exception.Message.Should().Be($"Message handler of type {typeof(HandlerA).FullName} created for message type {typeof(ConcreteMessageA).FullName} was null.");
}

public interface IHandle<in T>
{
Task HandleAsync(T message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ public async Task Should_invoke_OnBeforeRouting_and_OnAfterRouter_and_pass_state
interceptedMatchingState.Should().BeTrue();
}

[Fact]
public async Task Should_throw_if_created_handler_is_null()
{
var factory = new MessageRouteFactory();
var routes = new MessageRoutes
{
factory.Create(new[] {GetType().GetTypeInfo().Assembly}, typeof (IHandle<>))
};
UnitUnderTest = new SequentialAsyncRouter((t, e) => null, routes);

var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => UnitUnderTest.RouteAsync(new ConcreteMessageA()));

exception.Message.Should().Be($"Message handler of type {typeof(HandlerA).FullName} created for message type {typeof(ConcreteMessageA).FullName} was null.");
}

public interface IHandle<in T>
{
Task HandleAsync(T message);
Expand Down