Test canonical claim revisions and durable cleanup replay
Some checks failed
DuneGroups Replacement Build / dunegroups (push) Successful in 51s
DuneRealms Plugin CI / plugins (push) Failing after 2m11s

This commit is contained in:
unimatrixgit 2026-12-02 13:20:14 +01:00
commit fb3a74b316
4 changed files with 90 additions and 13 deletions

View file

@ -51,15 +51,15 @@ class HomeRegistryTest {
assertThrows(HomeRegistry.Rejected.class, () -> parse(null, rows(row(A, "active", false, 0, 0, "[]", "").replace(W, "bad-world"))));
assertThrows(HomeRegistry.Rejected.class, () -> parse(null, rows(row(A, "active", false, 99_999_999, 0, "[]", ""))));
assertThrows(HomeRegistry.Rejected.class, () -> parse(null, rows(row(A, "active", false, 0, 0, "[{\"x\":\"a\",\"z\":1}]", ""))));
assertThrows(HomeRegistry.Rejected.class, () -> parse(null, rows(row(A, "active", false, 0, 0, "[]", ",\"claimRevision\":-3"))));
assertThrows(HomeRegistry.Rejected.class, () -> parse(null, rows(row(A, "active", false, 0, 0, "[]", ",\"revision\":-3"))));
assertThrows(HomeRegistry.Rejected.class, () -> parse(rows(row(A, "active", false, 0, 0, "[]", ""), row(A, "active", false, 50, 50, "[]", "")), null));
}
@Test void aStaleClaimRevisionIsRejectedAndKeepsTheOldProjection() {
var first = parse(null, rows(row(A, "active", false, 0, 0, "[]", ",\"claimRevision\":5")));
var first = parse(null, rows(row(A, "active", false, 0, 0, "[]", ",\"revision\":5")));
assertEquals(5, first.values().iterator().next().revision());
assertThrows(HomeRegistry.Rejected.class, () -> parse(null, rows(row(A, "active", false, 200, 200, "[]", ",\"claimRevision\":4")), null, List.of(), first));
var newer = parse(null, rows(row(A, "active", false, 200, 200, "[]", ",\"claimRevision\":6")), null, List.of(), first);
assertThrows(HomeRegistry.Rejected.class, () -> parse(null, rows(row(A, "active", false, 200, 200, "[]", ",\"revision\":4")), null, List.of(), first));
var newer = parse(null, rows(row(A, "active", false, 200, 200, "[]", ",\"revision\":6")), null, List.of(), first);
assertNull(newer.get(HomeRegistry.key(W, 0, 0)), "old home chunk is released");
assertNotNull(newer.get(HomeRegistry.key(W, 12, 12)));
}
@ -82,7 +82,7 @@ class HomeRegistryTest {
}
@Test void officesProjectAsProtectedAndStorageRoundTrips() {
var m = parse(null, null, rows(row(A, "active", false, 0, 0, "[]", ",\"claimRevision\":7")), List.of(), Map.of());
var m = parse(null, null, rows(row(A, "active", false, 0, 0, "[]", ",\"revision\":7")), List.of(), Map.of());
assertEquals("protected", m.values().iterator().next().kind());
assertEquals(m, HomeRegistry.fromMap(HomeRegistry.toMap(m)));
var r = new HomeRegistry();

View file

@ -0,0 +1,39 @@
package com.dunerealms.jarvis;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
/** Publishes a replayable Home Block receipt only after the journal write succeeds. */
final class DuneGroupsAcknowledgements {
@FunctionalInterface interface Writer {
void write(List<String> pending) throws IOException;
}
private List<String> pending = List.of();
List<String> pending() { return pending; }
void restore(Collection<String> receipts) {
pending = List.copyOf(new LinkedHashSet<>(receipts));
}
void append(Collection<String> receipts, Writer writer) throws IOException {
var next = new LinkedHashSet<>(pending);
next.addAll(receipts);
publish(List.copyOf(next), writer);
}
void remove(String receipt, Writer writer) throws IOException {
var next = new ArrayList<>(pending);
next.remove(receipt);
publish(List.copyOf(next), writer);
}
private void publish(List<String> next, Writer writer) throws IOException {
writer.write(next);
pending = next;
}
}

View file

@ -30,7 +30,7 @@ final class DuneGroupsKeyWorker implements NativeKeys {
private final FactionsAdapter legacyFactions;private final TownyAdapter legacyTowns;
private final boolean legacyFactionsPresent,legacyTownyPresent;
private final Set<String> ownedClans=new TreeSet<>(),ownedCompanies=new TreeSet<>(),ownedOffices=new TreeSet<>();
private final List<String> pendingAcks=new ArrayList<>();
private final DuneGroupsAcknowledgements acknowledgements=new DuneGroupsAcknowledgements();
private volatile OrganisationDirectory directory=OrganisationDirectory.EMPTY;
private volatile boolean closed,homeNpcPending;private volatile String status="Waiting for central organisation keys";
private String previousFailure="",appliedState="";
@ -68,7 +68,7 @@ final class DuneGroupsKeyWorker implements NativeKeys {
private void load()throws IOException {
var yml=YamlConfiguration.loadConfiguration(stateFile.toFile());
ownedClans.addAll(yml.getStringList("clans"));ownedCompanies.addAll(yml.getStringList("companies"));ownedOffices.addAll(yml.getStringList("offices"));
pendingAcks.addAll(yml.getStringList("pendingAcks"));
acknowledgements.restore(yml.getStringList("pendingAcks"));
if(Files.exists(directoryFile))try{storedDirectory=OrganisationDirectory.fromJson(JsonParser.parseString(Files.readString(directoryFile)).getAsJsonObject());}
catch(RuntimeException unreadable){plugin.getLogger().warning("Stored organisation directory is unreadable; waiting for the next central state.");}
}
@ -103,10 +103,9 @@ final class DuneGroupsKeyWorker implements NativeKeys {
}
private void deliverPending()throws Exception {
for(String text:List.copyOf(pendingAcks)){
for(String text:acknowledgements.pending()){
api.homeCleanup(JsonParser.parseString(text).getAsJsonObject());
var remaining=new ArrayList<>(pendingAcks);remaining.remove(text);
save(storedDirectory,remaining);pendingAcks.remove(text);
acknowledgements.remove(text, remaining->save(storedDirectory,remaining));
}
}
@ -154,11 +153,10 @@ final class DuneGroupsKeyWorker implements NativeKeys {
done.complete(count);}catch(Exception failure){done.completeExceptionally(failure);}});
int chunks=done.get(20,TimeUnit.SECONDS);
ownedClans.clear();ownedClans.addAll(nextClans);ownedCompanies.clear();ownedCompanies.addAll(nextCompanies);ownedOffices.clear();ownedOffices.addAll(nextOffices);
var stagedAcks=new ArrayList<>(pendingAcks);
var stagedAcks=new ArrayList<String>();
for(var op:cleanupReady){var ack=new JsonObject();ack.addProperty("server",server);ack.add("requestId",op.get("requestId"));ack.add("keyId",op.get("keyId"));stagedAcks.add(ack.toString());}
save(nextDirectory,stagedAcks);
acknowledgements.append(stagedAcks, pending->save(nextDirectory,pending));
storedDirectory=nextDirectory;directory=nextDirectory;
pendingAcks.clear();pendingAcks.addAll(stagedAcks);
var activated=new CompletableFuture<Void>();
Bukkit.getScheduler().runTask(plugin,()->{
try{if(closed)throw new IllegalStateException("Translator stopped");invoke(activate);activated.complete(null);}

View file

@ -0,0 +1,40 @@
package com.dunerealms.jarvis;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.junit.jupiter.api.Assertions.*;
class DuneGroupsAcknowledgementsTest {
@TempDir Path directory;
@Test void failedWriteNeverMakesNewReceiptAvailableToSendOrReplay() throws IOException {
var stored = directory.resolve("pending");
var journal = new DuneGroupsAcknowledgements();
journal.append(List.of("previous"), pending -> Files.write(stored, pending));
assertThrows(IOException.class, () -> journal.append(List.of("new"), pending -> {
throw new IOException("simulated journal failure");
}));
assertEquals(List.of("previous"), journal.pending());
var restarted = new DuneGroupsAcknowledgements();
restarted.restore(Files.readAllLines(stored));
assertEquals(List.of("previous"), restarted.pending());
restarted.append(List.of("new", "new"), pending -> Files.write(stored, pending));
assertEquals(List.of("previous", "new"), Files.readAllLines(stored));
}
@Test void failedRemovalKeepsSentReceiptAvailableForIdempotentReplay() throws IOException {
var journal = new DuneGroupsAcknowledgements();
journal.restore(List.of("receipt"));
assertThrows(IOException.class, () -> journal.remove("receipt", pending -> {
throw new IOException("simulated journal failure");
}));
assertEquals(List.of("receipt"), journal.pending());
journal.remove("receipt", pending -> assertEquals(List.of(), pending));
assertTrue(journal.pending().isEmpty());
}
}