From 07593268b2bf5efd4f43cddb3ebb20a3aae769e5 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Fri, 18 Aug 2023 17:52:04 -0400 Subject: [PATCH] Add "sanity check" tests for AgentClient#ping (#268) This relates to an issue that was in the original consul-client library: https://github.com/rickfast/consul-client/issues/282 The issue claimed that calling AgentClient#ping resulted in a java.io.EOFException which we've never seen. But, go ahead and add an explicit test for this method. Since it is void, the best we can do here is assert that no exception is thrown when Consul is running. Add test for successful ping and for a failed ping when Consul cannot be connected to. The failed test is a RetryingTest that attempts to connect to Consul using a random port between 50000 and 65535. It will make up to five attempts before giving up. This should be more than enough attempts to find a port that causes a failure. I expect this to usually only make one attempt, but don't want to hard code the port, and don't want to fail in the (very unlikely) event there actually is a Consul running on the randomly selected port. --- .../kiwiproject/consul/AgentClientITest.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/itest/java/org/kiwiproject/consul/AgentClientITest.java b/src/itest/java/org/kiwiproject/consul/AgentClientITest.java index d9ff9db..e46a81e 100644 --- a/src/itest/java/org/kiwiproject/consul/AgentClientITest.java +++ b/src/itest/java/org/kiwiproject/consul/AgentClientITest.java @@ -3,14 +3,18 @@ import static java.util.Objects.nonNull; import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType; import static org.kiwiproject.consul.Awaiting.awaitAtMost500ms; import static org.kiwiproject.consul.TestUtils.randomUUIDString; +import com.google.common.net.HostAndPort; import org.assertj.core.api.Assertions; import org.assertj.core.api.Assumptions; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; +import org.junitpioneer.jupiter.RetryingTest; import org.kiwiproject.consul.model.ConsulResponse; import org.kiwiproject.consul.model.agent.FullService; import org.kiwiproject.consul.model.agent.ImmutableFullService; @@ -26,12 +30,14 @@ import org.kiwiproject.consul.option.ImmutableQueryParameterOptions; import org.kiwiproject.consul.option.QueryOptions; +import java.net.ConnectException; import java.net.MalformedURLException; import java.net.URI; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Predicate; @@ -49,6 +55,27 @@ void setUp() { agentClient = client.agentClient(); } + @RepeatedTest(5) + void shouldPing() { + assertThatCode(() -> agentClient.ping()).doesNotThrowAnyException(); + } + + @RetryingTest(name = "attempt {index}", maxAttempts = 5) + void shouldFailPing_WhenConnectionFailure() { + var randomPort = ThreadLocalRandom.current().nextInt(50_000, 65_536); + var consul = Consul.builder() + .withPing(false) + .withHostAndPort(HostAndPort.fromParts("localhost", randomPort)) + .build(); + + var expectToFailAgentClient = consul.agentClient(); + + assertThatExceptionOfType(ConsulException.class) + .isThrownBy(expectToFailAgentClient::ping) + .withMessage("Error connecting to Consul") + .withCauseExactlyInstanceOf(ConnectException.class); + } + @Test void shouldRetrieveAgentInformation() { var agent = agentClient.getAgent();