2727import io .serverlessworkflow .impl .persistence .bigmap .BytesMapPersistenceInstanceHandlers ;
2828import io .serverlessworkflow .impl .persistence .mvstore .MVStorePersistenceStore ;
2929import java .io .IOException ;
30- import java .nio .file .Files ;
3130import java .nio .file .Path ;
3231import java .util .Collection ;
3332import java .util .Map ;
33+ import java .util .stream .Stream ;
34+ import org .junit .jupiter .api .BeforeAll ;
3435import org .junit .jupiter .api .Test ;
36+ import org .junit .jupiter .api .io .TempDir ;
37+ import org .junit .jupiter .api .parallel .Execution ;
38+ import org .junit .jupiter .api .parallel .ExecutionMode ;
39+ import org .junit .jupiter .params .ParameterizedTest ;
40+ import org .junit .jupiter .params .provider .Arguments ;
41+ import org .junit .jupiter .params .provider .MethodSource ;
3542
36- public class MvStorePersistenceTest {
43+ @ Execution (ExecutionMode .SAME_THREAD )
44+ class MvStorePersistenceTest {
45+
46+ @ TempDir static Path tmp ;
47+
48+ static Path runningV1 , suspendedV1 , runningV0 , suspendedV0 ;
49+
50+ @ BeforeAll
51+ static void prepareDbSamples () throws IOException , InterruptedException {
52+ runningV1 = tmp .resolve ("running_v1.db" );
53+ suspendedV1 = tmp .resolve ("suspended_v1.db" );
54+ runningV0 = tmp .resolve ("running.db" );
55+ suspendedV0 = tmp .resolve ("suspended.db" );
56+
57+ ForkedDbGen .run (runningV1 , false );
58+ ForkedDbGen .run (suspendedV1 , true );
59+ ForkedDbGen .run (runningV0 , false );
60+ ForkedDbGen .run (suspendedV0 , true );
61+ }
3762
3863 @ Test
3964 void testSimpleRun () throws IOException {
40- final String dbName = "db-samples/ simple.db" ;
65+ final Path dbPath = tmp . resolve ( " simple.db") ;
4166 try (PersistenceInstanceHandlers handlers =
42- BytesMapPersistenceInstanceHandlers .builder (new MVStorePersistenceStore (dbName ))
67+ BytesMapPersistenceInstanceHandlers .builder (
68+ new MVStorePersistenceStore (dbPath .toString ()))
4369 .build ();
4470 WorkflowApplication application =
4571 PersistenceApplicationBuilder .builder (WorkflowApplication .builder (), handlers .writer ())
46- .build (); ) {
72+ .build ()) {
73+
4774 WorkflowDefinition definition =
4875 application .workflowDefinition (
4976 readWorkflowFromClasspath ("workflows-samples/simple-expression.yaml" ));
77+
5078 assertThat (handlers .reader ().readAll (definition ).values ()).isEmpty ();
79+
5180 definition .instance (Map .of ()).start ().join ();
81+
5282 assertThat (handlers .reader ().readAll (definition ).values ()).isEmpty ();
53- } finally {
54- Files .delete (Path .of (dbName ));
5583 }
5684 }
5785
@@ -60,54 +88,57 @@ void testWaitingInstance() throws IOException {
6088 TaskCounterPerInstanceListener taskCounter = new TaskCounterPerInstanceListener ();
6189 try (WorkflowApplication application =
6290 WorkflowApplication .builder ().withListener (taskCounter ).build ()) {
91+
6392 WorkflowDefinition definition =
6493 application .workflowDefinition (
6594 readWorkflowFromClasspath ("workflows-samples/set-listen-to-any.yaml" ));
6695
6796 WorkflowInstance instance = definition .instance (Map .of ());
6897 instance .start ();
98+
6999 assertThat (taskCounter .taskCounter (instance .id ()).completed ()).isEqualTo (1 );
70100 }
71101 }
72102
73- @ Test
74- void testRestoreWaitingInstanceV0 () throws IOException {
75- runIt ("db-samples/running.db" , WorkflowStatus .WAITING );
103+ @ ParameterizedTest (name = "{index} ⇒ {0} should restore as {1}" )
104+ @ MethodSource ("dbSamples" )
105+ void testRestoreInstances (Path dbFile , WorkflowStatus expectedStatus ) throws IOException {
106+ runIt (dbFile , expectedStatus );
76107 }
77108
78- @ Test
79- void testRestoreSuspendedInstanceV0 () throws IOException {
80- runIt ("db-samples/suspended.db" , WorkflowStatus .SUSPENDED );
109+ private static Stream <Arguments > dbSamples () {
110+ return Stream .of (
111+ Arguments .of (runningV0 , WorkflowStatus .WAITING ),
112+ Arguments .of (suspendedV0 , WorkflowStatus .SUSPENDED ),
113+ Arguments .of (runningV1 , WorkflowStatus .WAITING ),
114+ Arguments .of (suspendedV1 , WorkflowStatus .SUSPENDED ));
81115 }
82116
83- @ Test
84- void testRestoreWaitingInstanceV1 () throws IOException {
85- runIt ("db-samples/running_v1.db" , WorkflowStatus .WAITING );
86- }
87-
88- @ Test
89- void testRestoreSuspendedInstanceV1 () throws IOException {
90- runIt ("db-samples/suspended_v1.db" , WorkflowStatus .SUSPENDED );
91- }
92-
93- private void runIt (String dbName , WorkflowStatus expectedStatus ) throws IOException {
117+ private void runIt (Path dbFile , WorkflowStatus expectedStatus ) throws IOException {
94118 TaskCounterPerInstanceListener taskCounter = new TaskCounterPerInstanceListener ();
119+
95120 try (PersistenceInstanceHandlers handlers =
96- BytesMapPersistenceInstanceHandlers .builder (new MVStorePersistenceStore (dbName ))
121+ BytesMapPersistenceInstanceHandlers .builder (
122+ new MVStorePersistenceStore (dbFile .toString ()))
97123 .build ();
98124 WorkflowApplication application =
99125 PersistenceApplicationBuilder .builder (
100126 WorkflowApplication .builder ()
101127 .withListener (taskCounter )
102128 .withListener (new TraceExecutionListener ()),
103129 handlers .writer ())
104- .build (); ) {
130+ .build ()) {
131+
105132 WorkflowDefinition definition =
106133 application .workflowDefinition (
107134 readWorkflowFromClasspath ("workflows-samples/set-listen-to-any.yaml" ));
135+
108136 Collection <WorkflowInstance > instances = handlers .reader ().readAll (definition ).values ();
137+
109138 assertThat (instances ).hasSize (1 );
139+
110140 instances .forEach (WorkflowInstance ::start );
141+
111142 assertThat (instances )
112143 .singleElement ()
113144 .satisfies (
0 commit comments