|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.databind.JsonNode; |
4 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
5 | 6 | import io.github.easy4j.opencli.OpenCliProperties; |
6 | 7 | import io.github.easy4j.opencli.contract.ContractProbe; |
7 | 8 | import io.github.easy4j.opencli.contract.LifecycleProbe; |
|
13 | 14 | import java.nio.file.Path; |
14 | 15 | import java.util.Arrays; |
15 | 16 | import java.util.Base64; |
| 17 | +import java.util.HashSet; |
| 18 | +import java.util.Set; |
16 | 19 | import java.util.List; |
17 | 20 | import java.util.Objects; |
18 | 21 | import java.util.concurrent.BlockingQueue; |
|
26 | 29 | import org.java_websocket.server.WebSocketServer; |
27 | 30 | import org.junit.jupiter.api.Test; |
28 | 31 | import org.junit.jupiter.api.io.TempDir; |
| 32 | +import org.junit.jupiter.params.ParameterizedTest; |
| 33 | +import org.junit.jupiter.params.provider.CsvSource; |
| 34 | +import org.junit.jupiter.params.provider.ValueSource; |
29 | 35 |
|
30 | 36 | import static org.junit.jupiter.api.Assertions.assertEquals; |
31 | 37 | import static org.junit.jupiter.api.Assertions.assertFalse; |
32 | 38 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
33 | 39 | import static org.junit.jupiter.api.Assertions.assertNotSame; |
34 | 40 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
35 | 42 | import static org.junit.jupiter.api.Assertions.assertTrue; |
36 | 43 |
|
37 | 44 | class OpenCliWsReverseAgentSessionTest { |
@@ -112,6 +119,113 @@ void collectBeforeRegistrationAcknowledgementDoesNotStartChild(@TempDir Path tem |
112 | 119 | } |
113 | 120 | } |
114 | 121 |
|
| 122 | + @Test |
| 123 | + void defaultsToFourActiveCollects(@TempDir Path temporary) throws Exception { |
| 124 | + Path release = temporary.resolve("release"); |
| 125 | + OpenCliProperties properties = probe(LifecycleProbe.class.getName()); |
| 126 | + properties.setMaxConcurrentExecutions(8); |
| 127 | + properties.setCommandTimeoutMillis(10_000); |
| 128 | + try (LocalCenter center = new LocalCenter(); |
| 129 | + OpenCliWsReverseAgentClient agent = new OpenCliWsReverseAgentClient(properties, center.agentProperties())) { |
| 130 | + agent.start(); |
| 131 | + WebSocket socket = center.receive("register").socket; |
| 132 | + socket.send("{\"type\":\"registered\"}"); |
| 133 | + try { |
| 134 | + for (int index = 0; index < 4; index++) { |
| 135 | + sendHeldCollect(socket, "collect-" + index, temporary.resolve("child-" + index), release); |
| 136 | + } |
| 137 | + for (int index = 0; index < 4; index++) { |
| 138 | + assertTrue(awaitMarker(temporary.resolve("child-" + index), 5_000), "initial child did not start"); |
| 139 | + } |
| 140 | + Path queued = temporary.resolve("child-4"); |
| 141 | + sendHeldCollect(socket, "collect-4", queued, release); |
| 142 | + assertFalse(awaitMarker(queued, 1_250), "a fifth collect started before a worker was free"); |
| 143 | + } finally { |
| 144 | + Files.createFile(release); |
| 145 | + } |
| 146 | + Set<String> completed = new HashSet<>(); |
| 147 | + for (int index = 0; index < 5; index++) { |
| 148 | + JsonNode result = center.receive("result").body; |
| 149 | + assertTrue(result.path("success").asBoolean(), result::toString); |
| 150 | + completed.add(result.path("request_id").asText()); |
| 151 | + } |
| 152 | + assertEquals(new HashSet<>(Arrays.asList("collect-0", "collect-1", "collect-2", "collect-3", "collect-4")), |
| 153 | + completed); |
| 154 | + assertTrue(Files.exists(temporary.resolve("child-4"))); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @ParameterizedTest |
| 159 | + @ValueSource(ints = {0, 1}) |
| 160 | + void rejectsOverflowWithoutLaunchingAnotherChild(int queueCapacity, @TempDir Path temporary) throws Exception { |
| 161 | + Path release = temporary.resolve("release"); |
| 162 | + Path active = temporary.resolve("active"); |
| 163 | + Path queued = temporary.resolve("queued"); |
| 164 | + Path rejected = temporary.resolve("rejected"); |
| 165 | + try (LocalCenter center = new LocalCenter()) { |
| 166 | + OpenCliWsAgentConnectionProperties connection = center.agentProperties(); |
| 167 | + connection.setMaxConcurrentCollects(1); |
| 168 | + connection.setMaxPendingCollects(queueCapacity); |
| 169 | + try (OpenCliWsReverseAgentClient agent = new OpenCliWsReverseAgentClient( |
| 170 | + probe(LifecycleProbe.class.getName()), connection)) { |
| 171 | + agent.start(); |
| 172 | + WebSocket socket = center.receive("register").socket; |
| 173 | + socket.send("{\"type\":\"registered\"}"); |
| 174 | + try { |
| 175 | + sendHeldCollect(socket, "active", active, release); |
| 176 | + assertTrue(awaitMarker(active, 3_000), "first collect did not start"); |
| 177 | + if (queueCapacity > 0) { |
| 178 | + sendHeldCollect(socket, "queued", queued, release); |
| 179 | + } |
| 180 | + sendHeldCollect(socket, "overflow", rejected, release); |
| 181 | + JsonNode busy = center.receive("result").body; |
| 182 | + assertEquals("overflow", busy.path("request_id").asText()); |
| 183 | + assertFalse(busy.path("success").asBoolean()); |
| 184 | + assertEquals("worker is busy", busy.path("error").asText()); |
| 185 | + assertFalse(Files.exists(rejected)); |
| 186 | + assertFalse(Files.exists(queued)); |
| 187 | + } finally { |
| 188 | + Files.createFile(release); |
| 189 | + } |
| 190 | + JsonNode completed = center.receive("result").body; |
| 191 | + assertEquals("active", completed.path("request_id").asText()); |
| 192 | + assertTrue(completed.path("success").asBoolean()); |
| 193 | + if (queueCapacity > 0) { |
| 194 | + JsonNode drained = center.receive("result").body; |
| 195 | + assertEquals("queued", drained.path("request_id").asText()); |
| 196 | + assertTrue(drained.path("success").asBoolean()); |
| 197 | + assertTrue(Files.exists(queued)); |
| 198 | + } |
| 199 | + assertFalse(Files.exists(rejected)); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + @ParameterizedTest |
| 205 | + @CsvSource({"0,32", "-1,32", "4,-1", "4,2147483647", "4,2147483644"}) |
| 206 | + void rejectsInvalidCapacityBeforeConnection(int workers, int pending) { |
| 207 | + OpenCliWsAgentConnectionProperties connection = new OpenCliWsAgentConnectionProperties(); |
| 208 | + connection.setMaxConcurrentCollects(workers); |
| 209 | + connection.setMaxPendingCollects(pending); |
| 210 | + assertThrows(IllegalArgumentException.class, |
| 211 | + () -> new OpenCliWsReverseAgentClient(new OpenCliProperties(), connection)); |
| 212 | + } |
| 213 | + |
| 214 | + private static void sendHeldCollect(WebSocket socket, String id, Path marker, Path release) { |
| 215 | + ObjectNode message = MAPPER.createObjectNode() |
| 216 | + .put("type", "collect").put("request_id", id).put("site", "hold").put("command", marker.toString()); |
| 217 | + message.putArray("positional_args").add(release.toString()); |
| 218 | + socket.send(message.toString()); |
| 219 | + } |
| 220 | + |
| 221 | + private static boolean awaitMarker(Path marker, long timeoutMillis) throws InterruptedException { |
| 222 | + long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis); |
| 223 | + while (!Files.exists(marker) && System.nanoTime() < deadline) { |
| 224 | + Thread.sleep(10); |
| 225 | + } |
| 226 | + return Files.exists(marker); |
| 227 | + } |
| 228 | + |
115 | 229 | @Test |
116 | 230 | void retriesAfterHandshakeCloseAndReturnsChildFailureToCenter() throws Exception { |
117 | 231 | try (LocalCenter center = new LocalCenter(); |
|
0 commit comments