-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add testing that things seem to generally work OK with virtual thread…
…s. Not exhaustive.
- Loading branch information
1 parent
386a751
commit a8f49f0
Showing
11 changed files
with
435 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
transactionoutbox-testing/src/main/java/com/gruelbox/transactionoutbox/testing/BaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.gruelbox.transactionoutbox.testing; | ||
|
||
import com.gruelbox.transactionoutbox.*; | ||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import java.sql.SQLException; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
import lombok.experimental.Accessors; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
@Slf4j | ||
public abstract class BaseTest { | ||
|
||
protected HikariDataSource dataSource; | ||
|
||
@BeforeEach | ||
final void baseBeforeEach() { | ||
HikariConfig config = new HikariConfig(); | ||
config.setJdbcUrl(connectionDetails().url()); | ||
config.setUsername(connectionDetails().user()); | ||
config.setPassword(connectionDetails().password()); | ||
config.addDataSourceProperty("cachePrepStmts", "true"); | ||
dataSource = new HikariDataSource(config); | ||
} | ||
|
||
@AfterEach | ||
final void baseAfterEach() { | ||
dataSource.close(); | ||
} | ||
|
||
protected ConnectionDetails connectionDetails() { | ||
return ConnectionDetails.builder() | ||
.dialect(Dialect.H2) | ||
.driverClassName("org.h2.Driver") | ||
.url( | ||
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DEFAULT_LOCK_TIMEOUT=60000;LOB_TIMEOUT=2000;MV_STORE=TRUE;DATABASE_TO_UPPER=FALSE") | ||
.user("test") | ||
.password("test") | ||
.build(); | ||
} | ||
|
||
protected TransactionManager txManager() { | ||
return TransactionManager.fromDataSource(dataSource); | ||
} | ||
|
||
protected Persistor persistor() { | ||
return Persistor.forDialect(connectionDetails().dialect()); | ||
} | ||
|
||
protected void clearOutbox() { | ||
DefaultPersistor persistor = Persistor.forDialect(connectionDetails().dialect()); | ||
TransactionManager transactionManager = txManager(); | ||
transactionManager.inTransaction( | ||
tx -> { | ||
try { | ||
persistor.clear(tx); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
protected void withRunningFlusher(TransactionOutbox outbox, ThrowingRunnable runnable) | ||
throws Exception { | ||
Thread backgroundThread = | ||
new Thread( | ||
() -> { | ||
while (!Thread.interrupted()) { | ||
try { | ||
// Keep flushing work until there's nothing left to flush | ||
//noinspection StatementWithEmptyBody | ||
while (outbox.flush()) {} | ||
} catch (Exception e) { | ||
log.error("Error flushing transaction outbox. Pausing", e); | ||
} | ||
try { | ||
//noinspection BusyWait | ||
Thread.sleep(250); | ||
} catch (InterruptedException e) { | ||
break; | ||
} | ||
} | ||
}); | ||
backgroundThread.start(); | ||
try { | ||
runnable.run(); | ||
} finally { | ||
backgroundThread.interrupt(); | ||
backgroundThread.join(); | ||
} | ||
} | ||
|
||
@Value | ||
@Accessors(fluent = true) | ||
@Builder | ||
public static class ConnectionDetails { | ||
String driverClassName; | ||
String url; | ||
String user; | ||
String password; | ||
Dialect dialect; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>transactionoutbox-parent</artifactId> | ||
<groupId>com.gruelbox</groupId> | ||
<version>${revision}</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<name>Transaction Outbox Virtual Threads support</name> | ||
<packaging>jar</packaging> | ||
<artifactId>transactionoutbox-virtthreads</artifactId> | ||
<description>A safe implementation of the transactional outbox pattern for Java (core library)</description> | ||
<properties> | ||
<maven.compiler.source>21</maven.compiler.source> | ||
<maven.compiler.target>21</maven.compiler.target> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.gruelbox</groupId> | ||
<artifactId>transactionoutbox-core</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- Compile time --> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>com.gruelbox</groupId> | ||
<artifactId>transactionoutbox-testing</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.gruelbox</groupId> | ||
<artifactId>transactionoutbox-jooq</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>testcontainers</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>postgresql</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>oracle-xe</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>mysql</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.oracle.database.jdbc</groupId> | ||
<artifactId>ojdbc11</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Oops, something went wrong.