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 @@ -43,6 +43,7 @@ dependencies {
testImplementation("io.rest-assured:rest-assured:5.4.0")
testImplementation("io.rest-assured:json-path:5.4.0")
testImplementation("io.rest-assured:xml-path:5.4.0")
testImplementation("org.testcontainers:postgresql:1.21.3")
}

tasks.test {
Expand Down
34 changes: 34 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,34 @@
package dev.dbos.transact;

import dev.dbos.transact.config.DBOSConfig;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.PostgreSQLContainer;

public class DbSetupTestBase {

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

protected static DBOSConfig dbosConfig;

@BeforeAll
static void onetimeSetup() {
postgres.start();
dbosConfig =
DBOSConfig.defaults("systemdbtest")
.withDatabaseUrl(postgres.getJdbcUrl())
.withDbUser(postgres.getUsername())
.withDbPassword(postgres.getPassword())
.withMaximumPoolSize(2);
}

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

protected DBOSClient getDBOSClient() {
return new DBOSClient(postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword());
}
}
27 changes: 6 additions & 21 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.utils.DBUtils;
Expand All @@ -24,25 +24,10 @@
import org.junit.jupiter.api.*;

@org.junit.jupiter.api.Timeout(value = 2, unit = 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)
.withMaximumPoolSize(2);
}

@BeforeEach
void beforeEachTest() throws SQLException {
DBUtils.recreateDB(dbosConfig);
Expand All @@ -66,7 +51,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(
"dev.dbos.transact.client.ClientServiceImpl", "enqueueTest", "testQueue");
Expand Down Expand Up @@ -95,7 +80,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(
"dev.dbos.transact.client.ClientServiceImpl", "enqueueTest", "testQueue")
Expand All @@ -115,7 +100,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 @@ -131,7 +116,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(
"dev.dbos.transact.client.ClientServiceImpl", "sleep", "testQueue");
Expand Down
43 changes: 10 additions & 33 deletions transact/src/test/java/dev/dbos/transact/config/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import dev.dbos.transact.Constants;
import dev.dbos.transact.DBOS;
import dev.dbos.transact.DBOSTestAccess;
import dev.dbos.transact.DbSetupTestBase;
import dev.dbos.transact.database.DBTestAccess;
import dev.dbos.transact.invocation.HawkService;
import dev.dbos.transact.invocation.HawkServiceImpl;
Expand All @@ -26,19 +26,13 @@
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

@ExtendWith(SystemStubsExtension.class)
public class ConfigTest {
public class ConfigTest extends DbSetupTestBase {

@SystemStub private EnvironmentVariables envVars = new EnvironmentVariables();

@Test
public void setExecutorAndAppVersionViaConfig() throws Exception {
var config =
DBOSConfig.defaults("config-test")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys")
.withDbUser("postgres")
.withDbPassword(System.getenv("PGPASSWORD"))
.withAppVersion("test-app-version")
.withExecutorId("test-executor-id");
var config = dbosConfig.withAppVersion("test-app-version").withExecutorId("test-executor-id");

DBOS.reinitialize(config);
try {
Expand All @@ -57,13 +51,7 @@ public void setExecutorAndAppVersionViaEnv() throws Exception {
envVars.set("DBOS__VMID", "test-env-executor-id");
envVars.set("DBOS__APPVERSION", "test-env-app-version");

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

DBOS.reinitialize(config);
try {
Expand All @@ -78,11 +66,7 @@ public void setExecutorAndAppVersionViaEnv() 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 = dbosConfig;

DBOS.reinitialize(config);
try {
Expand All @@ -96,10 +80,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 = dbosConfig.withConductorKey("test-conductor-key");

DBOS.reinitialize(config);
try {
Expand All @@ -114,11 +95,7 @@ public void conductorExecutorId() 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 = dbosConfig;

DBOS.reinitialize(config);
try {
Expand All @@ -136,9 +113,9 @@ public void calcAppVersion() throws Exception {
public void configDataSource() 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
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 @@ -17,17 +17,12 @@
import org.junit.jupiter.api.Test;

@org.junit.jupiter.api.Timeout(value = 2, unit = 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");

DBUtils.recreateDB(dbosConfig);
MigrationManager.runMigrations(dbosConfig);
systemDatabase = new SystemDatabase(dbosConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import dev.dbos.transact.DBOS;
import dev.dbos.transact.config.DBOSConfig;
import dev.dbos.transact.DbSetupTestBase;
import dev.dbos.transact.exceptions.DBOSMaxRecoveryAttemptsExceededException;
import dev.dbos.transact.exceptions.DBOSQueueDuplicatedException;
import dev.dbos.transact.migrations.MigrationManager;
Expand All @@ -20,30 +20,20 @@

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;

@org.junit.jupiter.api.Timeout(value = 2, unit = TimeUnit.MINUTES)
public class SystemDatabaseTest {
private static DBOSConfig config;
public class SystemDatabaseTest extends DbSetupTestBase {
private SystemDatabase sysdb;
private HikariDataSource dataSource;

@BeforeAll
static void onetimeSetup() throws Exception {
config =
DBOSConfig.defaultsFromEnv("systemdbtest")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys")
.withMaximumPoolSize(10);
}

@BeforeEach
void beforeEachTest() throws SQLException {
DBUtils.recreateDB(config);
MigrationManager.runMigrations(config);
sysdb = new SystemDatabase(config);
dataSource = SystemDatabase.createDataSource(config);
DBUtils.recreateDB(dbosConfig);
MigrationManager.runMigrations(dbosConfig);
sysdb = new SystemDatabase(dbosConfig);
dataSource = SystemDatabase.createDataSource(dbosConfig);
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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.context.WorkflowOptions;
import dev.dbos.transact.database.SystemDatabase;
import dev.dbos.transact.exceptions.DBOSNonExistentWorkflowException;
Expand All @@ -20,24 +20,14 @@
import javax.sql.DataSource;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@org.junit.jupiter.api.Timeout(value = 2, unit = TimeUnit.MINUTES)
class DBOSExecutorTest {
class DBOSExecutorTest extends DbSetupTestBase {

private static DBOSConfig dbosConfig;
private static DataSource dataSource;

@BeforeAll
public static void onetimeBefore() {
DBOSExecutorTest.dbosConfig =
DBOSConfig.defaultsFromEnv("systemdbtest")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys")
.withMaximumPoolSize(2);
}

@BeforeEach
void setUp() throws SQLException {
DBUtils.recreateDB(dbosConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,19 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import dev.dbos.transact.DBOS;
import dev.dbos.transact.config.DBOSConfig;
import dev.dbos.transact.DbSetupTestBase;
import dev.dbos.transact.utils.DBUtils;

import java.sql.SQLException;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class LifecycleTest {
private static DBOSConfig dbosConfig;
public class LifecycleTest extends DbSetupTestBase {
private static LifecycleTestWorkflowsImpl impl;
private static TestLifecycleService svc;

@BeforeAll
static void onetimeSetup() throws Exception {
dbosConfig =
DBOSConfig.defaultsFromEnv("lifecycletest")
.withDatabaseUrl("jdbc:postgresql://localhost:5432/dbos_java_sys")
.withMaximumPoolSize(2);
}

@BeforeEach
void beforeEachTest() throws SQLException {
DBUtils.recreateDB(dbosConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import dev.dbos.transact.DBOS;
import dev.dbos.transact.DBOSTestAccess;
import dev.dbos.transact.DbSetupTestBase;
import dev.dbos.transact.StartWorkflowOptions;
import dev.dbos.transact.config.DBOSConfig;
import dev.dbos.transact.context.WorkflowOptions;
import dev.dbos.transact.database.SystemDatabase;
import dev.dbos.transact.utils.DBUtils;
Expand All @@ -22,16 +22,14 @@
import javax.sql.DataSource;

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.slf4j.Logger;
import org.slf4j.LoggerFactory;

@org.junit.jupiter.api.Timeout(value = 2, unit = TimeUnit.MINUTES)
class RecoveryServiceTest {
class RecoveryServiceTest extends DbSetupTestBase {

private static DBOSConfig dbosConfig;
private static DataSource dataSource;
private Queue testQueue;
private SystemDatabase systemDatabase;
Expand All @@ -40,15 +38,6 @@ class RecoveryServiceTest {
private ExecutingService executingService;
private static final Logger logger = LoggerFactory.getLogger(RecoveryServiceTest.class);

@BeforeAll
public static void onetimeBefore() {

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

@BeforeEach
void setUp() throws SQLException {
DBUtils.recreateDB(dbosConfig);
Expand Down
Loading