-
Notifications
You must be signed in to change notification settings - Fork 653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
InMemory saga persister creates duplicate saga instances #4182
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
namespace NServiceBus.AcceptanceTests.Sagas | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using NServiceBus.AcceptanceTesting; | ||
using NServiceBus.AcceptanceTests.EndpointTemplates; | ||
using NServiceBus.Config; | ||
using NServiceBus.Saga; | ||
using NUnit.Framework; | ||
|
||
public class When_saga_started_concurrently : NServiceBusAcceptanceTest | ||
{ | ||
[Test] | ||
public void Should_start_single_saga() | ||
{ | ||
var context = new Context | ||
{ | ||
SomeId = Guid.NewGuid().ToString() | ||
}; | ||
|
||
Scenario.Define(context) | ||
.WithEndpoint<ConcurrentHandlerEndpoint>(b => | ||
{ | ||
b.When(bus => | ||
{ | ||
Parallel.Invoke(() => | ||
{ | ||
bus.SendLocal(new StartMessageOne | ||
{ | ||
SomeId = context.SomeId | ||
}); | ||
}, () => | ||
{ | ||
bus.SendLocal(new StartMessageTwo | ||
{ | ||
SomeId = context.SomeId | ||
} | ||
); | ||
}); | ||
}); | ||
}) | ||
.AllowExceptions() | ||
.Done(c => c.PlacedSagaId != Guid.Empty && c.BilledSagaId != Guid.Empty) | ||
.Run(); | ||
|
||
Assert.AreNotEqual(Guid.Empty, context.PlacedSagaId); | ||
Assert.AreNotEqual(Guid.Empty, context.BilledSagaId); | ||
Assert.AreEqual(context.PlacedSagaId, context.BilledSagaId, "Both messages should have been handled by the same saga, but SagaIds don't match."); | ||
} | ||
|
||
class Context : ScenarioContext | ||
{ | ||
public string SomeId { get; set; } | ||
public Guid PlacedSagaId { get; set; } | ||
public Guid BilledSagaId { get; set; } | ||
public bool SagaCompleted { get; set; } | ||
} | ||
|
||
class ConcurrentHandlerEndpoint : EndpointConfigurationBuilder | ||
{ | ||
public ConcurrentHandlerEndpoint() | ||
{ | ||
EndpointSetup<DefaultServer>(b => { }) | ||
.WithConfig<TransportConfig>(c => | ||
{ | ||
c.MaxRetries = 3; | ||
c.MaximumConcurrencyLevel = 2; | ||
}) | ||
.WithConfig<SecondLevelRetriesConfig>(c => | ||
{ | ||
c.Enabled = false; | ||
}); | ||
} | ||
|
||
class ConcurrentlyStartedSaga : Saga<ConcurrentlyStartedSagaData>, | ||
IAmStartedByMessages<StartMessageTwo>, | ||
IAmStartedByMessages<StartMessageOne> | ||
{ | ||
public Context Context { get; set; } | ||
|
||
public void Handle(StartMessageOne message) | ||
{ | ||
Data.OrderId = message.SomeId; | ||
Data.Placed = true; | ||
Bus.SendLocal(new SuccessfulProcessing | ||
{ | ||
SagaId = Data.Id, | ||
Type = nameof(StartMessageOne) | ||
}); | ||
CheckForCompletion(); | ||
} | ||
|
||
public void Handle(StartMessageTwo message) | ||
{ | ||
Data.OrderId = message.SomeId; | ||
Data.Billed = true; | ||
Bus.SendLocal(new SuccessfulProcessing | ||
{ | ||
SagaId = Data.Id, | ||
Type = nameof(StartMessageTwo) | ||
}); | ||
CheckForCompletion(); | ||
} | ||
|
||
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<ConcurrentlyStartedSagaData> mapper) | ||
{ | ||
mapper.ConfigureMapping<StartMessageOne>(msg => msg.SomeId).ToSaga(saga => saga.OrderId); | ||
mapper.ConfigureMapping<StartMessageTwo>(msg => msg.SomeId).ToSaga(saga => saga.OrderId); | ||
} | ||
|
||
void CheckForCompletion() | ||
{ | ||
if (!Data.Billed || !Data.Placed) | ||
{ | ||
return; | ||
} | ||
MarkAsComplete(); | ||
Context.SagaCompleted = true; | ||
} | ||
} | ||
|
||
class ConcurrentlyStartedSagaData : ContainSagaData | ||
{ | ||
[Unique] | ||
public virtual string OrderId { get; set; } | ||
public virtual bool Placed { get; set; } | ||
public virtual bool Billed { get; set; } | ||
} | ||
|
||
// Intercepts the messages sent out by the saga | ||
class LogSuccessfulHandler : IHandleMessages<SuccessfulProcessing> | ||
{ | ||
public Context Context { get; set; } | ||
|
||
public void Handle(SuccessfulProcessing message) | ||
{ | ||
if (message.Type == nameof(StartMessageOne)) | ||
{ | ||
Context.PlacedSagaId = message.SagaId; | ||
} | ||
else if (message.Type == nameof(StartMessageTwo)) | ||
{ | ||
Context.BilledSagaId = message.SagaId; | ||
} | ||
else | ||
{ | ||
throw new Exception("Unknown type"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
[Serializable] | ||
class StartMessageOne : ICommand | ||
{ | ||
public string SomeId { get; set; } | ||
} | ||
|
||
[Serializable] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: We don't need the [Serializable] |
||
class StartMessageTwo : ICommand | ||
{ | ||
public string SomeId { get; set; } | ||
} | ||
|
||
[Serializable] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: We don't need the [Serializable] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember this is v5. It uses it consistently There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a 100% https://github.com/Particular/NServiceBus/blob/master/src/NServiceBus.AcceptanceTests/Audit/When_a_replymessage_is_audited.cs#L112 and there's probably more. But as I said nitpick, I'll make sure we remove them for v6.1 |
||
class SuccessfulProcessing : ICommand | ||
{ | ||
public string Type { get; set; } | ||
public Guid SagaId { get; set; } | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: We don't need the
[Serializable]