|
18 | 18 | */ |
19 | 19 | package org.apache.gravitino.integration.test.container; |
20 | 20 |
|
| 21 | +import com.sun.net.httpserver.HttpExchange; |
| 22 | +import com.sun.net.httpserver.HttpServer; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.OutputStream; |
| 25 | +import java.net.InetAddress; |
| 26 | +import java.net.InetSocketAddress; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
21 | 30 | import java.util.Optional; |
| 31 | +import java.util.concurrent.atomic.AtomicInteger; |
22 | 32 | import org.junit.jupiter.api.Assertions; |
23 | 33 | import org.junit.jupiter.api.Test; |
| 34 | +import org.mockito.Mockito; |
| 35 | +import org.testcontainers.containers.wait.strategy.WaitStrategyTarget; |
24 | 36 |
|
25 | | -/** Tests how {@link TrinoCluster} reads Trino statement responses; nothing here starts Trino. */ |
| 37 | +/** |
| 38 | + * Tests how {@link TrinoCluster} reads Trino responses and waits for a coordinator; a stub HTTP |
| 39 | + * server stands in for Trino, so nothing here starts a container. |
| 40 | + */ |
26 | 41 | public class TestTrinoCluster { |
27 | 42 |
|
| 43 | + @Test |
| 44 | + void testInfoSaysWhetherTheNodeHasStarted() { |
| 45 | + Assertions.assertFalse(TrinoCluster.hasStarted("{\"coordinator\":true,\"starting\":true}")); |
| 46 | + Assertions.assertTrue( |
| 47 | + TrinoCluster.hasStarted("{\"coordinator\":true, \"starting\" : false,\"uptime\":\"1s\"}")); |
| 48 | + Assertions.assertFalse(TrinoCluster.hasStarted("")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testCoordinatorIsReadyOnceStartedWithTheCatalogAndEveryWorker() throws IOException { |
| 53 | + AtomicInteger infoCalls = new AtomicInteger(); |
| 54 | + List<String> queries = new ArrayList<>(); |
| 55 | + HttpServer trino = |
| 56 | + HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); |
| 57 | + // Still starting on the first call; then up with the catalog and two active workers. |
| 58 | + trino.createContext( |
| 59 | + "/v1/info", |
| 60 | + exchange -> respond(exchange, "{\"starting\":" + (infoCalls.incrementAndGet() == 1) + "}")); |
| 61 | + trino.createContext( |
| 62 | + "/v1/statement", |
| 63 | + exchange -> { |
| 64 | + String sql = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8); |
| 65 | + queries.add(sql); |
| 66 | + int count = sql.contains("system.runtime.nodes") ? 2 : 1; |
| 67 | + respond(exchange, "{\"id\":\"q\",\"data\":[[" + count + "]]}"); |
| 68 | + }); |
| 69 | + trino.start(); |
| 70 | + try { |
| 71 | + WaitStrategyTarget target = Mockito.mock(WaitStrategyTarget.class); |
| 72 | + Mockito.when(target.getHost()).thenReturn(InetAddress.getLoopbackAddress().getHostAddress()); |
| 73 | + Mockito.when(target.getMappedPort(TrinoCluster.TRINO_PORT)) |
| 74 | + .thenReturn(trino.getAddress().getPort()); |
| 75 | + |
| 76 | + TrinoCluster.coordinatorReadiness(2).waitUntilReady(target); |
| 77 | + |
| 78 | + Assertions.assertEquals(2, infoCalls.get()); |
| 79 | + Assertions.assertEquals(2, queries.size(), queries.toString()); |
| 80 | + Assertions.assertTrue(queries.get(0).contains("catalog_name = 'gravitino'"), queries.get(0)); |
| 81 | + Assertions.assertTrue(queries.get(1).contains("state = 'active'"), queries.get(1)); |
| 82 | + } finally { |
| 83 | + trino.stop(0); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testPrivateClusterGivesItsCoordinatorARunName() { |
| 89 | + Assertions.assertEquals("trino", TrinoCluster.builder().coordinatorHost()); |
| 90 | + String name = TrinoCluster.builder().withPrivateNames(true).coordinatorHost(); |
| 91 | + Assertions.assertTrue(name.startsWith("gravitino-trino-" + RunNames.runId() + "-"), name); |
| 92 | + Assertions.assertNotEquals( |
| 93 | + name, TrinoCluster.builder().withPrivateNames(true).coordinatorHost()); |
| 94 | + } |
| 95 | + |
| 96 | + private static void respond(HttpExchange exchange, String body) throws IOException { |
| 97 | + byte[] bytes = body.getBytes(StandardCharsets.UTF_8); |
| 98 | + exchange.sendResponseHeaders(200, bytes.length); |
| 99 | + try (OutputStream out = exchange.getResponseBody()) { |
| 100 | + out.write(bytes); |
| 101 | + } |
| 102 | + } |
| 103 | + |
28 | 104 | @Test |
29 | 105 | void testQueuedPageHasNextUriAndNoValue() { |
30 | 106 | String queued = |
|
0 commit comments