-
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
And require to specify the tables to listen to explicitly.
- Loading branch information
Showing
6 changed files
with
168 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
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