Skip to content

Commit

Permalink
fix kafka test
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed May 10, 2024
1 parent cbd57b0 commit c85164e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;

/** Spring has a test container integration, but that doesn't work for Spring Boot 2 */
@DisabledInNativeImage // See GraalVmNativeKafkaSpringStarterSmokeTest for the GraalVM native test
public class AbstractJvmKafkaSpringStarterSmokeTest extends AbstractKafkaSpringStarterSmokeTest {
static KafkaContainer kafka;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ abstract class AbstractKafkaSpringStarterSmokeTest extends AbstractSpringStarter

@Autowired protected KafkaTemplate<String, String> kafkaTemplate;

// In kafka 2 ops.send is deprecated. We are using it to avoid reflection because kafka 3 also has
// ops.send, although with different return type.
@SuppressWarnings({"unchecked", "deprecation"})
@Test
void shouldInstrumentProducerAndConsumer() {
Expand All @@ -39,7 +37,14 @@ void shouldInstrumentProducerAndConsumer() {
() -> {
kafkaTemplate.executeInTransaction(
ops -> {
ops.send("testTopic", "10", "testSpan");
// return type is incompatible between Spring Boot 2 and 3
try {
ops.getClass()
.getDeclaredMethod("send", String.class, Object.class, Object.class)
.invoke(ops, "testTopic", "10", "testSpan");
} catch (Exception e) {
throw new IllegalStateException(e);
}
return 0;
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import io.opentelemetry.api.OpenTelemetry;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,6 +23,11 @@ public abstract class AbstractSpringStarterSmokeTest {

protected SpringSmokeTestRunner testing;

@BeforeAll
static void beforeAll() {
SpringSmokeTestRunner.resetExporters();
}

@BeforeEach
void setUpTesting() {
if (openTelemetry != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@
*/
public final class SpringSmokeTestRunner extends InstrumentationTestRunner {

static final InMemorySpanExporter testSpanExporter = InMemorySpanExporter.create();
static final InMemoryMetricExporter testMetricExporter =
InMemoryMetricExporter.create(AggregationTemporality.DELTA);
static final InMemoryLogRecordExporter testLogRecordExporter = InMemoryLogRecordExporter.create();
static InMemorySpanExporter testSpanExporter;
static InMemoryMetricExporter testMetricExporter;
static InMemoryLogRecordExporter testLogRecordExporter;

static OpenTelemetry openTelemetry;

public SpringSmokeTestRunner(OpenTelemetry openTelemetry) {
super(openTelemetry);
}

static void resetExporters() {
testSpanExporter = InMemorySpanExporter.create();
testMetricExporter = InMemoryMetricExporter.create(AggregationTemporality.DELTA);
testLogRecordExporter = InMemoryLogRecordExporter.create();
}

@Override
public void beforeTestClass() {}

Expand Down

0 comments on commit c85164e

Please sign in to comment.