Skip to content

Commit

Permalink
Fixed duplicate event handling on start in ProcessManager (#261)
Browse files Browse the repository at this point in the history
* Fixed duplicate event handling on start in ProcessManager

* Fixed test
  • Loading branch information
fraliv13 authored Aug 7, 2023
1 parent cdf85ed commit 8336da4
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Func<TEvent, object> IDefinition<TData>.GetCorrelationFilter<TEvent>()
return null;
}

IEnumerable<EventType> IDefinition.GetEventTypes() => _eventActivities.Select(x => new EventType(x.EventType, x.StartsProcess)).Distinct();
IEnumerable<EventType> IDefinition.GetEventTypes() => _eventActivities.Select(x => new EventType(x.EventType, _eventActivities.Count(e => e.GetType() == x.GetType() && e.StartsProcess) == 1)).Distinct();

EffectFunc<TEvent, TData> IDefinition<TData>.GetEffectFunc<TEvent>()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ public sealed class ObsoleteProcessAttribute : Attribute
{
}

public record struct EventType(Type Type, bool StartsProcess);
public record struct EventType(Type Type, bool OnlyStartsProcess);
}
3 changes: 2 additions & 1 deletion src/Orchestration/NBB.ProcessManager.Runtime/Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public void ProcessEvent<TEvent>(TEvent @event)
{
if (_definition.IsObsolete())
{
throw new Exception($"Definition {_definition.GetType().GetLongPrettyName()} is obsolete and new process instances cannot be started.");
_logger.LogWarning($"Definition {_definition.GetType().GetLongPrettyName()} is obsolete and new process instances cannot be started.");
return;
}

StartProcess(@event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public record OrderPaymentExpired (Guid OrderId, int DocumentId, int SiteId) : I

public record OrderPaymentReceived(Guid OrderId, int DocumentId, int SiteId) : INotification;

public record OrderShipped(Guid OrderId, DateTime ShippingDate);
}
public record OrderShipped(Guid OrderId, DateTime ShippingDate) : INotification;
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,20 @@ public OrderProcessManager7()
}

[Fact]
public void Should_throw_when_starting_obsolete_processes()
public void Should_not_start_when_obsolete_processes()
{
var @event = new OrderCreated(Guid.NewGuid(), 100, 0, 0);
var definition = new ObsoleteProcessManager();
var logger = Mock.Of<ILogger<Instance<OrderProcessManagerData>>>();

var instance = new Instance<OrderProcessManagerData>(definition, logger);
Action act = () => instance.ProcessEvent(@event);
act.Should().Throw<Exception>();
instance.ProcessEvent(@event);

instance.State.Should().Be(InstanceStates.NotStarted);

Mock.Get(logger).Verify(x => x.Log(LogLevel.Warning, It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, _) => v.ToString().Contains("obsolete")),
It.IsAny<Exception>(), It.IsAny<Func<It.IsAnyType, Exception, string>>()));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (c) TotalSoft.
// This source code is licensed under the MIT license.

using FluentAssertions;
using NBB.ProcessManager.Definition.Builder;
using NBB.ProcessManager.Runtime;
using NBB.ProcessManager.Tests.Commands;
using NBB.ProcessManager.Tests.Events;
using System;
using System.Threading;
using System.Threading.Tasks;
using NBB.Messaging.Effects;
using NBB.ProcessManager.Definition;
using Xunit;
using Moq;
using Microsoft.Extensions.Logging;
using NBB.ProcessManager.Runtime.Events;
using Microsoft.Extensions.DependencyInjection;
using NBB.Core.Effects;
using NBB.EventStore.InMemory;
using NBB.EventStore.Internal;
using NBB.ProcessManager.Runtime.Persistence;
using static NBB.ProcessManager.Tests.RegistrationTests.RegistrationProcessManager;
using System.Collections.Generic;
using System.Linq;
using MediatR;
using FluentAssertions.Common;


namespace NBB.ProcessManager.Tests
{
public class RegistrationTests
{

[Fact]
public void HandlersShouldBeRegisteredOnce()
{
var sp = BuildServiceProvider();
var orderCreatedHandlers = sp.GetServices<INotificationHandler<OrderCreated>>().OfType<ProcessManagerNotificationHandler<RegistrationProcessManager, RegistrationProcessManagerData, OrderCreated>>();
var orderPayemntCreatedHandlers = sp.GetServices<INotificationHandler<OrderPaymentCreated>>().OfType<ProcessManagerNotificationHandler<RegistrationProcessManager, RegistrationProcessManagerData, OrderPaymentCreated>>();

orderCreatedHandlers.Count().Should().Be(1);
orderPayemntCreatedHandlers.Count().Should().Be(1);
}

public IServiceProvider BuildServiceProvider()
{
var services = new ServiceCollection();
services.AddProcessManager(typeof(RegistrationProcessManager).Assembly);
services.AddEventStore(es =>
{
es.UseNewtownsoftJson();
es.UseInMemoryEventRepository();
});
services.AddLogging(builder => builder.AddConsole());

return services.BuildServiceProvider();
}

public class RegistrationProcessManager : AbstractDefinition<RegistrationProcessManagerData>
{
public record struct RegistrationProcessManagerData
{
public Guid OrderId { get; init; }
public int CreateCount { get; init; }
public int PaidCount { get; set;}

}
public RegistrationProcessManager()
{
Event<OrderCreated>(configurator => configurator.CorrelateById(orderCreated => orderCreated.OrderId));
Event<OrderPaymentCreated>(configurator =>
configurator.CorrelateById(paymentReceived => paymentReceived.OrderId));

StartWith<OrderCreated>();

When<OrderCreated>()
.SetState((orderCreated, state) =>
state.Data with
{
OrderId = orderCreated.OrderId,
CreateCount = state.Data.CreateCount + 1
})
.PublishEvent((orderCreated, state) => new OrderCompleted(orderCreated.OrderId, 100, 0, 0));

When<OrderPaymentCreated>()
.SetState((@event, state) => state.Data with { PaidCount = state.Data.PaidCount + 1 })
.Then((ev, state) =>
{
var effect = MessageBus.Publish(new DoPayment());
return effect;
});
}
}
}
}

0 comments on commit 8336da4

Please sign in to comment.