|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.datasource.doris; |
| 19 | + |
| 20 | +import org.apache.doris.common.ClientPool; |
| 21 | +import org.apache.doris.thrift.FrontendService; |
| 22 | +import org.apache.doris.thrift.TGetBackendMetaResult; |
| 23 | +import org.apache.doris.thrift.TNetworkAddress; |
| 24 | +import org.apache.doris.thrift.TStatus; |
| 25 | +import org.apache.doris.thrift.TStatusCode; |
| 26 | + |
| 27 | +import org.apache.thrift.protocol.TBinaryProtocol; |
| 28 | +import org.apache.thrift.transport.TSocket; |
| 29 | +import org.junit.jupiter.api.Assertions; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.mockito.Mockito; |
| 32 | + |
| 33 | +import java.net.InetAddress; |
| 34 | +import java.net.ServerSocket; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.concurrent.ExecutorService; |
| 37 | +import java.util.concurrent.Executors; |
| 38 | +import java.util.concurrent.Future; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | + |
| 41 | +class FeServiceClientTest { |
| 42 | + @Test |
| 43 | + void rejectsTimeoutThatCannotFitInSocketMilliseconds() { |
| 44 | + Assertions.assertThrows(ArithmeticException.class, () -> new FeServiceClient("remote_catalog", |
| 45 | + Collections.emptyList(), "test_user", "", 1, Integer.MAX_VALUE)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void metadataReadTimeoutUsesSeconds() throws Exception { |
| 50 | + FrontendService.Iface handler = Mockito.mock(FrontendService.Iface.class); |
| 51 | + Mockito.when(handler.getBackendMeta(Mockito.any())).thenAnswer(invocation -> { |
| 52 | + // Ordinary metadata latency must remain within the configured seconds-based timeout. |
| 53 | + Thread.sleep(100); |
| 54 | + return new TGetBackendMetaResult().setStatus(new TStatus(TStatusCode.OK)) |
| 55 | + .setBackends(Collections.emptyList()); |
| 56 | + }); |
| 57 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 58 | + try (ServerSocket server = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) { |
| 59 | + server.setSoTimeout(5000); |
| 60 | + Future<?> response = executor.submit(() -> { |
| 61 | + try (TSocket socket = new TSocket(server.accept())) { |
| 62 | + TBinaryProtocol protocol = new TBinaryProtocol(socket); |
| 63 | + new FrontendService.Processor<>(handler).process(protocol, protocol); |
| 64 | + } catch (Exception e) { |
| 65 | + throw new RuntimeException(e); |
| 66 | + } |
| 67 | + }); |
| 68 | + TNetworkAddress address = new TNetworkAddress(server.getInetAddress().getHostAddress(), |
| 69 | + server.getLocalPort()); |
| 70 | + try { |
| 71 | + FeServiceClient client = new FeServiceClient("remote_catalog", Collections.singletonList(address), |
| 72 | + "test_user", "", 1, 10); |
| 73 | + Assertions.assertTrue(client.listBackends().isEmpty()); |
| 74 | + response.get(5, TimeUnit.SECONDS); |
| 75 | + } finally { |
| 76 | + ClientPool.frontendPool.clearPool(address); |
| 77 | + } |
| 78 | + } finally { |
| 79 | + executor.shutdownNow(); |
| 80 | + Assertions.assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS)); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments