Skip to content

Commit 4cfad82

Browse files
committed
test: add ResponseAwaiter unit tests
1 parent 233ddf5 commit 4cfad82

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package dev.objz.commandbridge.net;
2+
3+
import dev.objz.commandbridge.logging.Log;
4+
import dev.objz.commandbridge.net.proto.Envelope;
5+
import dev.objz.commandbridge.net.proto.MessageType;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.time.Duration;
10+
import java.util.UUID;
11+
import java.util.concurrent.CompletableFuture;
12+
import java.util.concurrent.ExecutionException;
13+
import java.util.concurrent.TimeUnit;
14+
import java.util.concurrent.TimeoutException;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertFalse;
18+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
class ResponseAwaiterTest {
23+
24+
@BeforeAll
25+
static void installLog() {
26+
try {
27+
Log.install(java.util.logging.Logger.getLogger("test"));
28+
} catch (IllegalStateException ignored) {
29+
// already installed
30+
}
31+
}
32+
33+
private static Envelope envelope(UUID id, String from) {
34+
return new Envelope(1, id, MessageType.PING, from, "server1",
35+
System.currentTimeMillis(), Envelope.MAPPER.nullNode());
36+
}
37+
38+
@Test
39+
void signalMatchesById() throws Exception {
40+
ResponseAwaiter awaiter = new ResponseAwaiter();
41+
UUID testId = UUID.randomUUID();
42+
43+
CompletableFuture<Envelope> future =
44+
awaiter.await("client1", testId, env -> true, Duration.ofMillis(500));
45+
46+
Envelope env = envelope(testId, "client1");
47+
boolean consumed = awaiter.signal(env);
48+
49+
assertTrue(consumed);
50+
assertEquals(env, future.get(100, TimeUnit.MILLISECONDS));
51+
}
52+
53+
@Test
54+
void signalNonMatchingIdIgnored() {
55+
ResponseAwaiter awaiter = new ResponseAwaiter();
56+
UUID awaitId = UUID.randomUUID();
57+
UUID signalId = UUID.randomUUID();
58+
59+
CompletableFuture<Envelope> future =
60+
awaiter.await("client1", awaitId, env -> true, Duration.ofMillis(500));
61+
62+
Envelope env = envelope(signalId, "client1");
63+
boolean consumed = awaiter.signal(env);
64+
65+
assertFalse(consumed);
66+
assertFalse(future.isDone());
67+
}
68+
69+
@Test
70+
void timeoutCompletesExceptionally() {
71+
ResponseAwaiter awaiter = new ResponseAwaiter();
72+
UUID testId = UUID.randomUUID();
73+
74+
CompletableFuture<Envelope> future =
75+
awaiter.await("client1", testId, env -> true, Duration.ofMillis(50));
76+
77+
ExecutionException ex = assertThrows(ExecutionException.class,
78+
() -> future.get(300, TimeUnit.MILLISECONDS));
79+
80+
assertInstanceOf(TimeoutException.class, ex.getCause());
81+
}
82+
83+
@Test
84+
void doubleSignalReturnsFalse() throws Exception {
85+
ResponseAwaiter awaiter = new ResponseAwaiter();
86+
UUID testId = UUID.randomUUID();
87+
88+
awaiter.await("client1", testId, env -> true, Duration.ofMillis(500));
89+
90+
Envelope env = envelope(testId, "client1");
91+
boolean first = awaiter.signal(env);
92+
boolean second = awaiter.signal(env);
93+
94+
assertTrue(first);
95+
assertFalse(second);
96+
}
97+
98+
@Test
99+
void nullClientIdFallback() throws Exception {
100+
ResponseAwaiter awaiter = new ResponseAwaiter();
101+
UUID testId = UUID.randomUUID();
102+
103+
CompletableFuture<Envelope> future =
104+
awaiter.await(null, testId, env -> true, Duration.ofMillis(500));
105+
106+
Envelope env = envelope(testId, "anyClient");
107+
boolean consumed = awaiter.signal(env);
108+
109+
assertTrue(consumed);
110+
assertEquals(env, future.get(100, TimeUnit.MILLISECONDS));
111+
}
112+
}

0 commit comments

Comments
 (0)