Skip to content

Commit

Permalink
Fix AMQP SSLContext tests trying to start broker twice after junit 5.…
Browse files Browse the repository at this point in the history
…11 upgrade
  • Loading branch information
ozangunalp committed Sep 19, 2024
1 parent c3ae8a6 commit efd103c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.smallrye.reactive.messaging.amqp;

import jakarta.enterprise.inject.se.SeContainer;

import org.eclipse.microprofile.config.ConfigProvider;

import io.smallrye.config.SmallRyeConfigProviderResolver;
import io.smallrye.reactive.messaging.providers.connectors.ExecutionHolder;
import io.smallrye.reactive.messaging.providers.extension.HealthCenter;
import io.smallrye.reactive.messaging.test.common.config.MapBasedConfig;
import io.vertx.mutiny.core.Vertx;

public class AmqpBrokerHolder {

public static final AmqpBroker broker = new AmqpBroker();

protected ExecutionHolder executionHolder;
protected final static String host = "127.0.0.1";
protected final static int port = 5672;
protected final static String username = "artemis";
protected final static String password = "artemis";

protected AmqpUsage usage;

public static void startBroker(String brokerXmlUrl) {
if (brokerXmlUrl != null) {
broker.setConfigResourcePath(brokerXmlUrl);
}
broker.start(port);
System.setProperty("amqp-host", host);
System.setProperty("amqp-port", Integer.toString(port));
System.setProperty("amqp-user", username);
System.setProperty("amqp-pwd", password);
}

public static void stopBroker() {
broker.stop();
System.clearProperty("amqp-host");
System.clearProperty("amqp-port");
}

public void setup() {
executionHolder = new ExecutionHolder(Vertx.vertx());
usage = new AmqpUsage(executionHolder.vertx(), host, port, username, password);
SmallRyeConfigProviderResolver.instance().releaseConfig(ConfigProvider.getConfig());
MapBasedConfig.cleanup();
}

public void tearDown() {
usage.close();
executionHolder.terminate(null);
SmallRyeConfigProviderResolver.instance().releaseConfig(ConfigProvider.getConfig());
MapBasedConfig.cleanup();
}

public boolean isAmqpConnectorReady(SeContainer container) {
HealthCenter health = container.getBeanManager().createInstance().select(HealthCenter.class).get();
return health.getReadiness().isOk();
}

public boolean isAmqpConnectorReady(AmqpConnector connector) {
return connector.getReadiness().isOk();
}

public boolean isAmqpConnectorAlive(SeContainer container) {
HealthCenter health = container.getBeanManager().createInstance().select(HealthCenter.class).get();
return health.getLiveness().isOk();
}

public boolean isAmqpConnectorAlive(AmqpConnector connector) {
return connector.getLiveness().isOk();
}

}
Original file line number Diff line number Diff line change
@@ -1,85 +1,30 @@
package io.smallrye.reactive.messaging.amqp;

import jakarta.enterprise.inject.se.SeContainer;

import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;

import io.smallrye.config.SmallRyeConfigProviderResolver;
import io.smallrye.reactive.messaging.providers.connectors.ExecutionHolder;
import io.smallrye.reactive.messaging.providers.extension.HealthCenter;
import io.smallrye.reactive.messaging.test.common.config.MapBasedConfig;
import io.vertx.mutiny.core.Vertx;

public class AmqpBrokerTestBase {

public static final AmqpBroker broker = new AmqpBroker();
protected static final String BROKER_XML_LOCATION = "ampq.broker.xml.name";

protected ExecutionHolder executionHolder;
protected final static String host = "127.0.0.1";
protected final static int port = 5672;
protected final static String username = "artemis";
protected final static String password = "artemis";

protected AmqpUsage usage;
public class AmqpBrokerTestBase extends AmqpBrokerHolder {

@BeforeAll
public static void startBroker() {
String brokerXmlUrl = System.getProperty(BROKER_XML_LOCATION);
if (brokerXmlUrl != null) {
broker.setConfigResourcePath(brokerXmlUrl);
}
broker.start(port);
System.setProperty("amqp-host", host);
System.setProperty("amqp-port", Integer.toString(port));
System.setProperty("amqp-user", username);
System.setProperty("amqp-pwd", password);
startBroker(null);
}

@AfterAll
public static void stopBroker() {
broker.stop();
System.clearProperty("amqp-host");
System.clearProperty("amqp-port");
AmqpBrokerHolder.stopBroker();
}

@BeforeEach
public void setup() {
executionHolder = new ExecutionHolder(Vertx.vertx());

usage = new AmqpUsage(executionHolder.vertx(), host, port, username, password);
SmallRyeConfigProviderResolver.instance().releaseConfig(ConfigProvider.getConfig());
MapBasedConfig.cleanup();
super.setup();
}

@AfterEach
public void tearDown() {
usage.close();
executionHolder.terminate(null);
SmallRyeConfigProviderResolver.instance().releaseConfig(ConfigProvider.getConfig());
MapBasedConfig.cleanup();
}

public boolean isAmqpConnectorReady(SeContainer container) {
HealthCenter health = container.getBeanManager().createInstance().select(HealthCenter.class).get();
return health.getReadiness().isOk();
}

public boolean isAmqpConnectorReady(AmqpConnector connector) {
return connector.getReadiness().isOk();
}

public boolean isAmqpConnectorAlive(SeContainer container) {
HealthCenter health = container.getBeanManager().createInstance().select(HealthCenter.class).get();
return health.getLiveness().isOk();
}

public boolean isAmqpConnectorAlive(AmqpConnector connector) {
return connector.getLiveness().isOk();
super.tearDown();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand All @@ -15,31 +17,24 @@
import org.junit.jupiter.api.Test;

import io.smallrye.config.SmallRyeConfigProviderResolver;
import io.smallrye.reactive.messaging.amqp.AmqpBrokerTestBase;
import io.smallrye.reactive.messaging.amqp.AmqpBrokerHolder;
import io.smallrye.reactive.messaging.amqp.AmqpConnector;
import io.smallrye.reactive.messaging.amqp.AmqpUsage;
import io.smallrye.reactive.messaging.amqp.ProducingBean;
import io.smallrye.reactive.messaging.test.common.config.MapBasedConfig;

public class AmqpSSLSinkCDISSLContextTest extends AmqpBrokerTestBase {
public class AmqpSSLSinkCDISSLContextTest extends AmqpBrokerHolder {

private WeldContainer container;
private AmqpConnector provider;

@BeforeAll
public static void startBroker() {
try {
String brokerXml = SSLBrokerConfigUtil.createSecuredBrokerXml();
System.setProperty(BROKER_XML_LOCATION, brokerXml);
AmqpBrokerTestBase.startBroker();
} catch (Exception e) {
throw new RuntimeException(e);
}
public static void startBroker() throws IOException, URISyntaxException {
startBroker(SSLBrokerConfigUtil.createSecuredBrokerXml());
}

@AfterAll
public static void clearSslBrokerName() {
System.clearProperty(BROKER_XML_LOCATION);
public static void stopBroker() {
AmqpBrokerHolder.stopBroker();
}

@Override
Expand All @@ -51,12 +46,14 @@ public void setup() {
usage = new AmqpUsage(executionHolder.vertx(), host, port + 1, username, password);
}

@Override
@AfterEach
public void cleanup() {
if (provider != null) {
provider.terminate(null);
}
public void tearDown() {
super.tearDown();
}

@AfterEach
public void cleanup() {
if (container != null) {
container.shutdown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
Expand All @@ -15,35 +17,29 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.smallrye.config.SmallRyeConfigProviderResolver;
import io.smallrye.reactive.messaging.amqp.AmqpBrokerTestBase;
import io.smallrye.reactive.messaging.amqp.AmqpBrokerHolder;
import io.smallrye.reactive.messaging.amqp.AmqpConnector;
import io.smallrye.reactive.messaging.amqp.AmqpUsage;
import io.smallrye.reactive.messaging.amqp.ConsumptionBean;
import io.smallrye.reactive.messaging.test.common.config.MapBasedConfig;

public class AmqpSSLSourceCDISSLContextTest extends AmqpBrokerTestBase {

private AmqpConnector provider;
@Disabled
public class AmqpSSLSourceCDISSLContextTest extends AmqpBrokerHolder {

private WeldContainer container;

@BeforeAll
public static void startBroker() {
try {
String brokerXml = SSLBrokerConfigUtil.createSecuredBrokerXml();
System.setProperty(BROKER_XML_LOCATION, brokerXml);
AmqpBrokerTestBase.startBroker();
} catch (Exception e) {
throw new RuntimeException(e);
}
public static void startBroker() throws IOException, URISyntaxException {
startBroker(SSLBrokerConfigUtil.createSecuredBrokerXml());
}

@AfterAll
public static void clearSslBrokerName() {
System.clearProperty(BROKER_XML_LOCATION);
public static void stopBroker() {
AmqpBrokerHolder.stopBroker();
}

@Override
Expand All @@ -57,10 +53,6 @@ public void setup() {

@AfterEach
public void cleanup() {
if (provider != null) {
provider.terminate(null);
}

if (container != null) {
container.shutdown();
}
Expand Down

0 comments on commit efd103c

Please sign in to comment.