|
5 | 5 | import dev.dbos.transact.context.DBOSOptions; |
6 | 6 | import dev.dbos.transact.context.SetDBOSOptions; |
7 | 7 | import dev.dbos.transact.database.SystemDatabase; |
| 8 | +import dev.dbos.transact.exceptions.WorkflowCancelledException; |
8 | 9 | import dev.dbos.transact.execution.DBOSExecutor; |
| 10 | +import dev.dbos.transact.execution.ExecutingService; |
| 11 | +import dev.dbos.transact.queue.Queue; |
9 | 12 | import dev.dbos.transact.utils.DBUtils; |
10 | 13 | import org.junit.jupiter.api.AfterEach; |
11 | 14 | import org.junit.jupiter.api.BeforeAll; |
|
20 | 23 | import java.sql.SQLException; |
21 | 24 | import java.sql.Statement; |
22 | 25 | import java.util.concurrent.CountDownLatch; |
| 26 | +import java.util.concurrent.ExecutorService; |
| 27 | +import java.util.concurrent.Executors; |
23 | 28 |
|
24 | 29 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
25 | 31 |
|
26 | 32 | public class WorkflowMgmtTest { |
27 | 33 |
|
@@ -103,14 +109,160 @@ public void asyncCancelResumeTest() throws Exception { |
103 | 109 | workLatch.countDown(); |
104 | 110 |
|
105 | 111 | assertEquals(1, mgmtService.getStepsExecuted()) ; |
| 112 | + WorkflowHandle h = dbosExecutor.retrieveWorkflow(workflowId) ; |
| 113 | + assertEquals(WorkflowState.CANCELLED.name(), h.getStatus().getStatus()); |
106 | 114 |
|
107 | 115 | WorkflowHandle<?> handle = dbos.resumeWorkflow(workflowId) ; |
108 | 116 |
|
109 | 117 | result = (Integer) handle.getResult() ; |
110 | 118 | assertEquals(23, result); |
111 | 119 | assertEquals(3, mgmtService.getStepsExecuted()) ; |
112 | 120 |
|
| 121 | + // resume again |
| 122 | + |
| 123 | + handle = dbos.resumeWorkflow(workflowId) ; |
| 124 | + |
| 125 | + result = (Integer) handle.getResult() ; |
| 126 | + assertEquals(23, result); |
| 127 | + assertEquals(3, mgmtService.getStepsExecuted()) ; |
| 128 | + |
| 129 | + logger.info("Test completed"); |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void queuedCancelResumeTest() throws Exception { |
| 135 | + |
| 136 | + CountDownLatch mainLatch = new CountDownLatch(1); |
| 137 | + CountDownLatch workLatch = new CountDownLatch(1); |
| 138 | + |
| 139 | + MgmtService mgmtService = dbos.<MgmtService>Workflow() |
| 140 | + .interfaceClass(MgmtService.class) |
| 141 | + .implementation(new MgmtServiceImpl(mainLatch, workLatch)) |
| 142 | + .build(); |
| 143 | + mgmtService.setMgmtService(mgmtService); |
| 144 | + |
| 145 | + Queue myqueue = new DBOS.QueueBuilder("myqueue").build(); |
| 146 | + |
| 147 | + String workflowId = "wfid1" ; |
| 148 | + DBOSOptions options = new DBOSOptions.Builder(workflowId).queue(myqueue).build(); |
| 149 | + int result ; |
| 150 | + try (SetDBOSOptions o = new SetDBOSOptions(options)) { |
| 151 | + mgmtService.simpleWorkflow(23); |
| 152 | + } |
| 153 | + |
| 154 | + mainLatch.await(); |
| 155 | + dbos.cancelWorkflow(workflowId); |
| 156 | + workLatch.countDown(); |
| 157 | + |
| 158 | + assertEquals(1, mgmtService.getStepsExecuted()) ; |
| 159 | + WorkflowHandle h = dbosExecutor.retrieveWorkflow(workflowId) ; |
| 160 | + assertEquals(WorkflowState.CANCELLED.name(), h.getStatus().getStatus()); |
| 161 | + |
| 162 | + WorkflowHandle<?> handle = dbos.resumeWorkflow(workflowId) ; |
| 163 | + |
| 164 | + result = (Integer) handle.getResult() ; |
| 165 | + assertEquals(23, result); |
| 166 | + assertEquals(3, mgmtService.getStepsExecuted()) ; |
| 167 | + |
| 168 | + // resume again |
| 169 | + |
| 170 | + handle = dbos.resumeWorkflow(workflowId) ; |
| 171 | + |
| 172 | + result = (Integer) handle.getResult() ; |
| 173 | + assertEquals(23, result); |
| 174 | + assertEquals(3, mgmtService.getStepsExecuted()) ; |
| 175 | + |
113 | 176 | logger.info("Test completed"); |
114 | 177 |
|
115 | 178 | } |
| 179 | + |
| 180 | + |
| 181 | + @Test |
| 182 | + public void syncCancelResumeTest() throws Exception { |
| 183 | + |
| 184 | + CountDownLatch mainLatch = new CountDownLatch(1); |
| 185 | + CountDownLatch workLatch = new CountDownLatch(1); |
| 186 | + |
| 187 | + MgmtService mgmtService = dbos.<MgmtService>Workflow() |
| 188 | + .interfaceClass(MgmtService.class) |
| 189 | + .implementation(new MgmtServiceImpl(mainLatch, workLatch)) |
| 190 | + .build(); |
| 191 | + mgmtService.setMgmtService(mgmtService); |
| 192 | + |
| 193 | + |
| 194 | + ExecutorService e = Executors.newFixedThreadPool(2); |
| 195 | + String workflowId = "wfid1" ; |
| 196 | + |
| 197 | + CountDownLatch testLatch = new CountDownLatch(2) ; |
| 198 | + |
| 199 | + e.submit(() -> { |
| 200 | + |
| 201 | + DBOSOptions options = new DBOSOptions.Builder(workflowId).build(); |
| 202 | + |
| 203 | + try { |
| 204 | + try (SetDBOSOptions o = new SetDBOSOptions(options)) { |
| 205 | + mgmtService.simpleWorkflow(23); |
| 206 | + } |
| 207 | + } catch(Throwable t) { |
| 208 | + logger.info("caught ex"); |
| 209 | + // assertTrue(t instanceof WorkflowCancelledException) ; |
| 210 | + } |
| 211 | + |
| 212 | + assertEquals(1, mgmtService.getStepsExecuted()) ; |
| 213 | + logger.info("counting down latch") ; |
| 214 | + testLatch.countDown(); |
| 215 | + logger.info("exiting thread 1") ; |
| 216 | + }) ; |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + // mainLatch.await(); |
| 221 | + // dbos.cancelWorkflow(workflowId); |
| 222 | + // workLatch.countDown(); |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + e.submit(() -> { |
| 227 | + try { |
| 228 | + mainLatch.await(); |
| 229 | + dbos.cancelWorkflow(workflowId); |
| 230 | + workLatch.countDown(); |
| 231 | + |
| 232 | + logger.info("2 counting down lotch") ; |
| 233 | + testLatch.countDown(); |
| 234 | + logger.info("2 after counting down lotch") ; |
| 235 | + |
| 236 | + |
| 237 | + } catch(InterruptedException ie) { |
| 238 | + logger.error(ie.toString()) ; |
| 239 | + } |
| 240 | + logger.info("Exiting thread 2") ; |
| 241 | + }) ; |
| 242 | + |
| 243 | + logger.info("Before test latch"); |
| 244 | + testLatch.await(); |
| 245 | + logger.info("after test latch"); |
| 246 | + |
| 247 | + |
| 248 | + |
| 249 | + WorkflowHandle<?> handle = dbos.resumeWorkflow(workflowId) ; |
| 250 | + |
| 251 | + int result = (Integer) handle.getResult() ; |
| 252 | + assertEquals(23, result); |
| 253 | + assertEquals(3, mgmtService.getStepsExecuted()) ; |
| 254 | + |
| 255 | + // resume again |
| 256 | + |
| 257 | + handle = dbos.resumeWorkflow(workflowId) ; |
| 258 | + |
| 259 | + result = (Integer) handle.getResult() ; |
| 260 | + assertEquals(23, result); |
| 261 | + assertEquals(3, mgmtService.getStepsExecuted()) ; |
| 262 | + |
| 263 | + logger.info("Test completed"); |
| 264 | + |
| 265 | + } |
| 266 | + |
| 267 | + |
116 | 268 | } |
0 commit comments