|
| 1 | +package dev.dbos.transact.workflow; |
| 2 | + |
| 3 | +import dev.dbos.transact.DBOS; |
| 4 | +import dev.dbos.transact.config.DBOSConfig; |
| 5 | +import dev.dbos.transact.context.DBOSOptions; |
| 6 | +import dev.dbos.transact.context.SetDBOSOptions; |
| 7 | +import dev.dbos.transact.database.SystemDatabase; |
| 8 | +import dev.dbos.transact.execution.DBOSExecutor; |
| 9 | +import dev.dbos.transact.utils.DBUtils; |
| 10 | +import org.junit.jupiter.api.AfterEach; |
| 11 | +import org.junit.jupiter.api.BeforeAll; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import javax.sql.DataSource; |
| 18 | +import java.sql.Connection; |
| 19 | +import java.sql.DriverManager; |
| 20 | +import java.sql.SQLException; |
| 21 | +import java.sql.Statement; |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | + |
| 26 | +public class WorkflowMgmtTest { |
| 27 | + |
| 28 | + Logger logger = LoggerFactory.getLogger(WorkflowMgmtTest.class); |
| 29 | + |
| 30 | + private static DBOSConfig dbosConfig; |
| 31 | + private static DataSource dataSource ; |
| 32 | + private DBOS dbos ; |
| 33 | + private static SystemDatabase systemDatabase ; |
| 34 | + private DBOSExecutor dbosExecutor; |
| 35 | + |
| 36 | + @BeforeAll |
| 37 | + static void onetimeSetup() throws Exception { |
| 38 | + |
| 39 | + WorkflowMgmtTest.dbosConfig = new DBOSConfig |
| 40 | + .Builder() |
| 41 | + .name("systemdbtest") |
| 42 | + .dbHost("localhost") |
| 43 | + .dbPort(5432) |
| 44 | + .dbUser("postgres") |
| 45 | + .sysDbName("dbos_java_sys") |
| 46 | + .maximumPoolSize(2) |
| 47 | + .build(); |
| 48 | + |
| 49 | + String dbUrl = String.format("jdbc:postgresql://%s:%d/%s", dbosConfig.getDbHost(), dbosConfig.getDbPort(), "postgres"); |
| 50 | + |
| 51 | + String sysDb = dbosConfig.getSysDbName(); |
| 52 | + try (Connection conn = DriverManager.getConnection(dbUrl, dbosConfig.getDbUser(), dbosConfig.getDbPassword()); |
| 53 | + Statement stmt = conn.createStatement()) { |
| 54 | + |
| 55 | + |
| 56 | + String dropDbSql = String.format("DROP DATABASE IF EXISTS %s", sysDb); |
| 57 | + String createDbSql = String.format("CREATE DATABASE %s", sysDb); |
| 58 | + stmt.execute(dropDbSql); |
| 59 | + stmt.execute(createDbSql); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + @BeforeEach |
| 65 | + void beforeEachTest() throws SQLException { |
| 66 | + WorkflowMgmtTest.dataSource = DBUtils.createDataSource(dbosConfig) ; |
| 67 | + DBOS.initialize(dbosConfig); |
| 68 | + dbos = DBOS.getInstance(); |
| 69 | + SystemDatabase.initialize(dataSource); |
| 70 | + systemDatabase = SystemDatabase.getInstance(); |
| 71 | + dbosExecutor = new DBOSExecutor(dbosConfig, systemDatabase); |
| 72 | + dbos.setDbosExecutor(dbosExecutor); |
| 73 | + dbos.launch(); |
| 74 | + DBUtils.clearTables(dataSource); |
| 75 | + } |
| 76 | + |
| 77 | + @AfterEach |
| 78 | + void afterEachTest() throws SQLException { |
| 79 | + dbos.shutdown(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void asyncCancelResumeTest() throws Exception { |
| 84 | + |
| 85 | + CountDownLatch mainLatch = new CountDownLatch(1); |
| 86 | + CountDownLatch workLatch = new CountDownLatch(1); |
| 87 | + |
| 88 | + MgmtService mgmtService = dbos.<MgmtService>Workflow() |
| 89 | + .interfaceClass(MgmtService.class) |
| 90 | + .implementation(new MgmtServiceImpl(mainLatch, workLatch)) |
| 91 | + .build(); |
| 92 | + mgmtService.setMgmtService(mgmtService); |
| 93 | + |
| 94 | + String workflowId = "wfid1" ; |
| 95 | + DBOSOptions options = new DBOSOptions.Builder(workflowId).async().build(); |
| 96 | + int result ; |
| 97 | + try (SetDBOSOptions o = new SetDBOSOptions(options)) { |
| 98 | + mgmtService.simpleWorkflow(23); |
| 99 | + } |
| 100 | + |
| 101 | + mainLatch.await(); |
| 102 | + dbos.cancelWorkflow(workflowId); |
| 103 | + workLatch.countDown(); |
| 104 | + |
| 105 | + assertEquals(1, mgmtService.getStepsExecuted()) ; |
| 106 | + |
| 107 | + WorkflowHandle<?> handle = dbos.resumeWorkflow(workflowId) ; |
| 108 | + |
| 109 | + result = (Integer) handle.getResult() ; |
| 110 | + assertEquals(23, result); |
| 111 | + assertEquals(3, mgmtService.getStepsExecuted()) ; |
| 112 | + |
| 113 | + logger.info("Test completed"); |
| 114 | + |
| 115 | + } |
| 116 | +} |
0 commit comments