Skip to content

Commit

Permalink
JAMES-2717 Restart container periodically and closing all ES clients
Browse files Browse the repository at this point in the history
  • Loading branch information
trantienduchn committed May 9, 2019
1 parent 193a9d8 commit 4bd41b8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.net.InetAddress;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.james.util.Host;
import org.elasticsearch.client.Client;
Expand Down Expand Up @@ -53,11 +54,13 @@ public static ClientProviderImpl fromHosts(ImmutableList<Host> hosts, Optional<S

private final ImmutableList<Host> hosts;
private final Optional<String> clusterName;
private final ConcurrentLinkedQueue<Client> clients;

private ClientProviderImpl(ImmutableList<Host> hosts, Optional<String> clusterName) {
Preconditions.checkArgument(!hosts.isEmpty(), "You should provide at least one host");
this.hosts = hosts;
this.clusterName = clusterName;
this.clients = new ConcurrentLinkedQueue<>();
}


Expand All @@ -72,6 +75,7 @@ public Client get() {
InetAddress.getByName(host.getHostName()),
host.getPort())));
hosts.forEach(consumer.sneakyThrow());
clients.add(transportClient);
return transportClient;
}

Expand All @@ -83,4 +87,12 @@ public Client get() {
}
return Settings.EMPTY;
}

public ConcurrentLinkedQueue<Client> getClients() {
return clients;
}

public void clearClients() {
clients.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import org.apache.http.HttpStatus;
import org.apache.james.util.Host;
import org.apache.james.util.docker.DockerGenericContainer;
import org.apache.james.util.docker.Images;
import org.apache.james.util.docker.RateLimiters;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.lease.Releasable;
import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -59,9 +63,14 @@ static ElasticSearchAPI from(Host esHttpHost) {
private static final int ES_HTTP_PORT = 9200;
private static final int ES_TCP_PORT = 9300;

private static final int MAX_TESTS_PLAYED = 50;

private final DockerGenericContainer eSContainer;
private ClientProviderImpl clientProvider;
private AtomicInteger testPlayedCounter;

public DockerElasticSearch() {
this.testPlayedCounter = new AtomicInteger();
this.eSContainer = new DockerGenericContainer(Images.ELASTICSEARCH_2)
.withExposedPorts(ES_HTTP_PORT, ES_TCP_PORT)
.waitingFor(new HostPortWaitStrategy().withRateLimiter(RateLimiters.TWENTIES_PER_SECOND));
Expand All @@ -70,6 +79,7 @@ public DockerElasticSearch() {
public void start() {
if (!eSContainer.isRunning()) {
eSContainer.start();
clientProvider = createClientProvider();
}
}

Expand Down Expand Up @@ -108,6 +118,17 @@ public void unpause() {
public void cleanUpData() {
assertThat(esAPI().deleteAllIndexes().status())
.isEqualTo(HttpStatus.SC_OK);

Stream.of(clientProvider.getClients().toArray(new Client[]{}))
.forEach(Releasable::close);
clientProvider.clearClients();

// just assuming this is called after every tests. Actually I'm sure about it.
if (testPlayedCounter.incrementAndGet() >= MAX_TESTS_PLAYED) {
stop();
start();
testPlayedCounter.set(0);
}
}

public void awaitForElasticSearch() {
Expand All @@ -116,8 +137,11 @@ public void awaitForElasticSearch() {
}

public ClientProvider clientProvider() {
Optional<String> noClusterName = Optional.empty();
return ClientProviderImpl.fromHosts(ImmutableList.of(getTcpHost()), noClusterName);
return clientProvider;
}

private ClientProviderImpl createClientProvider() {
return ClientProviderImpl.fromHosts(ImmutableList.of(getTcpHost()), Optional.empty());
}

private ElasticSearchAPI esAPI() {
Expand Down

0 comments on commit 4bd41b8

Please sign in to comment.