-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: workflows sample with a basic deduplication (#9)
* docs: workflows sample with a basic deduplication * fixing logger * commandIds size limit
- Loading branch information
Showing
11 changed files
with
229 additions
and
81 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
66 changes: 66 additions & 0 deletions
66
...-compensation/src/it/java/com/example/wallet/application/WalletEntityIntegrationTest.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,66 @@ | ||
package com.example.wallet.application; | ||
|
||
import akka.javasdk.testkit.TestKitSupport; | ||
import com.example.wallet.application.WalletEntity.WalletResult; | ||
import com.example.wallet.domain.WalletCommand; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.example.transfer.TransferWorkflowIntegrationTest.randomId; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class WalletEntityIntegrationTest extends TestKitSupport { | ||
|
||
@Test | ||
public void shouldDeduplicateWithdrawCommand() { | ||
// given | ||
var walletId = randomId(); | ||
var withdraw = new WalletCommand.Withdraw(randomId(), 10); | ||
await(componentClient.forEventSourcedEntity(walletId) | ||
.method(WalletEntity::create) | ||
.invokeAsync(100)); | ||
|
||
// when | ||
withdraw(walletId, withdraw); | ||
withdraw(walletId, withdraw); | ||
withdraw(walletId, withdraw); | ||
|
||
// then | ||
Integer balance = await(componentClient.forEventSourcedEntity(walletId) | ||
.method(WalletEntity::get) | ||
.invokeAsync()); | ||
assertThat(balance).isEqualTo(100 - 10); | ||
} | ||
|
||
@Test | ||
public void shouldDeduplicateDepositCommand() { | ||
// given | ||
var walletId = randomId(); | ||
var deposit = new WalletCommand.Deposit(randomId(), 10); | ||
await(componentClient.forEventSourcedEntity(walletId) | ||
.method(WalletEntity::create) | ||
.invokeAsync(100)); | ||
|
||
// when | ||
deposit(walletId, deposit); | ||
deposit(walletId, deposit); | ||
deposit(walletId, deposit); | ||
|
||
// then | ||
Integer balance = await(componentClient.forEventSourcedEntity(walletId) | ||
.method(WalletEntity::get) | ||
.invokeAsync()); | ||
assertThat(balance).isEqualTo(100 + 10); | ||
} | ||
|
||
private WalletResult deposit(String walletId, WalletCommand.Deposit deposit) { | ||
return await(componentClient.forEventSourcedEntity(walletId) | ||
.method(WalletEntity::deposit) | ||
.invokeAsync(deposit)); | ||
} | ||
|
||
private WalletResult withdraw(String walletId, WalletCommand.Withdraw withdraw) { | ||
return await(componentClient.forEventSourcedEntity(walletId) | ||
.method(WalletEntity::withdraw) | ||
.invokeAsync(withdraw)); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
samples/transfer-workflow-compensation/src/it/java/com/example/wallet/domain/WalletTest.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,27 @@ | ||
package com.example.wallet.domain; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class WalletTest { | ||
|
||
@Test | ||
public void shouldLimitCommandIdsSize() { | ||
//given | ||
Wallet wallet = new Wallet("w1", 100, new ArrayList<>()); | ||
|
||
//when | ||
for (int i = 0; i < 10000; i++) { | ||
List<WalletEvent> events = wallet.handle(new WalletCommand.Deposit(UUID.randomUUID().toString(), 10)); | ||
wallet = wallet.applyEvent(events.get(0)); | ||
} | ||
|
||
//then | ||
assertThat(wallet.commandIds()).hasSize(1000); | ||
} | ||
} |
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
Oops, something went wrong.