Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add several configuration options to ConsulFactory #78

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/java/org/kiwiproject/dropwizard/consul/ConsulFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.base.Preconditions;
import com.google.common.net.HostAndPort;
import com.orbitz.consul.Consul;
import com.orbitz.consul.config.ClientConfig;
import io.dropwizard.util.Duration;
import io.dropwizard.validation.MinDuration;
import org.apache.commons.net.util.SubnetUtils;
Expand Down Expand Up @@ -55,6 +56,12 @@ public class ConsulFactory {

private Optional<String> healthCheckPath = Optional.empty();

private Optional<Long> networkWriteTimeoutMillis = Optional.empty();

private Optional<Long> networkReadTimeoutMillis = Optional.empty();

private Optional<ClientConfig> clientConfig = Optional.empty();

@JsonProperty
public boolean isEnabled() {
return enabled;
Expand Down Expand Up @@ -231,6 +238,30 @@ public void setHealthCheckPath(String healthCheckPath) {
this.healthCheckPath = Optional.ofNullable(healthCheckPath);
}

public Optional<Long> getNetworkWriteTimeoutMillis() {
return networkWriteTimeoutMillis;
}

public void setNetworkWriteTimeoutMillis(Long networkTimeout) {
this.networkWriteTimeoutMillis = Optional.ofNullable(networkTimeout);
}

public Optional<Long> getNetworkReadTimeoutMillis() {
return networkReadTimeoutMillis;
}

public void setNetworkReadTimeoutMillis(Long networkReadTimeoutMillis) {
this.networkReadTimeoutMillis = Optional.ofNullable(networkReadTimeoutMillis);
}

public Optional<ClientConfig> getClientConfig() {
return clientConfig;
}

public void setClientConfig(ClientConfig clientConfig) {
this.clientConfig = Optional.ofNullable(clientConfig);
}

@JsonIgnore
public Consul build() {

Expand All @@ -244,6 +275,10 @@ public Consul build() {
aclToken.ifPresent(token ->
consulBuilder.withAclToken(token).withHeaders(Map.of(CONSUL_AUTH_HEADER_KEY, token)));

networkWriteTimeoutMillis.ifPresent(consulBuilder::withWriteTimeoutMillis);
networkReadTimeoutMillis.ifPresent(consulBuilder::withReadTimeoutMillis);
clientConfig.ifPresent(consulBuilder::withClientConfiguration);

return consulBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;

import com.orbitz.consul.config.CacheConfig;
import com.orbitz.consul.config.ClientConfig;
import io.dropwizard.Configuration;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

class ConsulBundleTest {

private final ConsulFactory factory = spy(new ConsulFactory());
Expand Down Expand Up @@ -103,6 +108,24 @@ void testPopulatedServiceName() {
assertThat(factory.getServiceName()).isEqualTo("test-service-name");
}

@Test
void shouldSetConsulClientConfigurationProperties() {
var cacheConfig = CacheConfig.builder().withWatchDuration(Duration.of(20, ChronoUnit.SECONDS)).build();
var clientConfig = new ClientConfig(cacheConfig);

factory.setNetworkWriteTimeoutMillis(5_000L);
factory.setNetworkReadTimeoutMillis(10_005L);
factory.setClientConfig(clientConfig);

bundle.run(config, environment);

assertThat(factory.getNetworkWriteTimeoutMillis()).contains(5_000L);
assertThat(factory.getNetworkReadTimeoutMillis()).contains(10_005L);
assertThat(factory.getClientConfig()).hasValueSatisfying(theClientConfig -> {
assertThat(theClientConfig.getCacheConfig().getWatchDuration().toSeconds()).isEqualTo(20L);
});
}

@Test
void testAclToken() {
var token = "acl-token";
Expand Down