Skip to content

Commit

Permalink
Add "sanity check" tests for AgentClient#ping (#268)
Browse files Browse the repository at this point in the history
This relates to an issue that was in the original consul-client
library: rickfast/consul-client#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.
  • Loading branch information
sleberknight authored Aug 18, 2023
1 parent 9c8cdec commit 0759326
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/itest/java/org/kiwiproject/consul/AgentClientITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit 0759326

Please sign in to comment.