-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rodolfo
committed
Dec 4, 2024
1 parent
363b777
commit 3e5a2c4
Showing
13 changed files
with
226 additions
and
60 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
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
File renamed without changes.
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,61 @@ | ||
package crablet.example | ||
|
||
import crablet.AppendCondition | ||
import crablet.DomainIdentifier | ||
import crablet.EventName | ||
import crablet.SequenceNumber | ||
import crablet.StateId | ||
import crablet.StateName | ||
import crablet.StreamQuery | ||
import crablet.postgres.CrabletEventsAppender | ||
import crablet.postgres.CrabletStateBuilder | ||
import io.vertx.core.json.JsonArray | ||
import io.vertx.core.json.JsonObject | ||
import java.util.* | ||
|
||
fun main() { | ||
|
||
println("Vertx Pool started") | ||
|
||
val eventsAppender = CrabletEventsAppender(pool) | ||
|
||
val stateBuilder = CrabletStateBuilder( | ||
client = pool, | ||
initialState = JsonArray(), | ||
evolveFunction = { state, event -> state.add(event) }) | ||
|
||
val domainIdentifiers = listOf( | ||
DomainIdentifier(name = StateName("Account"), id = StateId(UUID.randomUUID().toString())) | ||
) | ||
|
||
val streamQuery = StreamQuery( | ||
identifiers = domainIdentifiers, | ||
eventTypes = listOf("AccountOpened", "AmountDeposited").map { EventName(it) } | ||
) | ||
|
||
val appendCondition = AppendCondition(query = streamQuery, maximumEventSequence = SequenceNumber(0)) | ||
|
||
val eventsToAppend: List<JsonObject> = listOf( | ||
JsonObject().put("type", "AccountOpened").put("id", 10), | ||
JsonObject().put("type", "AmountDeposited").put("amount", 100) | ||
) | ||
|
||
println("Append operation") | ||
println("--> eventsToAppend: $eventsToAppend") | ||
println("--> appendCondition: $appendCondition ") | ||
|
||
// append events | ||
eventsAppender.appendIf(eventsToAppend, appendCondition) | ||
.compose { | ||
// print the resulting sequenceId | ||
println() | ||
println("New sequence id ---> $it") | ||
// now project a state given the past events | ||
stateBuilder.buildFor(streamQuery) | ||
} | ||
.onSuccess { stateResult: Pair<JsonArray, SequenceNumber> -> | ||
println("New state ---> ${stateResult.first}") | ||
} | ||
.onFailure { it.printStackTrace() } | ||
|
||
} |
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,53 @@ | ||
package crablet.postgres | ||
|
||
import io.vertx.pgclient.PgConnectOptions | ||
import io.vertx.sqlclient.Pool | ||
import io.vertx.sqlclient.PoolOptions | ||
import org.testcontainers.containers.PostgreSQLContainer | ||
import org.testcontainers.utility.MountableFile | ||
|
||
abstract class AbstractCrabletTest { | ||
|
||
companion object { | ||
|
||
val PG_DOCKER_IMAGE = "postgres:latest" | ||
val DB_NAME = "postgres" | ||
val DB_USERNAME = "postgres" | ||
val DB_PASSWORD = "postgres" | ||
|
||
val postgresqlContainer = | ||
PostgreSQLContainer<Nothing>(PG_DOCKER_IMAGE) | ||
.apply { | ||
withDatabaseName(DB_NAME) | ||
withUsername(DB_USERNAME) | ||
withPassword(DB_PASSWORD) | ||
withCopyFileToContainer( | ||
MountableFile.forClasspathResource("./ddl/1-events_table.sql"), | ||
"/docker-entrypoint-initdb.d/1-events_table.sql", | ||
) | ||
withCopyFileToContainer( | ||
MountableFile.forClasspathResource("./ddl/2-append_events.sql"), | ||
"/docker-entrypoint-initdb.d/2-append_events.sql", | ||
) | ||
withEnv("POSTGRES_LOG_CONNECTIONS", "on") | ||
withEnv("POSTGRES_LOG_DISCONNECTIONS", "on") | ||
withEnv("POSTGRES_LOG_DURATION", "on") | ||
withEnv("POSTGRES_LOG_STATEMENT", "all") | ||
withEnv("POSTGRES_LOG_DIRECTORY", "/var/log/pg_log") | ||
withLogConsumer { frame -> println(frame.utf8String) } | ||
} | ||
|
||
val pool: Pool by lazy { | ||
postgresqlContainer.start() | ||
val connectOptions = PgConnectOptions() | ||
.setPort(if (postgresqlContainer.isRunning) postgresqlContainer.firstMappedPort else 5432) | ||
.setHost("127.0.0.1") | ||
.setDatabase(DB_NAME) | ||
.setUser(DB_USERNAME) | ||
.setPassword(DB_PASSWORD) | ||
val poolOptions = PoolOptions().setMaxSize(5) | ||
Pool.pool(connectOptions, poolOptions) | ||
} | ||
} | ||
|
||
} |
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,84 @@ | ||
package crablet.postgres | ||
|
||
import crablet.AppendCondition | ||
import crablet.DomainIdentifier | ||
import crablet.EventName | ||
import crablet.SequenceNumber | ||
import crablet.StateId | ||
import crablet.StateName | ||
import crablet.StreamQuery | ||
import io.vertx.core.json.JsonObject | ||
import io.vertx.junit5.VertxExtension | ||
import io.vertx.junit5.VertxTestContext | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertNotNull | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import java.util.* | ||
|
||
@ExtendWith(VertxExtension::class) | ||
class Scenario2TestAbstract : AbstractCrabletTest() { | ||
|
||
data class Account(val id: Int? = null, val balance: Int = 0) | ||
|
||
lateinit var eventsAppender: CrabletEventsAppender | ||
lateinit var stateBuilder: CrabletStateBuilder<Account> | ||
lateinit var appendCondition: AppendCondition | ||
lateinit var eventsToAppend: List<JsonObject> | ||
lateinit var streamQuery: StreamQuery | ||
|
||
@BeforeEach | ||
fun setUp(testContext: VertxTestContext) { | ||
eventsAppender = CrabletEventsAppender(pool) | ||
stateBuilder = CrabletStateBuilder( | ||
client = pool, | ||
initialState = Account(), | ||
evolveFunction = { state, event -> | ||
when (event.getString("type")) { | ||
"AccountOpened" -> state.copy(id = event.getInteger("id")) | ||
"AmountDeposited" -> state.copy(balance = state.balance.plus(event.getInteger("amount"))) | ||
else -> state | ||
} | ||
}) | ||
|
||
val domainIdentifiers = listOf( | ||
DomainIdentifier(name = StateName("Account"), id = StateId(UUID.randomUUID().toString())) | ||
) | ||
streamQuery = StreamQuery( | ||
identifiers = domainIdentifiers, | ||
eventTypes = listOf("AccountOpened", "AmountDeposited").map { EventName(it) } | ||
) | ||
appendCondition = AppendCondition(query = streamQuery, maximumEventSequence = SequenceNumber(0)) | ||
eventsToAppend = listOf( | ||
JsonObject().put("type", "AccountOpened").put("id", 10), | ||
JsonObject().put("type", "AmountDeposited").put("amount", 100) | ||
) | ||
testContext.completeNow() | ||
} | ||
|
||
@Test | ||
fun testAppendAndBuildState(testContext: VertxTestContext) { | ||
// Append events and build the state | ||
eventsAppender.appendIf(eventsToAppend, appendCondition) | ||
.compose { | ||
stateBuilder.buildFor(streamQuery) | ||
} | ||
.onSuccess { (state, sequence): Pair<Account, SequenceNumber> -> | ||
|
||
assertNotNull(state) | ||
assertNotNull(sequence) | ||
|
||
assertEquals(state.id, 10) | ||
assertEquals(state.balance, 100) | ||
|
||
// Complete the test context indicating the test passed | ||
testContext.completeNow() | ||
} | ||
.onFailure { it -> | ||
// Fail the test context indicating the test failed | ||
testContext.failNow(it) | ||
} | ||
} | ||
|
||
} |
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
File renamed without changes.