From 672e32a7caae61355e928bc4c299e3687f468d17 Mon Sep 17 00:00:00 2001 From: Craig Fowler Date: Sat, 6 Mar 2021 17:22:37 +0000 Subject: [PATCH] WIP #552 - Add unit test to repro Actually, this unit test does not repro the issue. Frustratingly I can't get it to save the executed workflow into MongoDB at all. I see workflow execution logs being saved, but the instance doesn't go in. I'm not sure if this is a mistake I'm making or whether I have found a different bug of some sort. --- appveyor.yml | 3 ++ ...thElsaSampleWorkflowAndMongoDbAttribute.cs | 31 +++++++++++++ .../Elsa.Core.IntegrationTests.csproj | 1 + .../MongoDb/MongoDbIntegrationTests.cs | 46 +++++++++++++++++++ .../Workflows/SampleWorkflow.cs | 20 ++++++++ 5 files changed, 101 insertions(+) create mode 100644 test/integration/Elsa.Core.IntegrationTests/Autofixture/HostBuilderWithElsaSampleWorkflowAndMongoDbAttribute.cs create mode 100644 test/integration/Elsa.Core.IntegrationTests/Persistence/MongoDb/MongoDbIntegrationTests.cs create mode 100644 test/integration/Elsa.Core.IntegrationTests/Workflows/SampleWorkflow.cs diff --git a/appveyor.yml b/appveyor.yml index ec959fab0..d6ae6f5af 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -13,6 +13,9 @@ environment: version: 2.0.0-preview7.{build} +services: + - mongodb + init: - cmd: git config --global core.autocrlf true diff --git a/test/integration/Elsa.Core.IntegrationTests/Autofixture/HostBuilderWithElsaSampleWorkflowAndMongoDbAttribute.cs b/test/integration/Elsa.Core.IntegrationTests/Autofixture/HostBuilderWithElsaSampleWorkflowAndMongoDbAttribute.cs new file mode 100644 index 000000000..f395dfc3e --- /dev/null +++ b/test/integration/Elsa.Core.IntegrationTests/Autofixture/HostBuilderWithElsaSampleWorkflowAndMongoDbAttribute.cs @@ -0,0 +1,31 @@ +using System.Reflection; +using AutoFixture; +using AutoFixture.Xunit2; +using Elsa.Activities.UserTask.Activities; +using Elsa.Core.IntegrationTests.Workflows; +using Elsa.Persistence.MongoDb.Extensions; +using Elsa.Testing.Shared.AutoFixture.Customizations; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace Elsa.Core.IntegrationTests.Autofixture +{ + public class HostBuilderWithElsaSampleWorkflowAndMongoDbAttribute : CustomizeAttribute + { + public override ICustomization GetCustomization(ParameterInfo parameter) + { + return new HostBubilderUsingServicesCustomization(services => { + services + .AddElsa(elsa => { + elsa.UseMongoDbPersistence(opts => { + opts.ConnectionString = "mongodb://localhost:27017"; + opts.Db = "IntegrationTests"; + }); + elsa.AddActivity(); + elsa.AddWorkflow(); + }) + ; + }, parameter); + } + } +} \ No newline at end of file diff --git a/test/integration/Elsa.Core.IntegrationTests/Elsa.Core.IntegrationTests.csproj b/test/integration/Elsa.Core.IntegrationTests/Elsa.Core.IntegrationTests.csproj index 3b1f11ec6..58e23c58f 100644 --- a/test/integration/Elsa.Core.IntegrationTests/Elsa.Core.IntegrationTests.csproj +++ b/test/integration/Elsa.Core.IntegrationTests/Elsa.Core.IntegrationTests.csproj @@ -45,6 +45,7 @@ + diff --git a/test/integration/Elsa.Core.IntegrationTests/Persistence/MongoDb/MongoDbIntegrationTests.cs b/test/integration/Elsa.Core.IntegrationTests/Persistence/MongoDb/MongoDbIntegrationTests.cs new file mode 100644 index 000000000..667ed4257 --- /dev/null +++ b/test/integration/Elsa.Core.IntegrationTests/Persistence/MongoDb/MongoDbIntegrationTests.cs @@ -0,0 +1,46 @@ +using Xunit; +using System.Threading.Tasks; +using Elsa.Core.IntegrationTests.Autofixture; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.DependencyInjection; +using System.Threading; +using Elsa.Services; +using Elsa.Core.IntegrationTests.Workflows; +using Elsa.Persistence; +using System; + +namespace Elsa.Core.IntegrationTests.Persistence.MongoDb +{ + public class MongoDbIntegrationTests + { + [Theory(DisplayName = "A saved workflow instance should be round-trippable without error"), AutoMoqData] + public async Task ASavedWorkflowInstanceShouldBeRoundTrippableWithoutError([HostBuilderWithElsaSampleWorkflowAndMongoDbAttribute] IHostBuilder hostBuilder) + { + hostBuilder.ConfigureServices((ctx, services) => services.AddHostedService()); + var host = await hostBuilder.StartAsync(); + } + + class HostedWorkflowRunner : IHostedService + { + readonly IWorkflowRunner workflowRunner; + readonly IWorkflowInstanceStore instanceStore; + + public async Task StartAsync(CancellationToken cancellationToken) + { + var instance = await workflowRunner.RunWorkflowAsync(); + var retrievedInstance = await instanceStore.FindByIdAsync(instance.Id); + + Assert.NotNull(retrievedInstance); + Assert.NotSame(instance, retrievedInstance); + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + + public HostedWorkflowRunner(IWorkflowRunner workflowRunner, IWorkflowInstanceStore instanceStore) + { + this.workflowRunner = workflowRunner ?? throw new System.ArgumentNullException(nameof(workflowRunner)); + this.instanceStore = instanceStore ?? throw new System.ArgumentNullException(nameof(instanceStore)); + } + } + } +} \ No newline at end of file diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/SampleWorkflow.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/SampleWorkflow.cs new file mode 100644 index 000000000..0ee6bd155 --- /dev/null +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/SampleWorkflow.cs @@ -0,0 +1,20 @@ +using Elsa.Activities.UserTask.Activities; +using Elsa.Builders; +using Elsa.Activities.ControlFlow; + +namespace Elsa.Core.IntegrationTests.Workflows +{ + public class SampleWorkflow : IWorkflow + { + public static readonly object Result = new object(); + + public void Build(IWorkflowBuilder builder) + { + builder + .StartWith(setup: s => { + s.Set(x => x.Actions, c => new [] { "Foo", "Bar" }); + }) + .Finish(x => x.WithOutput(Result)); + } + } +} \ No newline at end of file