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

Test improvements #1931

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void registerCallback(int depth, Consumer<PathChildrenCacheEvent> callbac
}

private HierarchicalCacheLevel createLevelCache(int depth, String path) {
BiFunction<Integer, String, HierarchicalCacheLevel> function =
BiFunction<Integer, String, HierarchicalCacheLevel> nextLevelFactory =
depth + 1 < maxDepth ? this::createLevelCache : null;
HierarchicalCacheLevel levelCache =
new HierarchicalCacheLevel(
Expand All @@ -74,7 +74,7 @@ private HierarchicalCacheLevel createLevelCache(int depth, String path) {
path(depth, path),
depth,
levelCallbacks.get(depth),
Optional.ofNullable(function),
Optional.ofNullable(nextLevelFactory),
removeNodesWithNoData);
try {
logger.debug("Starting hierarchical cache level for path {} and depth {}", path, depth);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package pl.allegro.tech.hermes.management.infrastructure.zookeeper;

import static org.slf4j.LoggerFactory.getLogger;

import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.slf4j.Logger;
import pl.allegro.tech.hermes.common.exception.InternalProcessingException;

public class ZookeeperClient {

private static final Logger logger = getLogger(ZookeeperClient.class);
private final CuratorFramework curatorFramework;
private final String datacenterName;

Expand All @@ -36,7 +32,7 @@ public void ensureEphemeralNodeExists(String path) {
.forPath(path);
}
} catch (Exception e) {
throw new InternalProcessingException("Could not ensure existence of path: " + path);
throw new InternalProcessingException("Could not ensure existence of path: " + path, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import static pl.allegro.tech.hermes.integrationtests.setup.HermesManagementTestApp.AUDIT_EVENT_PATH;
import static pl.allegro.tech.hermes.test.helper.endpoint.TimeoutAdjuster.adjust;

import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
Expand All @@ -34,7 +36,10 @@
import pl.allegro.tech.hermes.test.helper.environment.HermesTestApp;

public class HermesExtension
implements BeforeAllCallback, AfterAllCallback, ExtensionContext.Store.CloseableResource {
implements AfterEachCallback,
BeforeAllCallback,
AfterAllCallback,
ExtensionContext.Store.CloseableResource {

private static final Logger logger = LoggerFactory.getLogger(HermesExtension.class);

Expand Down Expand Up @@ -62,7 +67,8 @@ public class HermesExtension
public static BrokerOperations brokerOperations;

@Override
public void beforeAll(ExtensionContext context) {
public void beforeAll(ExtensionContext context) throws IOException, InterruptedException {

if (!started) {
Stream.of(hermesZookeeper, kafka).parallel().forEach(Startable::start);
schemaRegistry.start();
Expand Down Expand Up @@ -179,4 +185,13 @@ public HermesExtension withFrontendProfile(String profile) {
public void afterAll(ExtensionContext context) {
Stream.of(management, consumers, frontend).forEach(HermesTestApp::restoreDefaultSettings);
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
try {
clearManagementData();
} catch (Exception e) {
logger.error("Error during cleaning up management data", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import pl.allegro.tech.hermes.integrationtests.prometheus.PrometheusExtension;
import pl.allegro.tech.hermes.management.HermesManagement;
Expand Down Expand Up @@ -78,6 +81,7 @@ private void waitUntilReady() {
HttpRequest request =
HttpRequest.newBuilder()
.uri(new URI("http://localhost:" + getPort() + "/mode"))
.timeout(Duration.ofSeconds(1))
.GET()
.build();
HttpClient httpClient = HttpClient.newHttpClient();
Expand Down Expand Up @@ -109,7 +113,7 @@ public int getPort() {
@Override
public void stop() {
if (app != null) {
app.context().close();
Optional.ofNullable(app.context()).ifPresent(ConfigurableApplicationContext::close);
app = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import ch.qos.logback.core.read.ListAppender;
import java.time.Duration;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -48,12 +47,6 @@ void createLogAppender() {
@AfterEach
void teardown() {
((Logger) LoggerFactory.getLogger(DefaultConsumerProfiler.class)).detachAndStopAllAppenders();
hermes.clearManagementData();
}

@AfterAll
static void teardownClass() {
hermes.clearManagementData();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import pl.allegro.tech.hermes.api.OwnerId;
Expand All @@ -20,10 +20,10 @@ public class ListSubscriptionForOwnerTest {

@RegisterExtension public static final HermesExtension hermes = new HermesExtension();

private static Topic topic;
private Topic topic;

@BeforeAll
public static void createTopic() {
@BeforeEach
public void createTopic() {
topic = hermes.initHelper().createTopic(topicWithRandomName().build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import java.time.Duration;
import java.util.Set;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -34,16 +32,6 @@ public class ListUnhealthySubscriptionsForOwnerTest {
@RegisterExtension
public static final HermesExtension hermes = new HermesExtension().withPrometheus(prometheus);

@BeforeAll
static void setup() {
hermes.clearManagementData();
}

@AfterEach
void cleanup() {
hermes.clearManagementData();
}

@Test
public void shouldNotListHealthySubscriptions() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
Expand All @@ -26,8 +26,8 @@ public class OfflineRetransmissionManagementTest {

private static final String GROUP = "pl.allegro.retransmission";

@BeforeAll
public static void setupGroup() {
@BeforeEach
public void setupGroup() {
hermes.initHelper().createGroup(Group.from(GROUP));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</Pattern>
<Pattern>[%thread] - %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</Pattern>
</encoder>
</appender>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.stream.Stream;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.testcontainers.lifecycle.Startable;
Expand All @@ -33,23 +34,31 @@ public class KafkaReadinessCheckTest {
private static final KafkaContainerCluster kafka = new KafkaContainerCluster(3);
private static final ConfluentSchemaRegistryContainer schemaRegistry =
new ConfluentSchemaRegistryContainer().withKafkaCluster(kafka);
private static HermesFrontendTestApp frontendApp =
new HermesFrontendTestApp(hermesZookeeper, kafka, schemaRegistry);
private static Topic topic;
private static HermesInitHelper hermesInitHelper;
private static HermesTestApp management;

@BeforeAll
public static void setup() {
Stream.of(hermesZookeeper, kafka).parallel().forEach(Startable::start);
schemaRegistry.start();
HermesTestApp management =
new HermesManagementTestApp(hermesZookeeper, kafka, schemaRegistry).start();
management = new HermesManagementTestApp(hermesZookeeper, kafka, schemaRegistry).start();
frontendApp.start();
hermesInitHelper = new HermesInitHelper(management.getPort());
}

HermesInitHelper hermesInitHelper = new HermesInitHelper(management.getPort());
@BeforeEach
public void beforeEach() {
topic = hermesInitHelper.createTopic(topicWithRandomName().withAck(ALL).build());
management.stop();
}

@AfterAll
public static void clean() {
Stream.of(hermesZookeeper, kafka, schemaRegistry).parallel().forEach(Startable::stop);
frontendApp.stop();
management.stop();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</Pattern>
<Pattern>[%thread] - %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</Pattern>
</encoder>
</appender>

Expand Down
Loading