Skip to content

executor should publish if EndWorkflow() is set to determine end of workf… #1098

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/WorkflowCore/Services/WorkflowExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ private async Task DetermineNextExecutionTime(WorkflowInstance workflow, Workflo

if (workflow.Status == WorkflowStatus.Complete)
{
PublishWorkflowCompleted(workflow);
return;
}

Expand Down Expand Up @@ -265,6 +266,11 @@ private async Task DetermineNextExecutionTime(WorkflowInstance workflow, Workflo
await middlewareRunner.RunPostMiddleware(workflow, def);
}

PublishWorkflowCompleted(workflow);
}

private void PublishWorkflowCompleted(WorkflowInstance workflow)
{
_publisher.PublishNotification(new WorkflowCompleted
{
EventTimeUtc = _datetimeProvider.UtcNow,
Expand Down
65 changes: 62 additions & 3 deletions test/WorkflowCore.UnitTests/Services/WorkflowExecutorFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using WorkflowCore.Models;
using WorkflowCore.Services;
using Xunit;
using WorkflowCore.Models.LifeCycleEvents;
using Moq;
using Times = Moq.Times;

namespace WorkflowCore.UnitTests.Services
{
Expand All @@ -19,7 +22,7 @@ public class WorkflowExecutorFixture
protected IPersistenceProvider PersistenceProvider;
protected IWorkflowRegistry Registry;
protected IExecutionResultProcessor ResultProcesser;
protected ILifeCycleEventPublisher EventHub;
protected Mock<ILifeCycleEventPublisher> EventHub;
protected ICancellationProcessor CancellationProcessor;
protected IServiceProvider ServiceProvider;
protected IScopeProvider ScopeProvider;
Expand All @@ -36,7 +39,7 @@ public WorkflowExecutorFixture()
ScopeProvider = A.Fake<IScopeProvider>();
Registry = A.Fake<IWorkflowRegistry>();
ResultProcesser = A.Fake<IExecutionResultProcessor>();
EventHub = A.Fake<ILifeCycleEventPublisher>();
EventHub = new Mock<ILifeCycleEventPublisher>();
CancellationProcessor = A.Fake<ICancellationProcessor>();
DateTimeProvider = A.Fake<IDateTimeProvider>();
MiddlewareRunner = A.Fake<IWorkflowMiddlewareRunner>();
Expand Down Expand Up @@ -82,7 +85,7 @@ public WorkflowExecutorFixture()
var loggerFactory = new LoggerFactory();
//loggerFactory.AddConsole(LogLevel.Debug);

Subject = new WorkflowExecutor(Registry, ServiceProvider, ScopeProvider, DateTimeProvider, ResultProcesser, EventHub, CancellationProcessor, Options, loggerFactory);
Subject = new WorkflowExecutor(Registry, ServiceProvider, ScopeProvider, DateTimeProvider, ResultProcesser, EventHub.Object, CancellationProcessor, Options, loggerFactory);
}

[Fact(DisplayName = "Should execute active step")]
Expand Down Expand Up @@ -403,6 +406,62 @@ public void should_process_cancellations()
A.CallTo(() => CancellationProcessor.ProcessCancellations(instance, A<WorkflowDefinition>.Ignored, A<WorkflowExecutorResult>.Ignored)).MustHaveHappened();
}

[Fact(DisplayName = "Should execute active step")]
public void should_execute_publisher_sends_Event_when_completed()
{
//arrange
var param = A.Fake<IStepParameter>();

var step1Body = A.Fake<IStepBody>();
A.CallTo(() => step1Body.RunAsync(A<IStepExecutionContext>.Ignored)).Returns(ExecutionResult.Next());
WorkflowStep step1 = BuildFakeEndStep(step1Body, new List<IStepParameter>
{
param
},
new List<IStepParameter>());

Given1StepWorkflow(step1, "Workflow", 1);

var instance = new WorkflowInstance
{
WorkflowDefinitionId = "Workflow",
Version = 1,
Status = WorkflowStatus.Runnable,
NextExecution = 0,
Id = "001",
ExecutionPointers = new ExecutionPointerCollection(new List<ExecutionPointer>
{
new ExecutionPointer { Id = "1", Active = true, StepId = 0 }
})
};

EventHub.Setup(m => m.PublishNotification(new WorkflowCompleted()));

//act
Subject.Execute(instance);

//assert
EventHub.Verify(hub => hub.PublishNotification(It.IsAny<WorkflowCompleted>()), Times.Once());
A.CallTo(() => step1Body.RunAsync(A<IStepExecutionContext>.Ignored)).MustNotHaveHappened();
A.CallTo(() => ResultProcesser.ProcessExecutionResult(instance, A<WorkflowDefinition>.Ignored, A<ExecutionPointer>.Ignored, step1, A<ExecutionResult>.Ignored, A<WorkflowExecutorResult>.Ignored)).MustNotHaveHappened();
}

private WorkflowStep BuildFakeEndStep(IStepBody stepBody, List<IStepParameter> inputs, List<IStepParameter> outputs)
{
var result = A.Fake<WorkflowStep>();
A.CallTo(() => result.Id).Returns(0);
A.CallTo(() => result.BodyType).Returns(stepBody.GetType());
A.CallTo(() => result.ResumeChildrenAfterCompensation).Returns(true);
A.CallTo(() => result.RevertChildrenAfterCompensation).Returns(false);
A.CallTo(() => result.ConstructBody(ServiceProvider)).Returns(stepBody);
A.CallTo(() => result.Inputs).Returns(inputs);
A.CallTo(() => result.Outputs).Returns(outputs);
A.CallTo(() => result.Outcomes).Returns(new List<IStepOutcome>());
A.CallTo(() => result.InitForExecution(A<WorkflowExecutorResult>.Ignored, A<WorkflowDefinition>.Ignored, A<WorkflowInstance>.Ignored, A<ExecutionPointer>.Ignored)).Returns(ExecutionPipelineDirective.EndWorkflow);
A.CallTo(() => result.BeforeExecute(A<WorkflowExecutorResult>.Ignored, A<IStepExecutionContext>.Ignored, A<ExecutionPointer>.Ignored, A<IStepBody>.Ignored)).Returns(ExecutionPipelineDirective.Next);
return result;
}


private void Given1StepWorkflow(WorkflowStep step1, string id, int version)
{
Expand Down