Skip to content

Commit

Permalink
ci: add cluster termination for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kristian committed Oct 4, 2021
1 parent 6c8fea8 commit 88df665
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/test/java/io/neonbee/NeonBeeExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -34,11 +35,16 @@
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.hazelcast.core.LifecycleService;

import io.neonbee.test.helper.SystemHelper;
import io.vertx.core.Vertx;
import io.vertx.core.VertxException;
import io.vertx.core.impl.VertxImpl;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxTestContext;
import io.vertx.spi.cluster.hazelcast.HazelcastClusterManager;

@SuppressWarnings("rawtypes")
public class NeonBeeExtension implements ParameterResolver, BeforeTestExecutionCallback, AfterTestExecutionCallback,
Expand Down Expand Up @@ -273,10 +279,34 @@ private NeonBee createNeonBee(NeonBeeOptions neonBeeOptions) {
throw new VertxException("NeonBee initialization failed.");
}

@SuppressWarnings("FutureReturnValueIgnored")
private ThrowingConsumer<NeonBee> closeNeonBee() {
return neonBee -> {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Throwable> errorBox = new AtomicReference<>();

// additional logic for tests, due to Hazelcast clusters tend to get stuck, after test execution finishes
Vertx vertx = neonBee.getVertx();
if (vertx instanceof VertxImpl) {
ClusterManager clusterManager = ((VertxImpl) vertx).getClusterManager();
if (clusterManager instanceof HazelcastClusterManager) {
LifecycleService clusterLifecycleService =
((HazelcastClusterManager) clusterManager).getHazelcastInstance().getLifecycleService();
Executors.newSingleThreadScheduledExecutor(runnable -> {
Thread thread = new Thread(runnable, "neonbee-cluster-terminator");
thread.setDaemon(true);
return thread;
}).schedule(() -> {
if (clusterLifecycleService.isRunning()) {
LOGGER.warn("Forcefully terminating Hazelcast cluster after test");
}

// terminate the cluster in any case, if already terminated, this call will do nothing
clusterLifecycleService.terminate();
}, 10, TimeUnit.SECONDS);
}
}

neonBee.getVertx().close(ar -> {
if (ar.failed()) {
errorBox.set(ar.cause());
Expand Down

0 comments on commit 88df665

Please sign in to comment.