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

KAFKA-18617 Allow use of ClusterInstance inside BeforeEach #18662

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -247,8 +247,12 @@ default Set<GroupProtocol> supportedGroupProtocols() {

void start();

boolean started();

void stop();

boolean stopped();

void shutdownBroker(int brokerId);

void startBroker(int brokerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.apache.kafka.server.fault.FaultHandlerException;

import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;

Expand Down Expand Up @@ -87,7 +87,7 @@ public String getDisplayName(int invocationIndex) {
public List<Extension> getAdditionalExtensions() {
RaftClusterInstance clusterInstance = new RaftClusterInstance(clusterConfig, isCombined);
return Arrays.asList(
(BeforeTestExecutionCallback) context -> {
(BeforeEachCallback) context -> {
clusterInstance.format();
if (clusterConfig.isAutoStart()) {
clusterInstance.start();
Expand Down Expand Up @@ -186,13 +186,23 @@ public void start() {
}
}

@Override
public boolean started() {
return started.get();
}

@Override
public void stop() {
if (stopped.compareAndSet(false, true)) {
Utils.closeQuietly(clusterTestKit, "cluster");
}
}

@Override
public boolean stopped() {
return stopped.get();
}

@Override
public void shutdownBroker(int brokerId) {
findBrokerOrThrow(brokerId).shutdown();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.apache.kafka.common.test.api;

Check notice on line 1 in test-common/test-common-api/src/test/java/org/apache/kafka/common/test/api/ClusterTestBeforeEachTest.java

View workflow job for this annotation

GitHub Actions / build / Compile and Check Java

Unapproved License

File with unapproved license: test-common/test-common-api/src/test/java/org/apache/kafka/common/test/api/ClusterTestBeforeEachTest.java

import org.junit.jupiter.api.BeforeEach;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

license missing

import org.junit.jupiter.api.extension.ExtendWith;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(ClusterTestExtensions.class)
public class ClusterTestBeforeEachTest {
private final ClusterInstance clusterInstance;

ClusterTestBeforeEachTest(ClusterInstance clusterInstance) { // Constructor injections
this.clusterInstance = clusterInstance;
}

@BeforeEach
void before() {
assertNotNull(clusterInstance);
if (!clusterInstance.started()) {
clusterInstance.start();
}
assertDoesNotThrow(clusterInstance::waitForReadyBrokers);
}

@ClusterTest
public void testDefault() {
assertTrue(true);
assertNotNull(clusterInstance);
}

@ClusterTest(autoStart = AutoStart.NO)
public void testNoAutoStart() {
assertTrue(true);
assertNotNull(clusterInstance);
}
}
Loading