|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 B3Partners B.V. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | +package org.tailormap.api.controller; |
| 7 | + |
| 8 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 9 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 10 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 11 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 12 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 13 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; |
| 14 | +import static org.tailormap.api.TestRequestProcessor.setServletPath; |
| 15 | + |
| 16 | +import java.lang.invoke.MethodHandles; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import org.awaitility.Awaitility; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.parallel.Execution; |
| 22 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.beans.factory.annotation.Value; |
| 27 | +import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; |
| 28 | +import org.springframework.http.MediaType; |
| 29 | +import org.springframework.test.web.servlet.MockMvc; |
| 30 | +import org.springframework.test.web.servlet.MvcResult; |
| 31 | +import org.tailormap.api.annotation.PostgresIntegrationTest; |
| 32 | +import org.tailormap.api.viewer.model.ServerSentEventResponse; |
| 33 | + |
| 34 | +@PostgresIntegrationTest |
| 35 | +@AutoConfigureMockMvc |
| 36 | +@Execution(ExecutionMode.CONCURRENT) |
| 37 | +class ServerSentEventsControllerIntegrationTest { |
| 38 | + private static final Logger logger = |
| 39 | + LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 40 | + // Unique id avoids interference with parallel/other tests. |
| 41 | + private final String sseClientId = "keepalive-test-" + System.nanoTime(); |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private MockMvc mockMvc; |
| 45 | + |
| 46 | + @Value("${tailormap-api.base-path}") |
| 47 | + private String apiBasePath; |
| 48 | + |
| 49 | + private MvcResult sseResult; |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + void start_sse_stream() throws Exception { |
| 53 | + final String sseUrl = apiBasePath + "/events/" + sseClientId; |
| 54 | + sseResult = mockMvc.perform(get(sseUrl) |
| 55 | + .accept(MediaType.TEXT_EVENT_STREAM) |
| 56 | + .with(setServletPath(sseUrl)) |
| 57 | + .acceptCharset(StandardCharsets.UTF_8)) |
| 58 | + .andExpect(request().asyncStarted()) |
| 59 | + .andReturn(); |
| 60 | + } |
| 61 | + |
| 62 | + /** Check that at least 2 keep-alive messages arrive in 130 seconds. */ |
| 63 | + @Test |
| 64 | + void should_send_keep_alive_messages_for_two_minutes() { |
| 65 | + // Keep this test running for at least 2 minutes, then assert at least 2 keep-alives arrived. |
| 66 | + Awaitility.await("waiting for keep-alive messages") |
| 67 | + .pollDelay(45, SECONDS) |
| 68 | + .pollInterval(15, SECONDS) |
| 69 | + .atLeast(2, MINUTES) |
| 70 | + .atMost(130, SECONDS) |
| 71 | + .logging(logPrinter -> logger.debug("Checking for keep-alive messages in SSE stream... {}", logPrinter)) |
| 72 | + .untilAsserted(() -> { |
| 73 | + final String stream = sseResult.getResponse().getContentAsString(); |
| 74 | + assertThat(count_keep_alive_messages(stream), greaterThanOrEqualTo(2)); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + private int count_keep_alive_messages(String stream) { |
| 79 | + int count = 0; |
| 80 | + int index = 0; |
| 81 | + final String marker = "\"eventType\":\"" + ServerSentEventResponse.EventTypeEnum.KEEP_ALIVE + "\""; |
| 82 | + while ((index = stream.indexOf(marker, index)) != -1) { |
| 83 | + count++; |
| 84 | + index += marker.length(); |
| 85 | + } |
| 86 | + return count; |
| 87 | + } |
| 88 | +} |
0 commit comments