From dc616e0f340f28b320253bf9a70778a07b885ffd Mon Sep 17 00:00:00 2001 From: Kostya Date: Sun, 11 Oct 2026 17:15:39 +0300 Subject: server: add versioned sync endpoint for polling clients The spec has clients pull updates every half second or so, by fetching everything, only changes, or something in between. Take the middle road: the engine keeps a version that moves on every change, and GET /api/sync?since={version} answers {"changed": false} when nothing has happened, which is almost every poll. Otherwise it returns a snapshot of the event summaries, the user directory and the caller's account, with the version to send next time. A client looking at one event refetches its details from /api/events/{name} only when the version moves. The lock filter bumps the version after every state-changing request, still inside the write lock, so a reader can never pick up new data under an old version. CORS preflights now take the read lock, since they change nothing. A session that times out also bumps the version, so other users see that user go offline. The user directory rows move into a helper that /api/users and the snapshot share. --- .../test/java/market/guess/server/SyncTest.java | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 server/src/test/java/market/guess/server/SyncTest.java (limited to 'server/src/test/java/market') diff --git a/server/src/test/java/market/guess/server/SyncTest.java b/server/src/test/java/market/guess/server/SyncTest.java new file mode 100644 index 0000000..23c1c5f --- /dev/null +++ b/server/src/test/java/market/guess/server/SyncTest.java @@ -0,0 +1,83 @@ +package market.guess.server; + +import static market.guess.server.EventsTest.loggedIn; +import static market.guess.server.EventsTest.upload; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.file.Path; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class SyncTest { + @TempDir static Path base; + static TestServer server; + + @BeforeAll + static void start() throws Exception { + server = new TestServer(base); + } + + @AfterAll + static void stop() throws Exception { + server.close(); + } + + private static JsonObject sync(HttpClient client, long since) throws Exception { + var res = server.get(client, "/sync?since=" + since); + assertEquals(200, res.statusCode(), res.body()); + return JsonParser.parseString(res.body()).getAsJsonObject(); + } + + @Test + void pollsAreCheapUntilSomethingChanges() throws Exception { + var pia = loggedIn(server, "Pia"); + var first = sync(pia, -1); + assertTrue(first.get("changed").getAsBoolean()); + assertTrue(first.has("events") && first.has("users"), first.toString()); + assertEquals("Pia", first.getAsJsonObject("account").get("owner").getAsString()); + var version = first.get("version").getAsLong(); + + var idle = sync(pia, version); + assertFalse(idle.get("changed").getAsBoolean()); + assertFalse(idle.has("events"), "an unchanged poll should not carry data"); + + var quinn = loggedIn(server, "Quinn"); + server.post(quinn, "/user/deposit", "amount", "42"); + assertEquals(200, upload(server, quinn, "small.xml").statusCode()); + + var after = sync(pia, version); + assertTrue(after.get("changed").getAsBoolean()); + assertNotEquals(version, after.get("version").getAsLong()); + assertTrue(after.toString().contains("Mujtaba is Dead"), after.toString()); + assertTrue(after.toString().contains("\"balance\":\"42.00\""), after.toString()); + } + + @Test + void preflightsDoNotBumpTheVersion() throws Exception { + var client = loggedIn(server, "Ray"); + var version = sync(client, -1).get("version").getAsLong(); + client.send( + server + .request("/sync") + .method("OPTIONS", HttpRequest.BodyPublishers.noBody()) + .header("Origin", "http://localhost:5173") + .build(), + HttpResponse.BodyHandlers.discarding()); + assertFalse(sync(client, version).get("changed").getAsBoolean()); + } + + @Test + void requiresLogin() throws Exception { + assertEquals(401, server.get(TestServer.client(), "/sync?since=0").statusCode()); + } +} -- cgit v1.2.3