aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java
diff options
context:
space:
mode:
authorKostya <mail@sartin.in>2026-10-11 17:15:39 +0300
committerKostya <mail@sartin.in>2026-10-11 17:16:05 +0300
commitdc616e0f340f28b320253bf9a70778a07b885ffd (patch)
treea0344816c99604f8d89b4eb3db31c6e98ae2db7f /server/src/test/java
parent241fd5f0a46aff89a3302d2753a01e217a562e79 (diff)
downloadguess-market-dc616e0f340f28b320253bf9a70778a07b885ffd.tar.gz
guess-market-dc616e0f340f28b320253bf9a70778a07b885ffd.tar.xz
guess-market-dc616e0f340f28b320253bf9a70778a07b885ffd.zip
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.
Diffstat (limited to 'server/src/test/java')
-rw-r--r--server/src/test/java/market/guess/server/SyncTest.java83
1 files changed, 83 insertions, 0 deletions
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 @@
1package market.guess.server;
2
3import static market.guess.server.EventsTest.loggedIn;
4import static market.guess.server.EventsTest.upload;
5import static org.junit.jupiter.api.Assertions.assertEquals;
6import static org.junit.jupiter.api.Assertions.assertFalse;
7import static org.junit.jupiter.api.Assertions.assertNotEquals;
8import static org.junit.jupiter.api.Assertions.assertTrue;
9
10import com.google.gson.JsonObject;
11import com.google.gson.JsonParser;
12import java.net.http.HttpClient;
13import java.net.http.HttpRequest;
14import java.net.http.HttpResponse;
15import java.nio.file.Path;
16import org.junit.jupiter.api.AfterAll;
17import org.junit.jupiter.api.BeforeAll;
18import org.junit.jupiter.api.Test;
19import org.junit.jupiter.api.io.TempDir;
20
21class SyncTest {
22 @TempDir static Path base;
23 static TestServer server;
24
25 @BeforeAll
26 static void start() throws Exception {
27 server = new TestServer(base);
28 }
29
30 @AfterAll
31 static void stop() throws Exception {
32 server.close();
33 }
34
35 private static JsonObject sync(HttpClient client, long since) throws Exception {
36 var res = server.get(client, "/sync?since=" + since);
37 assertEquals(200, res.statusCode(), res.body());
38 return JsonParser.parseString(res.body()).getAsJsonObject();
39 }
40
41 @Test
42 void pollsAreCheapUntilSomethingChanges() throws Exception {
43 var pia = loggedIn(server, "Pia");
44 var first = sync(pia, -1);
45 assertTrue(first.get("changed").getAsBoolean());
46 assertTrue(first.has("events") && first.has("users"), first.toString());
47 assertEquals("Pia", first.getAsJsonObject("account").get("owner").getAsString());
48 var version = first.get("version").getAsLong();
49
50 var idle = sync(pia, version);
51 assertFalse(idle.get("changed").getAsBoolean());
52 assertFalse(idle.has("events"), "an unchanged poll should not carry data");
53
54 var quinn = loggedIn(server, "Quinn");
55 server.post(quinn, "/user/deposit", "amount", "42");
56 assertEquals(200, upload(server, quinn, "small.xml").statusCode());
57
58 var after = sync(pia, version);
59 assertTrue(after.get("changed").getAsBoolean());
60 assertNotEquals(version, after.get("version").getAsLong());
61 assertTrue(after.toString().contains("Mujtaba is Dead"), after.toString());
62 assertTrue(after.toString().contains("\"balance\":\"42.00\""), after.toString());
63 }
64
65 @Test
66 void preflightsDoNotBumpTheVersion() throws Exception {
67 var client = loggedIn(server, "Ray");
68 var version = sync(client, -1).get("version").getAsLong();
69 client.send(
70 server
71 .request("/sync")
72 .method("OPTIONS", HttpRequest.BodyPublishers.noBody())
73 .header("Origin", "http://localhost:5173")
74 .build(),
75 HttpResponse.BodyHandlers.discarding());
76 assertFalse(sync(client, version).get("changed").getAsBoolean());
77 }
78
79 @Test
80 void requiresLogin() throws Exception {
81 assertEquals(401, server.get(TestServer.client(), "/sync?since=0").statusCode());
82 }
83}