137 lines
8.8 KiB
Java
137 lines
8.8 KiB
Java
package games.dmg.fantasystick.persistence;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
import java.io.IOException;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigInteger;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.StandardCopyOption;
|
|
import java.time.Clock;
|
|
import java.util.concurrent.CompletionException;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.io.TempDir;
|
|
|
|
class SecondWindHealthPersistenceTest {
|
|
@TempDir Path data;
|
|
private static final BigDecimal LIMIT = BigDecimal.valueOf(1024);
|
|
|
|
@Test void provisionalTargetCannotLoadAfterRestartButAcknowledgedReconciliationCan() throws Exception {
|
|
var record = character(); var repository = new YamlCharacterRepository(data); repository.create(record);
|
|
try (var storage = new CharacterStorage(repository, Clock.systemUTC())) {
|
|
var pending = storage.prepareSecondWindHealth(record.id(), record.revision(), BigDecimal.valueOf(16), LIMIT)
|
|
.join().orElseThrow();
|
|
assertEquals(0, BigDecimal.valueOf(16).compareTo(storage.inspect(record.id()).join().orElseThrow().health()));
|
|
assertBlocked(storage, record);
|
|
try (var restarted = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC())) {
|
|
assertBlocked(restarted, record);
|
|
}
|
|
var settled = storage.reconcileSecondWindHealth(pending, BigDecimal.valueOf(5), LIMIT).join().orElseThrow();
|
|
assertEquals(record.cooldowns(), settled.cooldowns()); assertBlocked(storage, record);
|
|
assertTrue(storage.completeSecondWindHealth(pending, settled).join());
|
|
assertFalse(storage.completeSecondWindHealth(pending, settled).join(), "Duplicate acknowledgment is not new authority");
|
|
assertEquals(settled, storage.find(record.id()).join().orElseThrow());
|
|
var next = storage.prepareSecondWindHealth(record.id(), settled.revision(), BigDecimal.valueOf(11), LIMIT)
|
|
.join().orElseThrow();
|
|
assertFalse(storage.completeSecondWindHealth(pending, settled).join(), "Old acknowledgment cannot clear a new operation");
|
|
assertBlocked(storage, record);
|
|
assertTrue(storage.completeSecondWindHealth(next, next.saved()).join());
|
|
}
|
|
try (var restarted = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC())) {
|
|
assertEquals(0, BigDecimal.valueOf(11).compareTo(restarted.find(record.id()).join().orElseThrow().health()));
|
|
}
|
|
}
|
|
|
|
@Test void newerWriteIsPreservedAndNeitherReconciliationNorCleanupCanClearItsUncertainProof() throws Exception {
|
|
var record = character(); var repository = new YamlCharacterRepository(data); repository.create(record);
|
|
try (var storage = new CharacterStorage(repository, Clock.systemUTC())) {
|
|
var pending = storage.prepareSecondWindHealth(record.id(), record.revision(), BigDecimal.valueOf(16), LIMIT)
|
|
.join().orElseThrow();
|
|
var newer = storage.saveHealth(record.id(), pending.saved().revision(), BigDecimal.valueOf(8), LIMIT)
|
|
.join().orElseThrow();
|
|
assertTrue(storage.reconcileSecondWindHealth(pending, BigDecimal.valueOf(5), LIMIT).join().isEmpty());
|
|
assertFalse(storage.completeSecondWindHealth(pending, pending.saved()).join());
|
|
assertEquals(newer, storage.inspect(record.id()).join().orElseThrow()); assertBlocked(storage, record);
|
|
}
|
|
try (var restarted = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC())) {
|
|
assertBlocked(restarted, record);
|
|
assertEquals(0, BigDecimal.valueOf(8).compareTo(restarted.inspect(record.id()).join().orElseThrow().health()));
|
|
}
|
|
}
|
|
|
|
@Test void failedReconciliationRetainsDurableBarrierAndDoesNotClaimAnAcknowledgment() throws Exception {
|
|
var fail = new AtomicBoolean(); var repository = repository(fail); var record = character(); repository.create(record);
|
|
try (var storage = new CharacterStorage(repository, Clock.systemUTC())) {
|
|
var pending = storage.prepareSecondWindHealth(record.id(), record.revision(), BigDecimal.valueOf(16), LIMIT)
|
|
.join().orElseThrow();
|
|
fail.set(true);
|
|
assertThrows(CompletionException.class, () -> storage.reconcileSecondWindHealth(pending, BigDecimal.valueOf(5), LIMIT).join());
|
|
assertBlocked(storage, record);
|
|
assertEquals(pending.saved(), storage.inspect(record.id()).join().orElseThrow());
|
|
}
|
|
try (var restarted = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC())) { assertBlocked(restarted, record); }
|
|
}
|
|
|
|
@Test void failedProvisionalWriteRetainsProofEvenWhenTheOldRecordIsStillVisible() throws Exception {
|
|
var fail = new AtomicBoolean(); var repository = repository(fail); var record = character(); repository.create(record);
|
|
try (var storage = new CharacterStorage(repository, Clock.systemUTC())) {
|
|
fail.set(true);
|
|
assertThrows(CompletionException.class, () -> storage.prepareSecondWindHealth(record.id(), record.revision(), BigDecimal.valueOf(16), LIMIT).join());
|
|
assertEquals(record, storage.inspect(record.id()).join().orElseThrow()); assertBlocked(storage, record);
|
|
}
|
|
try (var restarted = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC())) { assertBlocked(restarted, record); }
|
|
}
|
|
|
|
@Test void malformedOrReplacedProofCannotBeSilentlyDeletedByCleanup() throws Exception {
|
|
var record = character(); var repository = new YamlCharacterRepository(data); repository.create(record);
|
|
try (var storage = new CharacterStorage(repository, Clock.systemUTC())) {
|
|
var pending = storage.prepareSecondWindHealth(record.id(), record.revision(), BigDecimal.valueOf(16), LIMIT)
|
|
.join().orElseThrow();
|
|
Path proof = data.resolve("second-wind-health").resolve(record.id() + ".pending");
|
|
Files.writeString(proof, java.util.UUID.randomUUID().toString());
|
|
assertThrows(CompletionException.class, () -> storage.completeSecondWindHealth(pending, pending.saved()).join());
|
|
assertTrue(Files.exists(proof)); assertBlocked(storage, record);
|
|
Files.writeString(proof, "incomplete");
|
|
assertThrows(CompletionException.class, () -> storage.reconcileSecondWindHealth(pending, BigDecimal.valueOf(5), LIMIT).join());
|
|
assertTrue(Files.exists(proof)); assertBlocked(storage, record);
|
|
}
|
|
}
|
|
|
|
@Test void staleInitialRevisionCannotCreateProofOrChangeHealth() throws Exception {
|
|
var record = character(); var repository = new YamlCharacterRepository(data); repository.create(record);
|
|
try (var storage = new CharacterStorage(repository, Clock.systemUTC())) {
|
|
assertTrue(storage.prepareSecondWindHealth(record.id(), record.revision().add(BigInteger.ONE), BigDecimal.valueOf(16), LIMIT).join().isEmpty());
|
|
assertEquals(record, storage.find(record.id()).join().orElseThrow());
|
|
assertFalse(Files.exists(data.resolve("second-wind-health")));
|
|
}
|
|
}
|
|
|
|
private YamlCharacterRepository repository(AtomicBoolean fail) {
|
|
return new YamlCharacterRepository(data, (source, target) -> {
|
|
if (fail.get()) throw new IOException("Injected health replacement failure");
|
|
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
|
|
});
|
|
}
|
|
|
|
private static void assertBlocked(CharacterStorage storage, CharacterRecord record) {
|
|
var failure = assertThrows(CompletionException.class, () -> storage.find(record.id()).join());
|
|
assertInstanceOf(IOException.class, failure.getCause());
|
|
assertTrue(failure.getCause().getMessage().contains("Second Wind"));
|
|
}
|
|
|
|
private static CharacterRecord character() {
|
|
var base = new games.dmg.fantasystick.crafting.CharacterFactory().create(
|
|
games.dmg.fantasystick.progression.ProgressionRules.defaults());
|
|
var stats = new java.util.EnumMap<>(base.stats());
|
|
stats.put(games.dmg.fantasystick.progression.Stat.CON, BigInteger.valueOf(12));
|
|
stats.put(games.dmg.fantasystick.progression.Stat.WIS, BigInteger.valueOf(12));
|
|
return new CharacterRecord(base.id(), base.name(), base.revision(), stats, base.level(), base.experience(),
|
|
base.unspentPoints(), BigDecimal.TEN, java.util.Set.of("second_wind"),
|
|
java.util.List.of("second_wind", "", "", "", "", "", ""), base.passives(),
|
|
java.util.Map.of("second_wind", java.time.Instant.now().plusSeconds(15)), base.restrictedKills(), false,
|
|
null, null, base.coins(), base.coinsInitialized(), base.rewardReceipts(), base.pvpCoinEscrow(), base.gear(), base.sheetGeneration());
|
|
}
|
|
}
|