Skip to content

Commit

Permalink
public api is async
Browse files Browse the repository at this point in the history
  • Loading branch information
kbilsted committed Sep 21, 2023
1 parent 8d130d6 commit 2c893c9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/Demos/GreenFeetWorkFlow.Tests/AdoSingletonStepTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public void When_creating_two_identical_singleton_Then_fail()
var step = new Step(helper.RndName) { Singleton = true, };
var step2 = new Step(helper.RndName) { Singleton = true, };

Action act = () => engine.Data.AddSteps(new[] { step, step2 });
Func<Task> act = () => engine.Data.AddStepsAsync(new[] { step, step2 });

act.Should()
.Throw<SqlException>()
.ThrowAsync<SqlException>()
.WithMessage("Cannot insert duplicate key row*");
}
}
30 changes: 15 additions & 15 deletions src/Demos/GreenFeetWorkFlow.Tests/AdoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void When_executing_OneStep_with_state_Then_succeed()
}

[Test]
public void When_adding_two_steps_in_the_same_transaction_Then_succeed()
public async Task When_adding_two_steps_in_the_same_transaction_Then_succeed()
{
string[] stepResults = new string[2];
const string name = "v1/When_adding_two_steps_in_the_same_transaction_Then_succeed";
Expand All @@ -53,11 +53,11 @@ public void When_adding_two_steps_in_the_same_transaction_Then_succeed()
return ExecutionResult.Done();
})));

using var connection = new SqlConnection(helper.ConnectionString);
await using var connection = new SqlConnection(helper.ConnectionString);
connection.Open();
using var tx = connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
engine.Data.AddStep(new Step(name, 0), tx);
engine.Data.AddStep(new Step(name, 1), tx);
await using var tx = connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
await engine.Data.AddStepAsync(new Step(name, 0), tx);
await engine.Data.AddStepAsync(new Step(name, 1), tx);
tx.Commit();
engine.Start(cfg);

Expand Down Expand Up @@ -102,10 +102,10 @@ public async Task When_executing_step_throwing_special_FailCurrentStepException_
name,
GenericImplementation.Create(step => throw step.FailAsException(newSteps: new Step(nameNewStep))))
);
engine.Data.AddStep(new Step(name) { FlowId = helper.FlowId });
await engine.Data.AddStepAsync(new Step(name) { FlowId = helper.FlowId });
await engine.StartAsSingleWorker(cfg);

var steps = engine.Data.SearchSteps(new SearchModel(FlowId: helper.FlowId), FetchLevels.ALL);
var steps = await engine.Data.SearchStepsAsync(new SearchModel(FlowId: helper.FlowId), FetchLevels.ALL);

steps.Is(@" [
{
Expand Down Expand Up @@ -360,15 +360,15 @@ public void When_step_is_in_the_future_Then_it_wont_execute()
FlowId = helper.FlowId,
ScheduleTime = DateTime.Now.AddYears(35)
};
var id = engine.Data.AddStep(futureStep, null);
var id = engine.Data.AddStepAsync(futureStep, null);
engine.Start(cfg);
helper.AssertTableCounts(helper.FlowId, ready: 1, done: 0, failed: 0);

stepResult.Should().BeNull();
}

[Test]
public void When_step_is_in_the_future_Then_it_can_be_activated_to_execute_now()
public async Task When_step_is_in_the_future_Then_it_can_be_activated_to_execute_now()
{
string? stepResult = null;

Expand All @@ -381,8 +381,8 @@ public void When_step_is_in_the_future_Then_it_can_be_activated_to_execute_now()
FlowId = helper.FlowId,
ScheduleTime = DateTime.Now.AddYears(35)
};
var id = engine.Data.AddStep(futureStep, null);
var count = engine.Data.ActivateStep(id, null);
var id = await engine.Data.AddStepAsync(futureStep, null);
var count = await engine.Data.ActivateStepAsync(id, null);
count.Should().Be(1);

engine.Start(cfg);
Expand All @@ -393,7 +393,7 @@ public void When_step_is_in_the_future_Then_it_can_be_activated_to_execute_now()
}

[Test]
public void When_step_is_in_the_future_Then_it_can_be_activated_to_execute_now_with_args()
public async Task When_step_is_in_the_future_Then_it_can_be_activated_to_execute_now_with_args()
{
string? stepResult = null;
string args = "1234";
Expand All @@ -406,8 +406,8 @@ public void When_step_is_in_the_future_Then_it_can_be_activated_to_execute_now_w
FlowId = helper.FlowId,
ScheduleTime = DateTime.Now.AddYears(35)
};
var id = engine.Data.AddStep(futureStep, null);
var count = engine.Data.ActivateStep(id, args);
var id = await engine.Data.AddStepAsync(futureStep, null);
var count = await engine.Data.ActivateStepAsync(id, args);
count.Should().Be(1);
engine.Start(cfg);

Expand Down Expand Up @@ -460,7 +460,7 @@ public void When_a_step_creates_two_steps_Then_those_steps_can_be_synchronized_a
(int count, Guid id, DateTime maxWait) = helper.Formatter!.Deserialize<(int, Guid, DateTime)>(step.State);
var sales = GroceryBuyer.SalesDb.Where(x => x.id == id).ToArray();
if (sales.Length != 2 && DateTime.Now <= maxWait)
return ExecutionResult.Rerun(scheduleTime: DateTime.Now.AddSeconds(0.1));
return ExecutionResult.Rerun(scheduleTime: DateTime.Now.AddSeconds(0.2));

stepResult = $"total: {sales.Sum(x => x.total)}";
helper.cts.Cancel();
Expand Down
6 changes: 3 additions & 3 deletions src/Demos/GreenFeetWorkFlow.Tests/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public void Setup()
}

[Test]
public void When_adding_an_event_Then_an_id_PK_is_returned()
public async Task When_adding_an_event_Then_an_id_PK_is_returned()
{
helper.CreateEngine();
Step step = new Step(helper.RndName) { ScheduleTime = DateTime.Now.AddMonths(1) };
var id = helper.Engine!.Data.AddStep(step);
var id = await helper.Engine!.Data.AddStepAsync(step);

var result = helper.Engine.Data.SearchSteps(new(Id: id), StepStatus.Ready);
var result = await helper.Engine.Data.SearchStepsAsync(new(Id: id), StepStatus.Ready);

result.Single().Name.Should().Be(helper.RndName);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Demos/GreenFeetWorkFlow.Tests/PerformanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace GreenFeetWorkflow.Tests;
public class PerformanceTests
{
[Test]
public void Inserting_10000_steps_timing()
public async Task Inserting_10000_steps_timing()
{
var helper = new TestHelper();

Expand All @@ -16,7 +16,7 @@ public void Inserting_10000_steps_timing()
var name = "inserttest";

var steps = Enumerable.Range(0, 10000).Select(x => new Step(name)).ToArray();
engine.Data.AddSteps(steps);
await engine.Data.AddStepsAsync(steps);

watch.Stop();
watch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(3));
Expand Down
30 changes: 15 additions & 15 deletions src/Demos/GreenFeetWorkFlow.Tests/RuntimeDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ public class RuntimeDataTests
NumberOfWorkers: 1);

[SetUp]
public void Setup()
public async Task Setup()
{
helper = new TestHelper();
}

[Test]
public void When_searching_with_no_parameters_Then_success()
public async Task When_searching_with_no_parameters_Then_success()
{
var engine = helper.CreateEngine();
var steps = engine.Data.SearchSteps(new SearchModel(), FetchLevels.ALL);
var steps = await engine.Data.SearchStepsAsync(new SearchModel(), FetchLevels.ALL);

steps.Keys.Count.Should().Be(3);
}

[Test]
public void When_SearchSteps_Then_success()
public async Task When_SearchSteps_Then_success()
{
var engine = helper.CreateEngine();
var step = new Step(helper.RndName)
Expand All @@ -39,47 +39,47 @@ public void When_SearchSteps_Then_success()
SearchKey = Guid.NewGuid().ToString(),
Description = Guid.NewGuid().ToString(),
};
var id = engine.Data.AddStep(step, null);
var id = await engine.Data.AddStepAsync(step, null);

FetchLevels fetchLevels = FetchLevels.ALL;
var steps = engine.Data.SearchSteps(new SearchModel()
var steps = await engine.Data.SearchStepsAsync(new SearchModel()
{
Id = id,
}, fetchLevels);
steps[StepStatus.Ready].Single().Id.Should().Be(id);

steps = engine.Data.SearchSteps(new SearchModel()
steps = await engine.Data.SearchStepsAsync(new SearchModel()
{
CorrelationId = step.CorrelationId,
}, fetchLevels);
steps[StepStatus.Ready].Single().Id.Should().Be(id);

steps = engine.Data.SearchSteps(new SearchModel()
steps = await engine.Data.SearchStepsAsync(new SearchModel()
{
Name = step.Name,
}, fetchLevels);
steps[StepStatus.Ready].Single().Id.Should().Be(id);

steps = engine.Data.SearchSteps(new SearchModel()
steps = await engine.Data.SearchStepsAsync(new SearchModel()
{
FlowId = step.FlowId,
}, fetchLevels);
steps[StepStatus.Ready].Single().Id.Should().Be(id);

steps = engine.Data.SearchSteps(new SearchModel()
steps = await engine.Data.SearchStepsAsync(new SearchModel()
{
SearchKey = step.SearchKey,
}, fetchLevels);
steps[StepStatus.Ready].Single().Id.Should().Be(id);

steps = engine.Data.SearchSteps(new SearchModel()
steps = await engine.Data.SearchStepsAsync(new SearchModel()
{
Description = step.Description,
}, fetchLevels);
steps[StepStatus.Ready].Single().Id.Should().Be(id);

// combined search
steps = engine.Data.SearchSteps(new SearchModel()
steps = await engine.Data.SearchStepsAsync(new SearchModel()
{
Id = id,
CorrelationId = step.CorrelationId,
Expand All @@ -99,11 +99,11 @@ public async Task When_reexecuting_a_step_Then_execute_it()

var stepState = 12345;
var step = new Step("v1/fail-and-reactivate", stepState) { FlowId = helper.FlowId, CorrelationId = helper.CorrelationId };
var id = engine.Data.AddStep(step);
var id = await engine.Data.AddStepAsync(step);
await engine.StartAsSingleWorker(cfg);

var newId = engine.Data
.ReExecuteSteps(new SearchModel(Id: id))
var newId = (await engine.Data
.ReExecuteStepsAsync(new SearchModel(Id: id)))
.Single();

var persister = helper.Persister;
Expand Down
12 changes: 6 additions & 6 deletions src/Demos/GreenFeetWorkFlow.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public WorkflowEngine CreateEngine(params (string, IStepImplementation)[] stepHa
return Engine;
}

public void CreateAndRunEngineForPerformance(Step[] steps, int workerCount, params (string, IStepImplementation)[] stepHandlers)
public async Task CreateAndRunEngineForPerformance(Step[] steps, int workerCount, params (string, IStepImplementation)[] stepHandlers)
{
logger = new DiagnosticsStepLogger();
logger.Configuration.TraceLoggingEnabledUntil = DateTime.MinValue;
Expand All @@ -74,7 +74,7 @@ public void CreateAndRunEngineForPerformance(Step[] steps, int workerCount, para
iocContainer = new AutofacAdaptor(builder.Build());
Engine = new WorkflowEngine(logger, iocContainer, Formatter);

Engine.Data.AddSteps(steps);
await Engine.Data.AddStepsAsync(steps);

var workflowConfiguration = new WorkflowConfiguration(new WorkerConfig()
{
Expand All @@ -84,7 +84,7 @@ public void CreateAndRunEngineForPerformance(Step[] steps, int workerCount, para
Engine.Start(workflowConfiguration, stoppingToken: cts.Token);
}

public void CreateAndRunEngine(Step[] steps, int workerCount, params (string, IStepImplementation)[] stepHandlers)
public async Task CreateAndRunEngine(Step[] steps, int workerCount, params (string, IStepImplementation)[] stepHandlers)
{
logger = new DiagnosticsStepLogger();

Expand All @@ -96,7 +96,7 @@ public void CreateAndRunEngine(Step[] steps, int workerCount, params (string, IS
iocContainer = new AutofacAdaptor(builder.Build());
Engine = new WorkflowEngine(logger, iocContainer, Formatter);

Engine.Data.AddSteps(steps);
await Engine.Data.AddStepsAsync(steps);

var workflowConfiguration = new WorkflowConfiguration(new WorkerConfig()
{
Expand All @@ -109,7 +109,7 @@ public void CreateAndRunEngine(Step[] steps, int workerCount, params (string, IS
Engine.Start(workflowConfiguration, stoppingToken: cts.Token);
}

public void CreateAndRunEngineWithAttributes(Step[] steps, int workerCount)
public async Task CreateAndRunEngineWithAttributes(Step[] steps, int workerCount)
{
logger = new DiagnosticsStepLogger();

Expand All @@ -121,7 +121,7 @@ public void CreateAndRunEngineWithAttributes(Step[] steps, int workerCount)
iocContainer = new AutofacAdaptor(builder.Build());
Engine = new WorkflowEngine(logger, iocContainer, Formatter);

Engine.Data.AddSteps(steps);
await Engine.Data.AddStepsAsync(steps);

var workflowConfiguration = new WorkflowConfiguration(new WorkerConfig()
{ StopWhenNoWork = workerCount == 1 },
Expand Down
2 changes: 1 addition & 1 deletion src/Demos/GreenFeetWorkFlow.WebApiDemo/WorkflowStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public WorkflowStarter(WorkflowEngine engine)

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
engine.Data.AddStep(new Step(StepFetchWeatherForecast.Name) { Singleton = true });
await engine.Data.AddStepAsync(new Step(StepFetchWeatherForecast.Name) { Singleton = true });

await engine.StartAsync(new WorkflowConfiguration(new WorkerConfig(), NumberOfWorkers: 1), stoppingToken: stoppingToken);
}
Expand Down
Loading

0 comments on commit 2c893c9

Please sign in to comment.