-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transactional outbox example tests (#5)
- Loading branch information
Showing
7 changed files
with
195 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# postgres-cdc | ||
|
||
[![Actions Status](https://github.com/rieske/postgres-cdc/workflows/master/badge.svg)](https://github.com/rieske/postgres-cdc/actions) | ||
|
||
Java library that utilizes [PostgreSQL logical replication](https://www.postgresql.org/docs/current/logical-replication.html) | ||
feature to implement [Change Data Capture](https://en.wikipedia.org/wiki/Change_data_capture). | ||
|
||
Once logical replication is configured on the PostgreSQL server, this library can subscribe to changes | ||
in the specified tables. | ||
The change events are streamed in real time and can be relayed to message brokers | ||
as they occur, allowing to implement the [Transactional Outbox](https://microservices.io/patterns/data/transactional-outbox.html) | ||
pattern. | ||
|
||
## Prerequisites | ||
|
||
PostgreSQL version 13.12 or later. | ||
Note 13.12 is the earliest one that this library is tested against at the time of writing. | ||
In theory, it may work with PostgreSQL 9.5 and above. | ||
|
||
Logical replication must be [configured](https://www.postgresql.org/docs/current/logical-replication-config.html#LOGICAL-REPLICATION-CONFIG-PUBLISHER) | ||
on the PostgreSQL server. | ||
|
||
If you are using AWS Aurora, see [here](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Replication.Logical.html#AuroraPostgreSQL.Replication.Logical.Configure) | ||
for instructions to enable logical replication. | ||
|
||
## Usage | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.github.rieske.cdc; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.function.Consumer; | ||
|
||
final class TestConsumers { | ||
private TestConsumers() { | ||
} | ||
|
||
static Consumer<ByteBuffer> printing() { | ||
return msg -> { | ||
int offset = msg.arrayOffset(); | ||
byte[] source = msg.array(); | ||
int length = source.length - offset; | ||
System.out.println(new String(source, offset, length)); | ||
}; | ||
} | ||
|
||
static <T> GatheringConsumer<T> gathering() { | ||
return new GatheringConsumer<>(); | ||
} | ||
} | ||
|
||
class GatheringConsumer<T> implements Consumer<T> { | ||
final List<T> consumedMessages = new CopyOnWriteArrayList<>(); | ||
|
||
@Override | ||
public void accept(T message) { | ||
consumedMessages.add(message); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
lib/src/test/java/io/github/rieske/cdc/TransactionalOutboxTest.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,94 @@ | ||
package io.github.rieske.cdc; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.time.Duration; | ||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
class TransactionalOutboxTest { | ||
|
||
@RegisterExtension | ||
final DatabaseExtension database = new DatabaseExtension(); | ||
|
||
private final String replicationSlotName = "cdc_stream"; | ||
private final String testEntityOutboxTable = "test_entity_outbox"; | ||
private final String anotherOutboxTable = "another_outbox"; | ||
|
||
private final GatheringConsumer<DatabaseChange> gatheringConsumer = TestConsumers.gathering(); | ||
|
||
private final PostgresReplicationListener listener = new PostgresReplicationListener( | ||
database.jdbcUrl(), | ||
database.databaseUsername(), | ||
database.databasePassword(), | ||
replicationSlotName, | ||
Set.of("public." + testEntityOutboxTable), | ||
TestConsumers.printing().andThen(new JsonDeserializingConsumer(gatheringConsumer)) | ||
); | ||
|
||
@BeforeEach | ||
void setup() { | ||
listener.createReplicationSlot(); | ||
listener.start(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
listener.stop(); | ||
listener.dropReplicationSlot(); | ||
} | ||
|
||
@Test | ||
void capturesInsertEvent() throws SQLException { | ||
String eventPayload = "{\"foo\":\"bar\"}"; | ||
try (Connection connection = database.getDataSource().getConnection()) { | ||
insertIntoOutboxTable(connection, testEntityOutboxTable, eventPayload); | ||
} | ||
|
||
await().atMost(Duration.ofSeconds(2)).untilAsserted(() -> assertThat(gatheringConsumer.consumedMessages).hasSize(1)); | ||
|
||
DatabaseChange event = gatheringConsumer.consumedMessages.get(0); | ||
assertThat(event.action).isEqualTo(DatabaseChange.Action.INSERT); | ||
assertThat(event.schema).isEqualTo("public"); | ||
assertThat(event.table).isEqualTo("test_entity_outbox"); | ||
assertThat(event.columns.get("event_payload")).isEqualTo(eventPayload); | ||
} | ||
|
||
@Test | ||
void ignoresEventsFromAnotherTable() throws SQLException { | ||
try (Connection connection = database.getDataSource().getConnection()) { | ||
insertIntoOutboxTable(connection, anotherOutboxTable, "{}"); | ||
} | ||
String eventPayload = "{\"foo\":\"bar\"}"; | ||
try (Connection connection = database.getDataSource().getConnection()) { | ||
insertIntoOutboxTable(connection, testEntityOutboxTable, eventPayload); | ||
} | ||
|
||
await().atMost(Duration.ofSeconds(2)).untilAsserted(() -> assertThat(gatheringConsumer.consumedMessages).hasSize(1)); | ||
|
||
DatabaseChange event = gatheringConsumer.consumedMessages.get(0); | ||
assertThat(event.action).isEqualTo(DatabaseChange.Action.INSERT); | ||
assertThat(event.schema).isEqualTo("public"); | ||
assertThat(event.table).isEqualTo("test_entity_outbox"); | ||
assertThat(event.columns.get("event_payload")).isEqualTo(eventPayload); | ||
} | ||
|
||
private void insertIntoOutboxTable(Connection connection, String outboxTable, String eventPayload) { | ||
try (PreparedStatement statement = connection.prepareStatement( | ||
"INSERT INTO " + outboxTable + " (event_payload) VALUES(?::json)" | ||
)) { | ||
statement.setString(1, eventPayload); | ||
statement.executeUpdate(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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