From 90db29bcba32b743cde8fdb6f08eeab32706fe5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Hjulstr=C3=B6m?= <51920152+martinhjulstrom@users.noreply.github.com> Date: Thu, 9 Dec 2021 23:02:58 +0100 Subject: [PATCH] Adds the ability to inject Elsa options into WorkflowsUnitTestBase. Also adds a test for a workflow with a Timer (#2574) --- .../Workflows/TimerWorkflow.cs | 18 +++++ .../Workflows/TimerWorkflowTests.cs | 75 +++++++++++++++++++ .../Unit/WorkflowsUnitTestBase.cs | 16 +++- 3 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 test/integration/Elsa.Core.IntegrationTests/Workflows/TimerWorkflow.cs create mode 100644 test/integration/Elsa.Core.IntegrationTests/Workflows/TimerWorkflowTests.cs diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/TimerWorkflow.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/TimerWorkflow.cs new file mode 100644 index 000000000..f1a91cdbe --- /dev/null +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/TimerWorkflow.cs @@ -0,0 +1,18 @@ +using Elsa.Activities.Console; +using Elsa.Activities.Temporal; +using Elsa.Builders; +using NodaTime; + +namespace Elsa.Core.IntegrationTests.Workflows +{ + public class TimerWorkflow : IWorkflow + { + public void Build(IWorkflowBuilder builder) + { + builder + .WriteLine("Starting timer workflow test") + .Timer(Duration.FromSeconds(1)) + .WriteLine("Finishing timer workflow test"); + } + } +} \ No newline at end of file diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/TimerWorkflowTests.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/TimerWorkflowTests.cs new file mode 100644 index 000000000..8954b7514 --- /dev/null +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/TimerWorkflowTests.cs @@ -0,0 +1,75 @@ +using System.Threading.Tasks; +using Elsa.Activities.Temporal; +using Elsa.Activities.Temporal.Common.Services; +using Elsa.Models; +using Elsa.Services; +using Elsa.Testing.Shared.Unit; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Moq; +using NodaTime; +using Xunit; +using Xunit.Abstractions; + +namespace Elsa.Core.IntegrationTests.Workflows +{ + public class TimerWorkflowTests : WorkflowsUnitTestBase + { + private static readonly Mock WorkflowDefinitionScheduler = new(); + private static readonly Mock WorkflowInstanceScheduler = new (); + private static readonly Mock Clock = new(); + + public TimerWorkflowTests(ITestOutputHelper testOutputHelper) + : base( + testOutputHelper, + services => + { + services.AddSingleton(WorkflowDefinitionScheduler.Object); + services.AddSingleton(WorkflowInstanceScheduler.Object); + services.Replace(new ServiceDescriptor(typeof(IClock), Clock.Object)); + }, + options => + { + options.AddCommonTemporalActivities(); + options.AddWorkflow(); + }) { } + + [Fact(DisplayName = "Runs timer workflow.")] + public async Task Test01() + { + var now = new Instant(); + + Clock + .Setup(x => x.GetCurrentInstant()) + .Returns(now); + + var runWorkflowResult = await WorkflowBuilderAndStarter.BuildAndStartWorkflowAsync(); + var workflowInstance = runWorkflowResult.WorkflowInstance!; + + Assert.Equal(WorkflowStatus.Suspended, workflowInstance.WorkflowStatus); + + var timerId = workflowInstance.LastExecutedActivityId!; + + WorkflowInstanceScheduler + .Verify(x => x + .ScheduleAsync( + workflowInstance.Id, + timerId, + now.Plus(Duration.FromSeconds(1)), + null, + default)); + + var workflowResumer = ServiceProvider.GetRequiredService(); + await workflowResumer.ResumeWorkflowAsync(workflowInstance, timerId); + + Assert.Equal(WorkflowStatus.Finished, workflowInstance.WorkflowStatus); + + WorkflowInstanceScheduler + .Verify(x => x + .UnscheduleAsync( + workflowInstance.Id, + timerId!, + default)); + } + } +} \ No newline at end of file diff --git a/test/shared/Elsa.Testing.Shared/Unit/WorkflowsUnitTestBase.cs b/test/shared/Elsa.Testing.Shared/Unit/WorkflowsUnitTestBase.cs index d13bf17b7..3dd2d2616 100644 --- a/test/shared/Elsa.Testing.Shared/Unit/WorkflowsUnitTestBase.cs +++ b/test/shared/Elsa.Testing.Shared/Unit/WorkflowsUnitTestBase.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using Elsa.Builders; +using Elsa.Options; using Elsa.Persistence; using Elsa.Services; using Elsa.Services.Bookmarks; @@ -16,15 +17,22 @@ namespace Elsa.Testing.Shared.Unit { private readonly TemporaryFolder _tempFolder; - protected WorkflowsUnitTestBase(ITestOutputHelper testOutputHelper, Action? configureServices = default) + protected WorkflowsUnitTestBase( + ITestOutputHelper testOutputHelper, + Action? configureServices = default, + Action? extraOptions = null) { _tempFolder = new TemporaryFolder(); TestOutputHelper = testOutputHelper; var services = new ServiceCollection() - .AddElsa(options => options - .AddConsoleActivities(Console.In, new XunitConsoleForwarder(testOutputHelper))); - + .AddElsa(options => + { + options.AddConsoleActivities(Console.In, new XunitConsoleForwarder(testOutputHelper)); + extraOptions?.Invoke(options); + } + ); + configureServices?.Invoke(services); ServiceProvider = services.BuildServiceProvider(); ServiceScope = ServiceProvider.CreateScope();