Skip to content

Commit

Permalink
Merge branch 'hotfix-4.0.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
indualagarsamy committed Oct 16, 2014
2 parents cda7236 + 2b40450 commit d03759a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
<Compile Include="Retries\When_messages_fails_flr.cs" />
<Compile Include="Retries\When_message_fails_with_retries_set_to_0.cs" />
<Compile Include="Retries\When_doing_flr_with_default_settings.cs" />
<Compile Include="Sagas\When_message_has_a_saga_id.cs" />
<Compile Include="Sagas\When_doing_request_response_between_sagas.cs" />
<Compile Include="Sagas\When_sending_from_a_saga_timeout.cs" />
<Compile Include="Sagas\When_sending_from_a_saga_handle.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
namespace NServiceBus.AcceptanceTests.Sagas
{
using System;
using AcceptanceTesting;
using EndpointTemplates;
using Saga;
using NUnit.Framework;

public class When_message_has_a_saga_id : NServiceBusAcceptanceTest
{
[Test]
public void Should_not_start_a_new_saga_if_not_found()
{
var context = Scenario.Define<Context>()
.WithEndpoint<SagaEndpoint>(b => b.Given(bus =>
{
var message = new MessageWithSagaId();
bus.SetMessageHeader(message, Headers.SagaId, Guid.NewGuid().ToString());
bus.SetMessageHeader(message, Headers.SagaType, typeof(MySaga).AssemblyQualifiedName);
bus.SendLocal(message);
}))
.Done(c => c.NotFoundHandlerCalled)
.Run(TimeSpan.FromSeconds(15));

Assert.True(context.NotFoundHandlerCalled);
Assert.False(context.MessageHandlerCalled);
Assert.False(context.TimeoutHandlerCalled);
}

class MySaga : Saga<MySaga.SagaData>, IAmStartedByMessages<MessageWithSagaId>,
IHandleTimeouts<MessageWithSagaId>,
IHandleSagaNotFound
{
public Context Context { get; set; }

public class SagaData : ContainSagaData
{
}

public void Handle(MessageWithSagaId message)
{
Context.MessageHandlerCalled = true;
}

public void Handle(object message)
{
Context.NotFoundHandlerCalled = true;
}

public void Timeout(MessageWithSagaId state)
{
Context.TimeoutHandlerCalled = true;
}
}

class Context : ScenarioContext
{
public bool NotFoundHandlerCalled { get; set; }
public bool MessageHandlerCalled { get; set; }
public bool TimeoutHandlerCalled { get; set; }
}

public class SagaEndpoint : EndpointConfigurationBuilder
{
public SagaEndpoint()
{
EndpointSetup<DefaultServer>();
}
}

public class MessageWithSagaId : IMessage
{
}
}
}
22 changes: 22 additions & 0 deletions src/NServiceBus.Core/Sagas/SagaDispatcherFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public IEnumerable<Action> GetDispatcher(Type messageHandlerType, IBuilder build
if (sagaTypesHandled.Contains(sagaType))
continue; // don't create the same saga type twice for the same message

if (!IsAllowedToStartANewSaga(sagaType))
continue;

sagaEntity = CreateNewSagaEntity(sagaType);

sagaEntityIsPersistent = false;
Expand Down Expand Up @@ -128,6 +131,25 @@ public IEnumerable<Action> GetDispatcher(Type messageHandlerType, IBuilder build
};
}

bool IsAllowedToStartANewSaga(Type sagaInstanceType)
{
string sagaType;

if (Bus.CurrentMessageContext.Headers.ContainsKey(Headers.SagaId) &&
Bus.CurrentMessageContext.Headers.TryGetValue(Headers.SagaType, out sagaType))
{
//we want to move away from the assembly fully qualified name since that will break if you move sagas
// between assemblies. We use the fullname instead which is enough to identify the saga
if (sagaType.StartsWith(sagaInstanceType.FullName))
{
//so now we have a saga id for this saga and if we can't find it we shouldn't start a new one
return false;
}
}

return true;
}

IContainSagaData CreateNewSagaEntity(Type sagaType)
{
var sagaEntityType = Features.Sagas.GetSagaEntityTypeForSagaType(sagaType);
Expand Down

0 comments on commit d03759a

Please sign in to comment.