Skip to content

Commit 8a22a15

Browse files
committed
refactor scenario tests
1 parent be4091f commit 8a22a15

30 files changed

+213
-220
lines changed

test/WorkflowCore.IntegrationTests/Scenarios/BaseScenario.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace WorkflowCore.IntegrationTests.Scenarios
99
{
10+
[Obsolete]
1011
public abstract class BaseScenario<TWorkflow, TData> : IDisposable
1112
where TWorkflow : IWorkflow<TData>, new()
1213
where TData : class, new()

test/WorkflowCore.IntegrationTests/Scenarios/BasicScenario.cs

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,59 @@
55
using WorkflowCore.Models;
66
using Xunit;
77
using FluentAssertions;
8+
using WorkflowCore.Testing;
89

910
namespace WorkflowCore.IntegrationTests.Scenarios
1011
{
11-
public class BasicScenario : BaseScenario<BasicScenario.BasicWorkflow, Object>
12+
public class BasicWorkflow : IWorkflow
1213
{
13-
static int Step1Ticker = 0;
14-
static int Step2Ticker = 0;
14+
internal static int Step1Ticker = 0;
15+
internal static int Step2Ticker = 0;
1516

16-
public class Step1 : StepBody
17+
public string Id => "BasicWorkflow";
18+
public int Version => 1;
19+
public void Build(IWorkflowBuilder<Object> builder)
1720
{
18-
public override ExecutionResult Run(IStepExecutionContext context)
19-
{
20-
Step1Ticker++;
21-
return ExecutionResult.Next();
22-
}
21+
builder
22+
.StartWith<Step1>()
23+
.Then(context =>
24+
{
25+
Step2Ticker++;
26+
return ExecutionResult.Next();
27+
});
28+
2329
}
30+
}
2431

25-
public class BasicWorkflow : IWorkflow
32+
internal class Step1 : StepBody
33+
{
34+
public override ExecutionResult Run(IStepExecutionContext context)
2635
{
27-
public string Id => "BasicWorkflow";
28-
public int Version => 1;
29-
public void Build(IWorkflowBuilder<Object> builder)
30-
{
31-
builder
32-
.StartWith<Step1>()
33-
.Then(context =>
34-
{
35-
Step2Ticker++;
36-
return ExecutionResult.Next();
37-
});
38-
39-
}
36+
BasicWorkflow.Step1Ticker++;
37+
return ExecutionResult.Next();
38+
}
39+
}
40+
41+
public class BasicScenario : WorkflowTest<BasicWorkflow, Object>
42+
{
43+
public BasicScenario()
44+
{
45+
Setup();
4046
}
4147

4248
[Fact]
4349
public void Scenario()
4450
{
45-
var workflowId = Host.StartWorkflow("BasicWorkflow").Result;
46-
var instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
47-
int counter = 0;
48-
while ((instance.Status == WorkflowStatus.Runnable) && (counter < 300))
49-
{
50-
System.Threading.Thread.Sleep(100);
51-
counter++;
52-
instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
53-
}
54-
55-
instance.Status.Should().Be(WorkflowStatus.Complete);
56-
Step1Ticker.Should().Be(1);
57-
Step2Ticker.Should().Be(1);
51+
BasicWorkflow.Step1Ticker = 0;
52+
BasicWorkflow.Step2Ticker = 0;
53+
54+
var workflowId = StartWorkflow(null);
55+
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));
56+
57+
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
58+
UnhandledStepErrors.Count.Should().Be(0);
59+
BasicWorkflow.Step1Ticker.Should().Be(1);
60+
BasicWorkflow.Step2Ticker.Should().Be(1);
5861
}
5962
}
6063
}

test/WorkflowCore.IntegrationTests/Scenarios/DataIOScenario.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
using WorkflowCore.Models;
66
using Xunit;
77
using FluentAssertions;
8+
using WorkflowCore.Testing;
89

910
namespace WorkflowCore.IntegrationTests.Scenarios
1011
{
11-
public class DataIOScenario : BaseScenario<DataIOScenario.DataIOWorkflow, DataIOScenario.MyDataClass>
12+
public class DataIOScenario : WorkflowTest<DataIOScenario.DataIOWorkflow, DataIOScenario.MyDataClass>
1213
{
1314
public class AddNumbers : StepBody
1415
{
@@ -44,21 +45,20 @@ public void Build(IWorkflowBuilder<MyDataClass> builder)
4445
}
4546
}
4647

48+
public DataIOScenario()
49+
{
50+
Setup();
51+
}
52+
4753
[Fact]
4854
public void Scenario()
4955
{
50-
var workflowId = Host.StartWorkflow("DataIOWorkflow", new MyDataClass() { Value1 = 2, Value2 = 3 }).Result;
51-
var instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
52-
int counter = 0;
53-
while ((instance.Status == WorkflowStatus.Runnable) && (counter < 300))
54-
{
55-
System.Threading.Thread.Sleep(100);
56-
counter++;
57-
instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
58-
}
56+
var workflowId = StartWorkflow(new MyDataClass() { Value1 = 2, Value2 = 3 });
57+
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));
5958

60-
instance.Status.Should().Be(WorkflowStatus.Complete);
61-
(instance.Data as MyDataClass).Value3.Should().Be(5);
59+
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
60+
UnhandledStepErrors.Count.Should().Be(0);
61+
GetData(workflowId).Value3.Should().Be(5);
6262
}
6363
}
6464
}

test/WorkflowCore.IntegrationTests/Scenarios/EndStepScenario.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
using System.Text;
55
using WorkflowCore.Interface;
66
using WorkflowCore.Models;
7+
using WorkflowCore.Testing;
78
using Xunit;
89

910
namespace WorkflowCore.IntegrationTests.Scenarios
1011
{
11-
public class EndStepScenario : BaseScenario<EndStepScenario.ScenarioWorkflow, Object>
12+
public class EndStepScenario : WorkflowTest<EndStepScenario.ScenarioWorkflow, Object>
1213
{
1314
internal static int StartStepCounter = 0;
1415
internal static int MidStepCounter = 0;
@@ -42,20 +43,18 @@ public void Build(IWorkflowBuilder<Object> builder)
4243
}
4344
}
4445

46+
public EndStepScenario()
47+
{
48+
Setup();
49+
}
50+
4551
[Fact]
4652
public void Scenario()
4753
{
48-
var workflowId = Host.StartWorkflow("EndStepScenario").Result;
49-
var instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
50-
int counter = 0;
51-
while ((instance.Status == WorkflowStatus.Runnable) && (counter < 300))
52-
{
53-
System.Threading.Thread.Sleep(100);
54-
counter++;
55-
instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
56-
}
54+
var workflowId = StartWorkflow(null);
55+
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));
5756

58-
instance.Status.Should().Be(WorkflowStatus.Complete);
57+
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
5958
StartStepCounter.Should().Be(1);
6059
MidStepCounter.Should().Be(1);
6160
EndStepCounter.Should().Be(0);

test/WorkflowCore.IntegrationTests/Scenarios/EventScenario.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
using Xunit;
77
using FluentAssertions;
88
using System.Linq;
9+
using WorkflowCore.Testing;
910

1011
namespace WorkflowCore.IntegrationTests.Scenarios
1112
{
12-
public class EventScenario : BaseScenario<EventScenario.EventWorkflow, EventScenario.MyDataClass>
13+
public class EventScenario : WorkflowTest<EventScenario.EventWorkflow, EventScenario.MyDataClass>
1314
{
1415
public class MyDataClass
1516
{
@@ -29,31 +30,23 @@ public void Build(IWorkflowBuilder<MyDataClass> builder)
2930
}
3031
}
3132

33+
public EventScenario()
34+
{
35+
Setup();
36+
}
37+
3238
[Fact]
3339
public void Scenario()
3440
{
35-
var workflowId = Host.StartWorkflow("EventWorkflow", new MyDataClass() { StrValue = "0" }).Result;
36-
37-
int counter = 0;
38-
while ((PersistenceProvider.GetSubcriptions("MyEvent", "0", DateTime.MaxValue).Result.Count() == 0) && (counter < 150))
39-
{
40-
System.Threading.Thread.Sleep(200);
41-
counter++;
42-
}
43-
44-
Host.PublishEvent("MyEvent", "0", "Pass");
45-
46-
var instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
47-
counter = 0;
48-
while ((instance.Status == WorkflowStatus.Runnable) && (counter < 150))
49-
{
50-
System.Threading.Thread.Sleep(200);
51-
counter++;
52-
instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
53-
}
41+
var eventKey = Guid.NewGuid().ToString();
42+
var workflowId = StartWorkflow(new MyDataClass() { StrValue = eventKey });
43+
WaitForEventSubscription("MyEvent", eventKey, TimeSpan.FromSeconds(30));
44+
Host.PublishEvent("MyEvent", eventKey, "Pass");
45+
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));
5446

55-
instance.Status.Should().Be(WorkflowStatus.Complete);
56-
(instance.Data as MyDataClass).StrValue.Should().Be("Pass");
47+
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
48+
UnhandledStepErrors.Count.Should().Be(0);
49+
GetData(workflowId).StrValue.Should().Be("Pass");
5750
}
5851
}
5952
}

test/WorkflowCore.IntegrationTests/Scenarios/ForeachScenario.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
using WorkflowCore.Models;
66
using Xunit;
77
using FluentAssertions;
8+
using WorkflowCore.Testing;
89

910
namespace WorkflowCore.IntegrationTests.Scenarios
1011
{
11-
public class ForeachScenario : BaseScenario<ForeachScenario.ForeachWorkflow, ForeachScenario.MyDataClass>
12+
public class ForeachScenario : WorkflowTest<ForeachScenario.ForeachWorkflow, ForeachScenario.MyDataClass>
1213
{
13-
static int Step1Ticker = 0;
14-
static int Step2Ticker = 0;
15-
static int Step3Ticker = 0;
16-
static int AfterLoopValue = 0;
17-
static int CheckSum = 0;
14+
internal static int Step1Ticker = 0;
15+
internal static int Step2Ticker = 0;
16+
internal static int Step3Ticker = 0;
17+
internal static int AfterLoopValue = 0;
18+
internal static int CheckSum = 0;
1819

1920
public class DoSomething : StepBody
2021
{
@@ -54,25 +55,24 @@ public void Build(IWorkflowBuilder<MyDataClass> builder)
5455
}
5556
}
5657

58+
public ForeachScenario()
59+
{
60+
Setup();
61+
}
62+
5763
[Fact]
5864
public void Scenario()
5965
{
60-
var workflowId = Host.StartWorkflow("ForeachWorkflow").Result;
61-
var instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
62-
int counter = 0;
63-
while ((instance.Status == WorkflowStatus.Runnable) && (counter < 300))
64-
{
65-
System.Threading.Thread.Sleep(100);
66-
counter++;
67-
instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
68-
}
66+
var workflowId = StartWorkflow(null);
67+
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));
6968

7069
Step1Ticker.Should().Be(1);
7170
Step2Ticker.Should().Be(3);
7271
Step3Ticker.Should().Be(1);
7372
AfterLoopValue.Should().Be(3);
7473
CheckSum.Should().Be(7);
75-
instance.Status.Should().Be(WorkflowStatus.Complete);
74+
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
75+
UnhandledStepErrors.Count.Should().Be(0);
7676
}
7777
}
7878
}

test/WorkflowCore.IntegrationTests/Scenarios/IfScenario.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
using Xunit;
77
using FluentAssertions;
88
using System.Threading;
9+
using WorkflowCore.Testing;
910

1011
namespace WorkflowCore.IntegrationTests.Scenarios
1112
{
12-
public class IfScenario : BaseScenario<IfScenario.IfWorkflow, IfScenario.MyDataClass>
13+
public class IfScenario : WorkflowTest<IfScenario.IfWorkflow, IfScenario.MyDataClass>
1314
{
14-
static int Step1Ticker = 0;
15-
static int Step2Ticker = 0;
16-
static int If1Ticker = 0;
17-
static int If2Ticker = 0;
18-
static int If3Ticker = 0;
19-
static DateTime LastIfBlock;
20-
static DateTime AfterIfBlock;
15+
internal static int Step1Ticker = 0;
16+
internal static int Step2Ticker = 0;
17+
internal static int If1Ticker = 0;
18+
internal static int If2Ticker = 0;
19+
internal static int If3Ticker = 0;
20+
internal static DateTime LastIfBlock;
21+
internal static DateTime AfterIfBlock;
2122

2223
public class MyDataClass
2324
{
@@ -69,18 +70,16 @@ public void Build(IWorkflowBuilder<MyDataClass> builder)
6970
}
7071
}
7172

73+
public IfScenario()
74+
{
75+
Setup();
76+
}
77+
7278
[Fact]
7379
public void Scenario()
7480
{
75-
var workflowId = Host.StartWorkflow("IfWorkflow", new MyDataClass() { Counter = 2 }).Result;
76-
var instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
77-
int counter = 0;
78-
while ((instance.Status == WorkflowStatus.Runnable) && (counter < 300))
79-
{
80-
Thread.Sleep(100);
81-
counter++;
82-
instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;
83-
}
81+
var workflowId = StartWorkflow(new MyDataClass() { Counter = 2 });
82+
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));
8483

8584
Step1Ticker.Should().Be(1);
8685
Step2Ticker.Should().Be(1);
@@ -91,7 +90,8 @@ public void Scenario()
9190

9291
AfterIfBlock.Should().BeAfter(LastIfBlock);
9392

94-
instance.Status.Should().Be(WorkflowStatus.Complete);
93+
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
94+
UnhandledStepErrors.Count.Should().Be(0);
9595
}
9696
}
9797
}

0 commit comments

Comments
 (0)