Skip to content

Commit

Permalink
tests: refactor OnFailureExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
VonDerBeck committed Nov 22, 2023
1 parent 88d00ab commit 721c0ff
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import io.camunda.zeebe.model.bpmn.BpmnModelInstance;
import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.zeebe.exporter.proto.Schema;
import io.zeebe.redis.exporter.ProtobufCodec;
import io.zeebe.redis.exporter.XInfoGroup;
import io.zeebe.redis.testcontainers.OnFailureExtension;
import io.zeebe.redis.testcontainers.ZeebeTestContainer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

Expand All @@ -22,6 +20,7 @@
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;

@Testcontainers
@ExtendWith(OnFailureExtension.class)
public class ExporterDeleteAfterAcknowledgeTest {

private static final BpmnModelInstance WORKFLOW =
Expand All @@ -37,15 +36,11 @@ public class ExporterDeleteAfterAcknowledgeTest {
public ZeebeTestContainer zeebeContainer = ZeebeTestContainer
.withCleanupCycleInSeconds(2).doDeleteAfterAcknowledge(true);

@RegisterExtension
static OnFailureExtension onFailureExtension = new OnFailureExtension();

private RedisClient redisClient;
private StatefulRedisConnection<String, byte[]> redisConnection;

@BeforeEach
public void init() {
onFailureExtension.setZeebeTestContainer(zeebeContainer);
redisClient = RedisClient.create(zeebeContainer.getRedisAddress());
redisConnection = redisClient.connect(new ProtobufCodec());
redisConnection.sync().xtrim("zeebe:DEPLOYMENT", 0);
Expand Down
10 changes: 2 additions & 8 deletions exporter/src/test/java/io/zeebe/redis/ExporterJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
import io.camunda.zeebe.model.bpmn.BpmnModelInstance;
import io.lettuce.core.Range;
import io.lettuce.core.RedisClient;
import io.lettuce.core.XGroupCreateArgs;
import io.lettuce.core.XReadArgs;
import io.lettuce.core.api.StatefulRedisConnection;
import io.zeebe.redis.exporter.ExporterConfiguration;
import io.zeebe.redis.testcontainers.OnFailureExtension;
import io.zeebe.redis.testcontainers.ZeebeTestContainer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.assertj.core.api.Assertions.assertThat;

@Testcontainers
@ExtendWith(OnFailureExtension.class)
public class ExporterJsonTest {

private static final BpmnModelInstance WORKFLOW =
Expand All @@ -34,15 +32,11 @@ public class ExporterJsonTest {
@Container
public ZeebeTestContainer zeebeContainer = ZeebeTestContainer.withJsonFormat();

@RegisterExtension
static OnFailureExtension onFailureExtension = new OnFailureExtension();

private RedisClient redisClient;
private StatefulRedisConnection<String, String> redisConnection;

@BeforeEach
public void init() {
onFailureExtension.setZeebeTestContainer(zeebeContainer);
redisClient = RedisClient.create(zeebeContainer.getRedisAddress());
redisConnection = redisClient.connect();
redisConnection.sync().xtrim("zeebe:DEPLOYMENT", 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.shaded.org.awaitility.Awaitility;
Expand All @@ -21,6 +21,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@Testcontainers
@ExtendWith(OnFailureExtension.class)
public class ExporterMaxTimeToLiveTest {

private static final BpmnModelInstance WORKFLOW =
Expand All @@ -36,15 +37,11 @@ public class ExporterMaxTimeToLiveTest {
public ZeebeTestContainer zeebeContainer = ZeebeTestContainer
.withCleanupCycleInSeconds(2).withMaxTTLInSeconds(6);

@RegisterExtension
static OnFailureExtension onFailureExtension = new OnFailureExtension();

private RedisClient redisClient;
private StatefulRedisConnection<String, byte[]> redisConnection;

@BeforeEach
public void init() {
onFailureExtension.setZeebeTestContainer(zeebeContainer);
redisClient = RedisClient.create(zeebeContainer.getRedisAddress());
redisConnection = redisClient.connect(new ProtobufCodec());
redisConnection.sync().xtrim("zeebe:DEPLOYMENT", 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

Expand All @@ -20,6 +20,7 @@
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;

@Testcontainers
@ExtendWith(OnFailureExtension.class)
public class ExporterMinTimeToLiveTest {

private static final BpmnModelInstance WORKFLOW =
Expand All @@ -35,15 +36,11 @@ public class ExporterMinTimeToLiveTest {
public ZeebeTestContainer zeebeContainer = ZeebeTestContainer
.withCleanupCycleInSeconds(2).doDeleteAfterAcknowledge(true).withMinTTLInSeconds(5);

@RegisterExtension
static OnFailureExtension onFailureExtension = new OnFailureExtension();

private RedisClient redisClient;
private StatefulRedisConnection<String, byte[]> redisConnection;

@BeforeEach
public void init() {
onFailureExtension.setZeebeTestContainer(zeebeContainer);
redisClient = RedisClient.create(zeebeContainer.getRedisAddress());
redisConnection = redisClient.connect(new ProtobufCodec());
redisConnection.sync().xtrim("zeebe:DEPLOYMENT", 0);
Expand Down
7 changes: 2 additions & 5 deletions exporter/src/test/java/io/zeebe/redis/ExporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.assertj.core.api.Assertions.assertThat;

@Testcontainers
@ExtendWith(OnFailureExtension.class)
public class ExporterTest {

private static final BpmnModelInstance WORKFLOW =
Expand All @@ -32,15 +33,11 @@ public class ExporterTest {
@Container
public ZeebeTestContainer zeebeContainer = ZeebeTestContainer.withDefaultConfig();

@RegisterExtension
static OnFailureExtension onFailureExtension = new OnFailureExtension();

private RedisClient redisClient;
private StatefulRedisConnection<String, byte[]> redisConnection;

@BeforeEach
public void init() {
onFailureExtension.setZeebeTestContainer(zeebeContainer);
redisClient = RedisClient.create(zeebeContainer.getRedisAddress());
redisConnection = redisClient.connect(new ProtobufCodec());
redisConnection.sync().xtrim("zeebe:DEPLOYMENT", 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.shaded.org.awaitility.Awaitility;
Expand All @@ -22,6 +22,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@Testcontainers
@ExtendWith(OnFailureExtension.class)
public class RedisUnavailabilityTest {

private static final BpmnModelInstance WORKFLOW =
Expand All @@ -35,15 +36,11 @@ public class RedisUnavailabilityTest {
@Container
public ZeebeTestContainer zeebeContainer = ZeebeTestContainer.withJsonFormat();

@RegisterExtension
static OnFailureExtension onFailureExtension = new OnFailureExtension();

private RedisClient redisClient;
private StatefulRedisConnection<String, String> redisConnection;

@BeforeEach
public void init() {
onFailureExtension.setZeebeTestContainer(zeebeContainer);
redisClient = RedisClient.create(zeebeContainer.getRedisAddress());
redisConnection = redisClient.connect();
redisConnection.sync().xtrim("zeebe:DEPLOYMENT", 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
package io.zeebe.redis.testcontainers;

import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.*;
import org.testcontainers.junit.jupiter.Container;

public class OnFailureExtension implements AfterTestExecutionCallback {
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

ZeebeTestContainer zeebeTestContainer;
public class OnFailureExtension implements TestWatcher, BeforeEachCallback, AfterEachCallback {

public void setZeebeTestContainer(ZeebeTestContainer zeebeTestContainer) {
this.zeebeTestContainer = zeebeTestContainer;
private static final Logger LOGGER = Logger.getLogger("ZeebeTestContainer");

private String containerLogs;

@Override
public void testFailed(ExtensionContext context, Throwable cause) {
if (containerLogs != null) {
var prefix = "\n----------------------------------------------------------------------"
+ "\nZeebe container log for " + context.getDisplayName()
+ "\n----------------------------------------------------------------------\n";
LOGGER.log(Level.WARNING, prefix + containerLogs);
}
}

@Override
public void afterTestExecution(ExtensionContext extensionContext) throws Exception {
if (extensionContext.getExecutionException() != null && zeebeTestContainer != null) {
zeebeTestContainer.writeContainerLogs();
public void afterEach(ExtensionContext extensionContext) {
var testInstance = extensionContext.getTestInstance();
if (testInstance.isEmpty()) return;
var zeebeTestContainerField = Arrays.stream(testInstance.get().getClass().getDeclaredFields())
.filter(field -> field.isAnnotationPresent(Container.class)
&& field.getType().equals(ZeebeTestContainer.class)).findFirst();
if (zeebeTestContainerField.isPresent()) {
var field = zeebeTestContainerField.get();
field.setAccessible(true);
try {
var zeebeTestContainer = (ZeebeTestContainer) field.get(testInstance.get());
containerLogs = zeebeTestContainer.getLogs();
} catch (IllegalAccessException ex) {
// NOOP
}
}
}

@Override
public void beforeEach(ExtensionContext extensionContext) {
containerLogs = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,4 @@ public String getRedisAddress() {
return "redis://" + redisContainer.getRedisServerExternalAddress();
}

public void writeContainerLogs() {
var prefix = "\n----------------------------------------------------------------------"
+ "\nZeebe container log:"
+ "\n----------------------------------------------------------------------\n";
LOGGER.log(Level.WARNING, prefix + getLogs());
}
}

0 comments on commit 721c0ff

Please sign in to comment.