Fix junit extension
@@ -22,6 +22,9 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import io.etcd.jetcd.launcher.Etcd;
+import io.etcd.jetcd.launcher.EtcdCluster;
+
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
@@ -29,9 +32,6 @@ import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.containers.Network;
-import io.etcd.jetcd.launcher.Etcd;
-import io.etcd.jetcd.launcher.EtcdCluster;
-
/**
* JUnit5 Extension to have etcd cluster in tests.
*/
@@ -86,18 +86,19 @@ public class EtcdClusterExtension implements BeforeAllCallback, BeforeEachCallba
}
protected synchronized void before(ExtensionContext context) {
- EtcdCluster oldCluster = CLUSTERS.put(cluster.clusterName(), cluster);
- if (oldCluster != null) {
- oldCluster.stop();
- }
-
+ EtcdCluster oldCluster = CLUSTERS.putIfAbsent(cluster.clusterName(), cluster);
+ if (oldCluster == null) {
cluster.start();
}
+ }
protected synchronized void after(ExtensionContext context) {
+ try {
cluster.close();
+ } finally {
CLUSTERS.remove(cluster.clusterName());
}
+ }
public static EtcdCluster cluster(String clusterName) {
return CLUSTERS.get(clusterName);
