Skip to content

Commit

Permalink
Add test to reproduce race condition
Browse files Browse the repository at this point in the history
If the host is stopped as soon as WorkflowCompleted LifeCycleEvent is
raised, some persistence providers cannot persist the 'Completed' state
in time.
  • Loading branch information
mamidenn committed Aug 16, 2022
1 parent 16661ef commit 6bebb2e
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 11 deletions.
16 changes: 15 additions & 1 deletion src/WorkflowCore.Testing/WorkflowTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public abstract class WorkflowTest<TWorkflow, TData> : IDisposable
protected IWorkflowHost Host;
protected IPersistenceProvider PersistenceProvider;
protected List<StepError> UnhandledStepErrors = new List<StepError>();
private bool isDisposed;

protected virtual void Setup()
{
Expand Down Expand Up @@ -116,9 +117,22 @@ protected TData GetData(string workflowId)
return (TData)instance.Data;
}

protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
if (disposing)
{
Host.Stop();
}
isDisposed = true;
}
}

public void Dispose()
{
Host.Stop();
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}

Expand Down
47 changes: 47 additions & 0 deletions test/WorkflowCore.IntegrationTests/Scenarios/StopScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using WorkflowCore.Models.LifeCycleEvents;
using WorkflowCore.Testing;
using Xunit;

namespace WorkflowCore.IntegrationTests.Scenarios
{
public class StopScenario : WorkflowTest<StopScenario.StopWorkflow, object>
{
public class StopWorkflow : IWorkflow
{
public string Id => "StopWorkflow";
public int Version => 1;
public void Build(IWorkflowBuilder<object> builder)
{
builder.StartWith(context => ExecutionResult.Next());
}
}

public StopScenario() => Setup();

[Fact]
public async Task Scenario()
{
var tcs = new TaskCompletionSource<object>();
Host.OnLifeCycleEvent += async (evt) =>
{
if (evt is WorkflowCompleted)
{
await Host.StopAsync(CancellationToken.None);
tcs.SetResult(default);
}
};

var workflowId = StartWorkflow(default);
await tcs.Task;

GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
}

protected override void Dispose(bool disposing) { }
}
}
18 changes: 18 additions & 0 deletions test/WorkflowCore.Tests.DynamoDB/Scenarios/DynamoStopScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using Amazon.DynamoDBv2;
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.IntegrationTests.Scenarios;
using Xunit;

namespace WorkflowCore.Tests.DynamoDB.Scenarios
{
[Collection("DynamoDb collection")]
public class DynamoStopScenario : StopScenario
{
protected override void ConfigureServices(IServiceCollection services)
{
var cfg = new AmazonDynamoDBConfig {ServiceURL = DynamoDbDockerSetup.ConnectionString};
services.AddWorkflow(x => x.UseAwsDynamoPersistence(DynamoDbDockerSetup.Credentials, cfg, "tests-"));
}
}
}
16 changes: 16 additions & 0 deletions test/WorkflowCore.Tests.MongoDB/Scenarios/MongoStopScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.IntegrationTests.Scenarios;
using Xunit;

namespace WorkflowCore.Tests.MongoDB.Scenarios
{
[Collection("Mongo collection")]
public class MongoStopScenario : StopScenario
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddWorkflow(x => x.UseMongoDB(MongoDockerSetup.ConnectionString, "integration-tests"));
}
}
}
16 changes: 16 additions & 0 deletions test/WorkflowCore.Tests.MySQL/Scenarios/MysqlStopScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.IntegrationTests.Scenarios;
using Xunit;

namespace WorkflowCore.Tests.MySQL.Scenarios
{
[Collection("Mysql collection")]
public class MysqlStopScenario : StopScenario
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddWorkflow(x => x.UseMySQL(MysqlDockerSetup.ScenarioConnectionString, true, true));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.IntegrationTests.Scenarios;
using Xunit;

namespace WorkflowCore.Tests.PostgreSQL.Scenarios
{
[Collection("Postgres collection")]
public class PostgresStopScenario : StopScenario
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddWorkflow(x => x.UsePostgreSQL(PostgresDockerSetup.ScenarioConnectionString, true, true));
}
}
}
16 changes: 16 additions & 0 deletions test/WorkflowCore.Tests.Redis/Scenarios/RedisStopScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.IntegrationTests.Scenarios;
using Xunit;

namespace WorkflowCore.Tests.Redis.Scenarios
{
[Collection("Redis collection")]
public class RedisStopScenario : StopScenario
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddWorkflow(x => x.UseRedisPersistence(RedisDockerSetup.ConnectionString, "scenario-"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.IntegrationTests.Scenarios;
using Xunit;

namespace WorkflowCore.Tests.SqlServer.Scenarios
{
[Collection("SqlServer collection")]
public class SqlServerStopScenario : StopScenario
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddWorkflow(x => x.UseSqlServer(SqlDockerSetup.ScenarioConnectionString, true, true));
}
}
}
17 changes: 17 additions & 0 deletions test/WorkflowCore.Tests.Sqlite/Scenarios/SqliteStopScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.IntegrationTests.Scenarios;
using WorkflowCore.Tests.Sqlite;
using Xunit;

namespace WorkflowCore.Tests.Sqlite.Scenarios
{
[Collection("Sqlite collection")]
public class SqliteStopScenario : StopScenario
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddWorkflow(x => x.UseSqlite(SqliteSetup.ConnectionString, true));
}
}
}
2 changes: 1 addition & 1 deletion test/WorkflowCore.Tests.Sqlite/SqliteCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class SqliteCollection : ICollectionFixture<SqliteSetup>

public class SqliteSetup : IDisposable
{
public string ConnectionString { get; set; }
public static string ConnectionString { get; set; }

public SqliteSetup()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,11 @@ namespace WorkflowCore.Tests.Sqlite
[Collection("Sqlite collection")]
public class SqlitePersistenceProviderFixture : BasePersistenceFixture
{
string _connectionString;

public SqlitePersistenceProviderFixture(SqliteSetup setup)
{
_connectionString = setup.ConnectionString;
}

protected override IPersistenceProvider Subject
{
get
{
var db = new EntityFrameworkPersistenceProvider(new SqliteContextFactory(_connectionString), true, false);
{
var db = new EntityFrameworkPersistenceProvider(new SqliteContextFactory(SqliteSetup.ConnectionString), true, false);
db.EnsureStoreExists();
return db;
}
Expand Down

0 comments on commit 6bebb2e

Please sign in to comment.