-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MassTransitBusEngine now leverages MassTransit headers to store Messa…
…ge properties
- Loading branch information
Showing
7 changed files
with
276 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using MassTransit; | ||
|
||
namespace Nybus.Configuration | ||
{ | ||
public interface IContextManager | ||
{ | ||
EventMessage<TEvent> CreateEventMessage<TEvent>(IConsumeContext<TEvent> context) | ||
where TEvent : class, IEvent; | ||
|
||
void SetEventMessageHeaders<TEvent>(EventMessage<TEvent> message, IPublishContext<TEvent> context) | ||
where TEvent : class, IEvent; | ||
|
||
CommandMessage<TCommand> CreateCommandMessage<TCommand>(IConsumeContext<TCommand> context) | ||
where TCommand : class, ICommand; | ||
|
||
void SetCommandMessageHeaders<TCommand>(CommandMessage<TCommand> message, IPublishContext<TCommand> context) | ||
where TCommand : class, ICommand; | ||
} | ||
|
||
public class RabbitMqContextManager : IContextManager | ||
{ | ||
public const string CorrelationIdKey = "nybus:CorrelationId"; | ||
|
||
public EventMessage<TEvent> CreateEventMessage<TEvent>(IConsumeContext<TEvent> context) where TEvent : class, IEvent | ||
{ | ||
return new EventMessage<TEvent> | ||
{ | ||
CorrelationId = ExtractCorrelationId(context.Headers), | ||
Event = context.Message | ||
}; | ||
} | ||
|
||
public void SetEventMessageHeaders<TEvent>(EventMessage<TEvent> message, IPublishContext<TEvent> context) where TEvent : class, IEvent | ||
{ | ||
PersistCorrelationId(context, message.CorrelationId); | ||
} | ||
|
||
public CommandMessage<TCommand> CreateCommandMessage<TCommand>(IConsumeContext<TCommand> context) where TCommand : class, ICommand | ||
{ | ||
return new CommandMessage<TCommand> | ||
{ | ||
Command = context.Message, | ||
CorrelationId = ExtractCorrelationId(context.Headers) | ||
}; | ||
} | ||
|
||
public void SetCommandMessageHeaders<TCommand>(CommandMessage<TCommand> message, IPublishContext<TCommand> context) where TCommand : class, ICommand | ||
{ | ||
PersistCorrelationId(context, message.CorrelationId); | ||
} | ||
|
||
private Guid ExtractCorrelationId(IMessageHeaders messageHeaders) | ||
{ | ||
return Guid.Parse(messageHeaders[CorrelationIdKey]); | ||
} | ||
|
||
private void PersistCorrelationId(ISendContext sendContext, Guid correlationId) | ||
{ | ||
sendContext.SetHeader(CorrelationIdKey, correlationId.ToString("D")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
tests/Tests.MassTransit/Configuration/RabbitMqContextManagerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using System; | ||
using MassTransit; | ||
using Moq; | ||
using NUnit.Framework; | ||
using Nybus; | ||
using Nybus.Configuration; | ||
using Ploeh.AutoFixture; | ||
|
||
namespace Tests.Configuration | ||
{ | ||
public class RabbitMqContextManagerTests | ||
{ | ||
private IFixture fixture; | ||
|
||
private Mock<IConsumeContext<TestEvent>> mockEventConsumeContext; | ||
|
||
private Mock<IPublishContext<TestEvent>> mockEventPublishContext; | ||
|
||
private Mock<IConsumeContext<TestCommand>> mockCommandConsumeContext; | ||
|
||
private Mock<IPublishContext<TestCommand>> mockCommandPublishContext; | ||
|
||
[SetUp] | ||
public void Initialize() | ||
{ | ||
fixture = new Fixture(); | ||
|
||
mockEventConsumeContext = new Mock<IConsumeContext<TestEvent>>(); | ||
|
||
mockEventPublishContext = new Mock<IPublishContext<TestEvent>>(); | ||
|
||
mockCommandConsumeContext = new Mock<IConsumeContext<TestCommand>>(); | ||
|
||
mockCommandPublishContext = new Mock<IPublishContext<TestCommand>>(); | ||
} | ||
|
||
private RabbitMqContextManager CreateSystemUnderTest() | ||
{ | ||
return new RabbitMqContextManager(); | ||
} | ||
|
||
[Test] | ||
public void CreateEventMessage_retrieves_CorrelationId_from_context() | ||
{ | ||
var sut = CreateSystemUnderTest(); | ||
|
||
var body = fixture.Create<TestEvent>(); | ||
|
||
var correlationId = fixture.Create<Guid>(); | ||
|
||
mockEventConsumeContext.SetupGet(p => p.Message).Returns(body); | ||
mockEventConsumeContext.Setup(p => p.Headers[RabbitMqContextManager.CorrelationIdKey]).Returns(correlationId.ToString()); | ||
|
||
var message = sut.CreateEventMessage(mockEventConsumeContext.Object); | ||
|
||
Assert.That(message.CorrelationId, Is.EqualTo(correlationId)); | ||
} | ||
|
||
[Test] | ||
public void CreateEventMessage_retrieves_Event_from_context() | ||
{ | ||
var sut = CreateSystemUnderTest(); | ||
|
||
TestEvent body = fixture.Create<TestEvent>(); | ||
|
||
Guid correlationId = fixture.Create<Guid>(); | ||
|
||
mockEventConsumeContext.SetupGet(p => p.Message).Returns(body); | ||
mockEventConsumeContext.Setup(p => p.Headers[RabbitMqContextManager.CorrelationIdKey]).Returns(correlationId.ToString()); | ||
|
||
var message = sut.CreateEventMessage(mockEventConsumeContext.Object); | ||
|
||
Assert.That(message.Event, Is.SameAs(body)); | ||
} | ||
|
||
[Test] | ||
public void SetEventMessageHeaders_sets_correlationId_to_context() | ||
{ | ||
var sut = CreateSystemUnderTest(); | ||
|
||
var message = fixture.Create<EventMessage<TestEvent>>(); | ||
|
||
sut.SetEventMessageHeaders(message, mockEventPublishContext.Object); | ||
|
||
mockEventPublishContext.Verify(p => p.SetHeader(RabbitMqContextManager.CorrelationIdKey, message.CorrelationId.ToString("D"))); | ||
} | ||
|
||
[Test] | ||
public void CreateCommandMessage_retrieves_CorrelationId_from_context() | ||
{ | ||
var sut = CreateSystemUnderTest(); | ||
|
||
var body = fixture.Create<TestCommand>(); | ||
|
||
var correlationId = fixture.Create<Guid>(); | ||
|
||
mockCommandConsumeContext.SetupGet(p => p.Message).Returns(body); | ||
mockCommandConsumeContext.Setup(p => p.Headers[RabbitMqContextManager.CorrelationIdKey]).Returns(correlationId.ToString()); | ||
|
||
var message = sut.CreateCommandMessage(mockCommandConsumeContext.Object); | ||
|
||
Assert.That(message.CorrelationId, Is.EqualTo(correlationId)); | ||
} | ||
|
||
[Test] | ||
public void CreateCommandMessage_retrieves_Command_from_context() | ||
{ | ||
var sut = CreateSystemUnderTest(); | ||
|
||
var body = fixture.Create<TestCommand>(); | ||
|
||
var correlationId = fixture.Create<Guid>(); | ||
|
||
mockCommandConsumeContext.SetupGet(p => p.Message).Returns(body); | ||
mockCommandConsumeContext.Setup(p => p.Headers[RabbitMqContextManager.CorrelationIdKey]).Returns(correlationId.ToString()); | ||
|
||
var message = sut.CreateCommandMessage(mockCommandConsumeContext.Object); | ||
|
||
Assert.That(message.Command, Is.SameAs(body)); | ||
} | ||
|
||
[Test] | ||
public void SetCommandMessageHeaders_sets_correlationId_to_context() | ||
{ | ||
var sut = CreateSystemUnderTest(); | ||
|
||
var message = fixture.Create<CommandMessage<TestCommand>>(); | ||
|
||
sut.SetCommandMessageHeaders(message, mockCommandPublishContext.Object); | ||
|
||
mockCommandPublishContext.Verify(p => p.SetHeader(RabbitMqContextManager.CorrelationIdKey, message.CorrelationId.ToString("D"))); | ||
} | ||
|
||
|
||
} | ||
} |
Oops, something went wrong.