Skip to content

Commit

Permalink
Support inheritance in completion conditions (#104)
Browse files Browse the repository at this point in the history
* Support inheritance in MessageWasProcessedByHandler

* minor styling tweaks

* simpler tests

* Support inheritance in MessageWasProcessed

* Support inheritance in MessageWasProcessedBySaga
  • Loading branch information
mauroservienti authored Oct 17, 2020
1 parent 7411607 commit ceed0b6
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.Threading.Tasks;
using NUnit.Framework;

namespace NServiceBus.IntegrationTesting.Tests
{
public class When_capturing_handlers_invocations
{
class TestMessage : IMessage
{

}

interface IMessageInterface: IMessage
{

}

class InheritedMessage : IMessageInterface
{

}

class TestMessageHandler : IHandleMessages<TestMessage>
{
public Task Handle(TestMessage message, IMessageHandlerContext context)
{
return Task.CompletedTask;
}
}

class MessageInterfaceHandler : IHandleMessages<IMessageInterface>
{
public Task Handle(IMessageInterface message, IMessageHandlerContext context)
{
return Task.CompletedTask;
}
}

[Test]
public void MessageWasProcessedByHandler_condition_should_match_type()
{
var scenarioContext = new IntegrationScenarioContext();
scenarioContext.CaptureInvokedHandler(new HandlerInvocation()
{
Message = new TestMessage(),
EndpointName = "fake-endpoint",
HandlerType = typeof(TestMessageHandler),
HandlingError = null,
MessageType = typeof(TestMessage)
});

Assert.IsTrue(scenarioContext.MessageWasProcessedByHandler<TestMessage, TestMessageHandler>());
}

[Test]
public void MessageWasProcessedByHandler_condition_should_match_base_type()
{
var scenarioContext = new IntegrationScenarioContext();
scenarioContext.CaptureInvokedHandler(new HandlerInvocation()
{
Message = new InheritedMessage(),
EndpointName = "fake-endpoint",
HandlerType = typeof(MessageInterfaceHandler),
HandlingError = null,
MessageType = typeof(InheritedMessage)
});

Assert.IsTrue(scenarioContext.MessageWasProcessedByHandler<IMessageInterface, MessageInterfaceHandler>());
}

[Test]
public void MessageWasProcessed_condition_should_match_type()
{
var scenarioContext = new IntegrationScenarioContext();
scenarioContext.CaptureInvokedHandler(new HandlerInvocation()
{
Message = new TestMessage(),
EndpointName = "fake-endpoint",
HandlerType = typeof(TestMessageHandler),
HandlingError = null,
MessageType = typeof(TestMessage)
});

Assert.IsTrue(scenarioContext.MessageWasProcessed<TestMessage>());
}

[Test]
public void MessageWasProcessed_condition_should_match_base_type()
{
var scenarioContext = new IntegrationScenarioContext();
scenarioContext.CaptureInvokedHandler(new HandlerInvocation()
{
Message = new InheritedMessage(),
EndpointName = "fake-endpoint",
HandlerType = typeof(MessageInterfaceHandler),
HandlingError = null,
MessageType = typeof(InheritedMessage)
});

Assert.IsTrue(scenarioContext.MessageWasProcessed<IMessageInterface>());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Threading.Tasks;
using NUnit.Framework;

namespace NServiceBus.IntegrationTesting.Tests
{
public class When_capturing_sagas_invocations
{
class TestMessage : IMessage
{
}

interface IMessageInterface : IMessage
{
}

class InheritedMessage : IMessageInterface
{
}

class TestSaga : Saga<TestSaga.Data>,
IHandleMessages<TestMessage>,
IHandleMessages<IMessageInterface>
{
internal class Data : ContainSagaData
{
}

protected override void ConfigureHowToFindSaga(SagaPropertyMapper<Data> mapper)
{
/**/
}

public Task Handle(TestMessage message, IMessageHandlerContext context)
{
return Task.CompletedTask;
}

public Task Handle(IMessageInterface message, IMessageHandlerContext context)
{
return Task.CompletedTask;
}
}

[Test]
public void MessageWasProcessedBySaga_condition_should_match_type()
{
var scenarioContext = new IntegrationScenarioContext();
scenarioContext.CaptureInvokedSaga(new SagaInvocation()
{
Message = new TestMessage(), EndpointName = "fake-endpoint", SagaType = typeof(TestSaga), MessageType = typeof(TestMessage)
});

Assert.IsTrue(scenarioContext.MessageWasProcessedBySaga<TestMessage, TestSaga>());
}

[Test]
public void MessageWasProcessedBySaga_condition_should_match_base_type()
{
var scenarioContext = new IntegrationScenarioContext();
scenarioContext.CaptureInvokedSaga(new SagaInvocation()
{
Message = new InheritedMessage(), EndpointName = "fake-endpoint", SagaType = typeof(TestSaga), MessageType = typeof(InheritedMessage)
});

Assert.IsTrue(scenarioContext.MessageWasProcessedBySaga<IMessageInterface, TestSaga>());
}

[Test]
public void MessageWasProcessed_condition_should_match_type()
{
var scenarioContext = new IntegrationScenarioContext();
scenarioContext.CaptureInvokedSaga(new SagaInvocation()
{
Message = new TestMessage(),
EndpointName = "fake-endpoint",
SagaType = typeof(TestSaga),
HandlingError = null,
MessageType = typeof(TestMessage)
});

Assert.IsTrue(scenarioContext.MessageWasProcessed<TestMessage>());
}

[Test]
public void MessageWasProcessed_condition_should_match_base_type()
{
var scenarioContext = new IntegrationScenarioContext();
scenarioContext.CaptureInvokedSaga(new SagaInvocation()
{
Message = new InheritedMessage(),
EndpointName = "fake-endpoint",
SagaType = typeof(TestSaga),
HandlingError = null,
MessageType = typeof(InheritedMessage)
});

Assert.IsTrue(scenarioContext.MessageWasProcessed<IMessageInterface>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ public bool SagaWasCompleted<TSaga>() where TSaga : Saga

public bool MessageWasProcessed<TMessage>()
{
return invokedHandlers.Any(invocation => invocation.MessageType == typeof(TMessage))
|| invokedSagas.Any(invocation => invocation.MessageType == typeof(TMessage));
return invokedHandlers.Any(invocation => typeof(TMessage).IsAssignableFrom(invocation.MessageType))
|| invokedSagas.Any(invocation => typeof(TMessage).IsAssignableFrom(invocation.MessageType));
}

public bool MessageWasProcessedBySaga<TMessage, TSaga>()
{
return invokedSagas.Any(i => i.SagaType == typeof(TSaga) && i.MessageType == typeof(TMessage));
return invokedSagas.Any(i => i.SagaType == typeof(TSaga) && typeof(TMessage).IsAssignableFrom(i.MessageType));
}

public bool MessageWasProcessedByHandler<TMessage, THandler>()
{
return invokedHandlers.Any(i => i.HandlerType == typeof(THandler) && i.MessageType == typeof(TMessage));
return invokedHandlers.Any(i => i.HandlerType == typeof(THandler) && typeof(TMessage).IsAssignableFrom(i.MessageType));
}

public bool HasFailedMessages()
Expand Down

0 comments on commit ceed0b6

Please sign in to comment.