-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow for correlation between header and saga data
- Loading branch information
1 parent
b03cc6e
commit 77ff0ee
Showing
8 changed files
with
149 additions
and
20 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using Rebus.Activation; | ||
using Rebus.Bus; | ||
using Rebus.Config; | ||
using Rebus.Messages; | ||
using Rebus.Sagas; | ||
using Rebus.Tests.Contracts; | ||
using Rebus.Tests.Contracts.Utilities; | ||
using Rebus.Transport.InMem; | ||
|
||
namespace Rebus.Tests.Sagas | ||
{ | ||
[TestFixture] | ||
public class TestHeaderCorrelation : FixtureBase | ||
{ | ||
BuiltinHandlerActivator _activator; | ||
IBus _bus; | ||
|
||
protected override void SetUp() | ||
{ | ||
_activator = new BuiltinHandlerActivator(); | ||
|
||
Using(_activator); | ||
|
||
Configure.With(_activator) | ||
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "header-correlation")) | ||
.Start(); | ||
|
||
_bus = _activator.Bus; | ||
} | ||
|
||
[Test] | ||
public async Task CanCorrelateWithHeadersOfIncomingMessages() | ||
{ | ||
var sagaDataCounters = new ConcurrentDictionary<Guid, int>(); | ||
var sharedCounter = new SharedCounter(5); | ||
|
||
_activator.Register(() => new MySaga(sharedCounter, sagaDataCounters)); | ||
|
||
var sameMessage = new MyMessage(); | ||
var headers1 = new Dictionary<string, string> { { "custom-correlation-id", "saga1" } }; | ||
var headers2 = new Dictionary<string, string> { { "custom-correlation-id", "saga2" } }; | ||
|
||
await _bus.SendLocal(sameMessage, headers1); | ||
await _bus.SendLocal(sameMessage, headers2); | ||
await _bus.SendLocal(sameMessage, headers2); | ||
await _bus.SendLocal(sameMessage, headers1); | ||
await _bus.SendLocal(sameMessage, headers1); | ||
|
||
sharedCounter.WaitForResetEvent(timeoutSeconds:2); | ||
} | ||
|
||
class MyMessage { } | ||
|
||
class MySaga : Saga<MySagaData>, IAmInitiatedBy<MyMessage> | ||
{ | ||
readonly SharedCounter _sharedCounter; | ||
readonly ConcurrentDictionary<Guid, int> _sagaDataCounters; | ||
|
||
public MySaga(SharedCounter sharedCounter, ConcurrentDictionary<Guid, int> sagaDataCounters) | ||
{ | ||
_sharedCounter = sharedCounter; | ||
_sagaDataCounters = sagaDataCounters; | ||
} | ||
|
||
protected override void CorrelateMessages(ICorrelationConfig<MySagaData> config) | ||
{ | ||
config.CorrelateHeader<MyMessage>("custom-correlation-id", d => d.CorrelationId); | ||
} | ||
|
||
public async Task Handle(MyMessage message) | ||
{ | ||
_sagaDataCounters.AddOrUpdate(Data.Id, 1, (_, count) => count + 1); | ||
_sharedCounter.Decrement(); | ||
} | ||
} | ||
|
||
class MySagaData : SagaData | ||
{ | ||
public string CorrelationId { get; set; } | ||
} | ||
} | ||
} |
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
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