Skip to content
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

Merged
merged 2 commits into from
Oct 4, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<Compile Include="PerfMon\CriticalTime\When_deferring_a_message.cs" />
<Compile Include="PubSub\When_publishing_from_sendonly.cs" />
<Compile Include="PubSub\When_publishing_an_event_implementing_two_unrelated_interfaces.cs" />
<Compile Include="Sagas\When_saga_started_concurrently.cs" />
<Compile Include="Sagas\When_sagas_cant_be_found.cs" />
<Compile Include="Sagas\When_using_ReplyToOriginator.cs" />
<Compile Include="ScaleOut\When_individualization_is_enabled_for_msmq.cs" />
Expand Down
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]
Copy link
Member

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]

class StartMessageOne : ICommand
{
public string SomeId { get; set; }
}

[Serializable]
Copy link
Member

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]

class StartMessageTwo : ICommand
{
public string SomeId { get; set; }
}

[Serializable]
Copy link
Member

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]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember this is v5. It uses it consistently

Copy link
Member

Choose a reason for hiding this comment

The 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; }
}
}
}
1 change: 0 additions & 1 deletion src/NServiceBus.Core/NServiceBus.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@
<Compile Include="Unicast\Transport\Config\TransportExtensions.cs" />
<Compile Include="Unicast\Transport\Config\UseTransportExtensions_Obsolete.cs" />
<Compile Include="Utils\BaseDictionary.cs" />
<Compile Include="Utils\WeakKeyDictionary.cs" />
<Compile Include="Utils\Guard.cs" />
<Compile Include="Utils\ExceptionHeaderHelper.cs" />
<Compile Include="Utils\ElevateChecker.cs" />
Expand Down