|
12 | 12 | import com.launchdarkly.testhelpers.httptest.Handlers; |
13 | 13 | import com.launchdarkly.testhelpers.httptest.HttpServer; |
14 | 14 | import com.launchdarkly.testhelpers.httptest.RequestInfo; |
| 15 | +import com.launchdarkly.testhelpers.tcptest.TcpHandler; |
| 16 | +import com.launchdarkly.testhelpers.tcptest.TcpHandlers; |
| 17 | +import com.launchdarkly.testhelpers.tcptest.TcpServer; |
15 | 18 | import org.junit.Test; |
16 | 19 |
|
17 | 20 | import java.net.URI; |
18 | 21 | import java.time.Duration; |
19 | 22 | import java.util.concurrent.CompletableFuture; |
20 | 23 | import java.util.concurrent.TimeUnit; |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
21 | 25 |
|
22 | 26 | import static com.launchdarkly.sdk.server.ComponentsImpl.toHttpProperties; |
23 | 27 | import static com.launchdarkly.sdk.server.TestComponents.basicDiagnosticStore; |
@@ -358,6 +362,57 @@ public void goodbyeEventInResponse() throws Exception { |
358 | 362 | } |
359 | 363 | } |
360 | 364 |
|
| 365 | + @Test |
| 366 | + public void reconnectOpensNewConnection() throws Exception { |
| 367 | + String serverIntent = makeEvent("server-intent", "{\"payloads\":[{\"id\":\"payload-1\",\"target\":100,\"intentCode\":\"xfer-full\",\"reason\":\"payload-missing\"}]}"); |
| 368 | + String payloadTransferred = makeEvent("payload-transferred", "{\"state\":\"(p:payload-1:100)\",\"version\":100}"); |
| 369 | + |
| 370 | + // The first stream ends normally. This leaves a connection that the HTTP client could reuse. |
| 371 | + try (HttpServer server = HttpServer.start(Handlers.sequential( |
| 372 | + Handlers.all( |
| 373 | + Handlers.SSE.start(), |
| 374 | + Handlers.SSE.event(serverIntent), |
| 375 | + Handlers.SSE.event(payloadTransferred)), |
| 376 | + Handlers.all( |
| 377 | + Handlers.SSE.start(), |
| 378 | + Handlers.SSE.event(serverIntent), |
| 379 | + Handlers.SSE.event(payloadTransferred), |
| 380 | + Handlers.SSE.leaveOpen())))) { |
| 381 | + AtomicInteger connectionCount = new AtomicInteger(); |
| 382 | + TcpHandler forwardToServer = TcpHandlers.forwardToPort(server.getPort()); |
| 383 | + TcpHandler countConnections = socket -> { |
| 384 | + connectionCount.incrementAndGet(); |
| 385 | + forwardToServer.apply(socket); |
| 386 | + }; |
| 387 | + |
| 388 | + try (TcpServer countingServer = TcpServer.start(countConnections)) { |
| 389 | + HttpProperties httpProperties = toHttpProperties(clientContext("sdk-key", baseConfig().build()).getHttp()); |
| 390 | + |
| 391 | + StreamingSynchronizerImpl synchronizer = new StreamingSynchronizerImpl( |
| 392 | + httpProperties, |
| 393 | + countingServer.getHttpUri(), |
| 394 | + "/stream", |
| 395 | + testLogger, |
| 396 | + mockSelectorSource(), |
| 397 | + null, |
| 398 | + Duration.ofMillis(100), |
| 399 | + Thread.NORM_PRIORITY, |
| 400 | + null |
| 401 | + ); |
| 402 | + |
| 403 | + FDv2SourceResult result = synchronizer.next().get(5, TimeUnit.SECONDS); |
| 404 | + assertEquals(SourceResultType.CHANGE_SET, result.getResultType()); |
| 405 | + |
| 406 | + server.getRecorder().requireRequest(); |
| 407 | + server.getRecorder().requireRequest(); |
| 408 | + |
| 409 | + assertEquals(2, connectionCount.get()); |
| 410 | + |
| 411 | + synchronizer.close(); |
| 412 | + } |
| 413 | + } |
| 414 | + } |
| 415 | + |
361 | 416 | @Test |
362 | 417 | public void heartbeatEvent() throws Exception { |
363 | 418 | String heartbeatEvent = makeEvent("heartbeat", "{}"); |
|
0 commit comments