Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions transact/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies {
testImplementation("io.rest-assured:json-path:6.0.0")
testImplementation("io.rest-assured:xml-path:6.0.0")
testImplementation("org.apache.maven:maven-artifact:3.9.12")
testImplementation("org.testcontainers:testcontainers-postgresql:2.0.3")
}

val projectVersion = project.version.toString()
Expand Down
55 changes: 55 additions & 0 deletions transact/src/test/java/dev/dbos/transact/DbSetupTestBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package dev.dbos.transact;

import dev.dbos.transact.config.DBOSConfig;
import dev.dbos.transact.database.SystemDatabase;

import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.postgresql.PostgreSQLContainer;

public class DbSetupTestBase {

protected static final PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:18");

protected static DBOSConfig dbosConfig;
protected static HikariDataSource dataSource;

@BeforeAll
protected static void onetimeSetup() throws Exception {
postgres.start();
dbosConfig =
DBOSConfig.defaults("systemdbtest")
.withDatabaseUrl(postgres.getJdbcUrl())
.withDbUser(postgres.getUsername())
.withDbPassword(postgres.getPassword());
dataSource = SystemDatabase.createDataSource(dbosConfig);
}

@AfterAll
protected static void afterAll() throws Exception {
postgres.stop();
}

protected DBOSClient getDBOSClient() {
return new DBOSClient(postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword());
}

protected static DBOSConfig createConfig(String appName) {
return DBOSConfig.defaults(appName)
.withDatabaseUrl(postgres.getJdbcUrl())
.withDbUser(postgres.getUsername())
.withDbPassword(postgres.getPassword());
}

protected static DBOSConfig createConfigFromEnv(String appName) {
return DBOSConfig.defaultsFromEnv(appName)
.withDatabaseUrl(postgres.getJdbcUrl())
.withDbUser(postgres.getUsername())
.withDbPassword(postgres.getPassword());
}

protected static String getJdbcUrl(String databaseName) {
return postgres.getJdbcUrl().replaceFirst("/[^/?]+(\\?.*)?$", "/" + databaseName + "$1");
}
}
32 changes: 8 additions & 24 deletions transact/src/test/java/dev/dbos/transact/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import dev.dbos.transact.DBOS;
import dev.dbos.transact.DBOSClient;
import dev.dbos.transact.DBOSTestAccess;
import dev.dbos.transact.config.DBOSConfig;
import dev.dbos.transact.DbSetupTestBase;
import dev.dbos.transact.database.SystemDatabase;
import dev.dbos.transact.exceptions.DBOSAwaitedWorkflowCancelledException;
import dev.dbos.transact.exceptions.DBOSNonExistentWorkflowException;
Expand All @@ -24,24 +24,10 @@
import org.junit.jupiter.api.*;

@org.junit.jupiter.api.Timeout(value = 2, unit = java.util.concurrent.TimeUnit.MINUTES)
public class ClientTest {
private static DBOSConfig dbosConfig;
private static final String dbUrl = "jdbc:postgresql://localhost:5432/dbos_java_sys";
private static final String dbUser = "postgres";
private static final String dbPassword = System.getenv("PGPASSWORD");

public class ClientTest extends DbSetupTestBase {
private ClientService service;
private HikariDataSource dataSource;

@BeforeAll
static void onetimeSetup() throws Exception {
dbosConfig =
DBOSConfig.defaults("systemdbtest")
.withDatabaseUrl(dbUrl)
.withDbUser(dbUser)
.withDbPassword(dbPassword);
}

@BeforeEach
void beforeEachTest() throws SQLException {
DBUtils.recreateDB(dbosConfig);
Expand All @@ -50,9 +36,7 @@ void beforeEachTest() throws SQLException {
service = DBOS.registerWorkflows(ClientService.class, new ClientServiceImpl());
DBOS.launch();

dataSource =
SystemDatabase.createDataSource(
dbosConfig.databaseUrl(), dbosConfig.dbUser(), dbosConfig.dbPassword());
dataSource = SystemDatabase.createDataSource(dbosConfig);
}

@AfterEach
Expand Down Expand Up @@ -108,7 +92,7 @@ public void clientEnqueue() throws Exception {
var qs = DBOSTestAccess.getQueueService();
qs.pause();

try (var client = new DBOSClient(dbUrl, dbUser, dbPassword)) {
try (var client = getDBOSClient()) {
var options = new DBOSClient.EnqueueOptions("ClientServiceImpl", "enqueueTest", "testQueue");
var handle = client.enqueueWorkflow(options, new Object[] {42, "spam"});
var rows = DBUtils.getWorkflowRows(dataSource);
Expand All @@ -135,7 +119,7 @@ public void clientEnqueueDeDupe() throws Exception {
var qs = DBOSTestAccess.getQueueService();
qs.pause();

try (var client = new DBOSClient(dbUrl, dbUser, dbPassword)) {
try (var client = getDBOSClient()) {
var options =
new DBOSClient.EnqueueOptions("ClientServiceImpl", "enqueueTest", "testQueue")
.withDeduplicationId("plugh!");
Expand All @@ -154,7 +138,7 @@ public void clientSend() throws Exception {

var idempotencyKey = UUID.randomUUID().toString();

try (var client = new DBOSClient(dbUrl, dbUser, dbPassword)) {
try (var client = getDBOSClient()) {
client.send(handle.workflowId(), "test.message", "test-topic", idempotencyKey);
}

Expand All @@ -163,7 +147,7 @@ public void clientSend() throws Exception {

@Test
public void clientEnqueueTimeouts() throws Exception {
try (var client = new DBOSClient(dbUrl, dbUser, dbPassword)) {
try (var client = getDBOSClient()) {
var options = new DBOSClient.EnqueueOptions("ClientServiceImpl", "sleep", "testQueue");

var handle1 =
Expand Down Expand Up @@ -198,7 +182,7 @@ public void clientEnqueueTimeouts() throws Exception {
public void invalidSend() throws Exception {
var invalidWorkflowId = UUID.randomUUID().toString();

try (var client = new DBOSClient(dbUrl, dbUser, dbPassword)) {
try (var client = getDBOSClient()) {
var ex =
assertThrows(
DBOSNonExistentWorkflowException.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import dev.dbos.transact.DBOS;
import dev.dbos.transact.DBOSTestAccess;
import dev.dbos.transact.config.DBOSConfig;
import dev.dbos.transact.DbSetupTestBase;
import dev.dbos.transact.database.SystemDatabase;
import dev.dbos.transact.exceptions.DBOSAwaitedWorkflowCancelledException;
import dev.dbos.transact.execution.ThrowingRunnable;
Expand All @@ -25,31 +25,16 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.postgresql.util.PSQLException;

@org.junit.jupiter.api.Timeout(value = 2, unit = java.util.concurrent.TimeUnit.MINUTES)
public class PgSqlClientTest {
private static DBOSConfig dbosConfig;
private static final String dbUrl = "jdbc:postgresql://localhost:5432/dbos_java_sys";
private static final String dbUser = "postgres";
private static final String dbPassword = System.getenv("PGPASSWORD");

public class PgSqlClientTest extends DbSetupTestBase {
private ClientService service;

private HikariDataSource dataSource;

@BeforeAll
static void onetimeSetup() throws Exception {
dbosConfig =
DBOSConfig.defaults("systemdbtest")
.withDatabaseUrl(dbUrl)
.withDbUser(dbUser)
.withDbPassword(dbPassword);
}

@BeforeEach
void beforeEachTest() throws SQLException {
DBUtils.recreateDB(dbosConfig);
Expand All @@ -58,9 +43,7 @@ void beforeEachTest() throws SQLException {
service = DBOS.registerWorkflows(ClientService.class, new ClientServiceImpl());
DBOS.launch();

dataSource =
SystemDatabase.createDataSource(
dbosConfig.databaseUrl(), dbosConfig.dbUser(), dbosConfig.dbPassword());
dataSource = SystemDatabase.createDataSource(dbosConfig);
}

@AfterEach
Expand Down
62 changes: 19 additions & 43 deletions transact/src/test/java/dev/dbos/transact/config/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import dev.dbos.transact.Constants;
import dev.dbos.transact.DBOS;
import dev.dbos.transact.DBOSTestAccess;
import dev.dbos.transact.DbSetupTestBase;
import dev.dbos.transact.StartWorkflowOptions;
import dev.dbos.transact.database.DBTestAccess;
import dev.dbos.transact.internal.AppVersionComputer;
Expand Down Expand Up @@ -38,7 +38,7 @@

@org.junit.jupiter.api.Timeout(value = 2, unit = java.util.concurrent.TimeUnit.MINUTES)
@ExtendWith(SystemStubsExtension.class)
public class ConfigTest {
public class ConfigTest extends DbSetupTestBase {

@SystemStub private EnvironmentVariables envVars = new EnvironmentVariables();

Expand All @@ -49,10 +49,7 @@ public void configOverridesEnvAppVerAndExecutor() throws Exception {
envVars.set("DBOS__APPID", "test-env-app-id");

var config =
DBOSConfig.defaults("config-test")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys")
.withDbUser("postgres")
.withDbPassword(System.getenv("PGPASSWORD"))
createConfig("config-test")
.withAppVersion("test-app-version")
.withExecutorId("test-executor-id");

Expand All @@ -75,11 +72,7 @@ public void envAppVerAndExecutor() throws Exception {
envVars.set("DBOS__APPVERSION", "test-env-app-version");
envVars.set("DBOS__APPID", "test-env-app-id");

var config =
DBOSConfig.defaults("config-test")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys")
.withDbUser("postgres")
.withDbPassword(System.getenv("PGPASSWORD"));
var config = createConfig("config-test");

DBOSTestAccess.reinitialize(config);
try {
Expand All @@ -102,10 +95,7 @@ public void dbosCloudEnvOverridesConfigAppVerAndExecutor() throws Exception {
envVars.set("DBOS__APPID", "test-env-app-id");

var config =
DBOSConfig.defaults("config-test")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys")
.withDbUser("postgres")
.withDbPassword(System.getenv("PGPASSWORD"))
createConfig("config-test")
.withAppVersion("test-app-version")
.withExecutorId("test-executor-id");

Expand All @@ -123,11 +113,7 @@ public void dbosCloudEnvOverridesConfigAppVerAndExecutor() throws Exception {

@Test
public void localExecutorId() throws Exception {
var config =
DBOSConfig.defaults("config-test")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys")
.withDbUser("postgres")
.withDbPassword(System.getenv("PGPASSWORD"));
var config = createConfig("config-test");

DBOSTestAccess.reinitialize(config);
try {
Expand All @@ -142,10 +128,7 @@ public void localExecutorId() throws Exception {

@Test
public void conductorExecutorId() throws Exception {
var config =
DBOSConfig.defaultsFromEnv("config-test")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys")
.withConductorKey("test-conductor-key");
var config = createConfigFromEnv("config-test").withConductorKey("test-conductor-key");

DBOSTestAccess.reinitialize(config);
try {
Expand All @@ -162,8 +145,7 @@ public void conductorExecutorId() throws Exception {
@Test
public void cantSetExecutorIdWhenUsingConductor() throws Exception {
var config =
DBOSConfig.defaultsFromEnv("config-test")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys")
createConfigFromEnv("config-test")
.withConductorKey("test-conductor-key")
.withExecutorId("test-executor-id");

Expand Down Expand Up @@ -196,11 +178,7 @@ public void cantSetEmptyConfigFields() throws Exception {

@Test
public void calcAppVersion() throws Exception {
var config =
DBOSConfig.defaults("config-test")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys")
.withDbUser("postgres")
.withDbPassword(System.getenv(Constants.POSTGRES_PASSWORD_ENV_VAR));
var config = createConfig("config-test");

DBOSTestAccess.reinitialize(config);
try {
Expand All @@ -219,17 +197,17 @@ public void calcAppVersion() throws Exception {

@Test
public void configPGSimpleDataSource() throws Exception {
var url = "jdbc:postgresql://localhost:5432/dbos_java_sys";
var user = "postgres";
var password = System.getenv(Constants.POSTGRES_PASSWORD_ENV_VAR);
var url = dbosConfig.databaseUrl();
var user = dbosConfig.dbUser();
var password = dbosConfig.dbPassword();
DBUtils.recreateDB(url, user, password);

PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setServerNames(new String[] {"localhost"});
ds.setDatabaseName("dbos_java_sys");
ds.setServerNames(new String[] {postgres.getHost()});
ds.setDatabaseName(postgres.getDatabaseName());
ds.setUser(user);
ds.setPassword(password);
ds.setPortNumbers(new int[] {5432});
ds.setPortNumbers(new int[] {postgres.getFirstMappedPort()});

var config =
DBOSConfig.defaults("config-test")
Expand Down Expand Up @@ -263,9 +241,9 @@ public void configPGSimpleDataSource() throws Exception {
public void configHikariDataSource() throws Exception {

var poolName = "dbos-configDataSource";
var url = "jdbc:postgresql://localhost:5432/dbos_java_sys";
var user = "postgres";
var password = System.getenv(Constants.POSTGRES_PASSWORD_ENV_VAR);
var url = dbosConfig.databaseUrl();
var user = dbosConfig.dbUser();
var password = dbosConfig.dbPassword();

DBUtils.recreateDB(url, user, password);

Expand Down Expand Up @@ -330,9 +308,7 @@ public void dbosVersion() throws Exception {
public void appVersion() throws Exception {
try {
envVars.set("DBOS__APPID", "test-env-app-id");
var dbosConfig =
DBOSConfig.defaultsFromEnv("systemdbtest")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys");
var dbosConfig = createConfigFromEnv("systemdbtest");
DBUtils.recreateDB(dbosConfig);
DBOSTestAccess.reinitialize(dbosConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import dev.dbos.transact.config.DBOSConfig;
import dev.dbos.transact.DbSetupTestBase;
import dev.dbos.transact.migrations.MigrationManager;
import dev.dbos.transact.utils.DBUtils;

Expand All @@ -16,17 +16,11 @@
import org.junit.jupiter.api.Test;

@org.junit.jupiter.api.Timeout(value = 2, unit = java.util.concurrent.TimeUnit.MINUTES)
public class ExternalStateTest {
public class ExternalStateTest extends DbSetupTestBase {
private static SystemDatabase systemDatabase;
private static DBOSConfig dbosConfig;

@BeforeAll
static void onetimeSetup() throws Exception {

dbosConfig =
DBOSConfig.defaultsFromEnv("systemdbtest")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys");

protected static void onetimeSetup() throws Exception {
DBUtils.recreateDB(dbosConfig);
MigrationManager.runMigrations(dbosConfig);
systemDatabase = SystemDatabase.create(dbosConfig);
Expand Down
Loading