Jenkins builds are randomly failing in the junits for the dmaap simulator.
From the console output, it appears that the simulator is being shut down
before all of the test methods have executed. Moved the code for
managing the "Main" object from the common superclass to the only
subclass that needed it.
Issue-ID: POLICY-2749
Change-Id: I2615bbef9b790dbb633ed7f123aeb3f880826862
Signed-off-by: Jim Hahn <jrh3@att.com>
import javax.ws.rs.core.MediaType;
import lombok.Getter;
import org.glassfish.jersey.client.ClientProperties;
-import org.junit.AfterClass;
-import org.junit.Before;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
import org.onap.policy.common.utils.network.NetworkUtil;
-import org.onap.policy.common.utils.services.Registry;
-import org.onap.policy.models.sim.dmaap.DmaapSimException;
-import org.onap.policy.models.sim.dmaap.startstop.Main;
import org.onap.policy.sim.dmaap.parameters.CommonTestData;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* Common base class for rest server tests.
*/
public class CommonRestServer {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(CommonRestServer.class);
-
public static final String NOT_ALIVE = "not alive";
public static final String ALIVE = "alive";
public static final String SELF = "self";
protected static String httpPrefix;
- private static Main main;
-
/**
- * Allocates a port for the server, writes a config file, and then starts Main.
- *
- * @param shouldStartMain {@code true} if Main should be started, {@code false}
- * otherwise
+ * Allocates a port for the server, writes a config file.
*
* @throws Exception if an error occurs
*/
- public static void reconfigure(boolean shouldStartMain) throws Exception {
+ public static void reconfigure() throws Exception {
port = NetworkUtil.allocPort();
httpPrefix = "http://localhost:" + port + "/";
makeConfigFile(CONFIG_FILE, json);
HttpServletServerFactoryInstance.getServerFactory().destroy();
-
- if (shouldStartMain) {
- startMain();
- }
- }
-
- /**
- * Stops Main.
- */
- @AfterClass
- public static void teardownAfterClass() {
- try {
- if (main != null) {
- Main main2 = main;
- main = null;
-
- main2.shutdown();
- }
-
- } catch (DmaapSimException exp) {
- LOGGER.error("cannot stop main", exp);
- }
- }
-
- /**
- * Set up.
- *
- * @throws Exception if an error occurs
- */
- @Before
- public void setUp() throws Exception {
- // restart, if not currently running
- if (main == null) {
- startMain();
- }
}
/**
}
}
- /**
- * Starts the "Main".
- *
- * @throws Exception if an error occurs
- */
- private static void startMain() throws Exception {
- Registry.newRegistry();
-
- // make sure port is available
- if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) {
- throw new IllegalStateException("port " + port + " is still in use");
- }
-
- final String[] simConfigParameters = {"-c", "src/test/resources/parameters/TestConfigParams.json"};
-
- main = new Main(simConfigParameters);
-
- if (!NetworkUtil.isTcpPortOpen("localhost", port, 60, 1000L)) {
- throw new IllegalStateException("server is not listening on port " + port);
- }
- }
-
/**
* Sends a request to an endpoint.
*
import org.onap.policy.common.endpoints.parameters.TopicParameters;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.network.NetworkUtil;
+import org.onap.policy.common.utils.services.Registry;
+import org.onap.policy.models.sim.dmaap.DmaapSimException;
import org.onap.policy.models.sim.dmaap.rest.CommonRestServer;
+import org.onap.policy.models.sim.dmaap.startstop.Main;
/**
* This tests the simulator using dmaap endpoints to verify that it works from publisher
private static final String TOPIC2 = "MY-TOPIC-B";
private static final int MAX_MSG = 200;
+ private static Main main;
+
/**
* Messages from the topic are placed here by the endpoint.
*/
public static void setUpBeforeClass() throws Exception {
TopicEndpointManager.getManager().shutdown();
- CommonRestServer.reconfigure(true);
+ CommonRestServer.reconfigure();
+
+ startMain();
queue = new LinkedBlockingQueue<>();
queue2 = new LinkedBlockingQueue<>();
* Stops the topics and clears the queues.
*/
@AfterClass
- public static void tearDownAfterClass() {
+ public static void tearDownAfterClass() throws DmaapSimException {
TopicEndpointManager.getManager().shutdown();
queue = null;
queue2 = null;
+
+ main.shutdown();
}
/**
* @throws CoderException if the parameters cannot be decoded
*/
@Before
- @Override
- public void setUp() throws CoderException {
+ public void setUp() {
queue.clear();
queue2.clear();
}
assertEquals(testName + " message 1", msg1, queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));
assertEquals(testName + " message 2", msg2, queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));
}
+
+ /**
+ * Starts the "Main".
+ *
+ * @throws Exception if an error occurs
+ */
+ private static void startMain() throws Exception {
+ Registry.newRegistry();
+
+ int port = CommonRestServer.getPort();
+
+ // make sure port is available
+ if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) {
+ throw new IllegalStateException("port " + port + " is still in use");
+ }
+
+ final String[] simConfigParameters = {"-c", "src/test/resources/parameters/TestConfigParams.json"};
+
+ main = new Main(simConfigParameters);
+
+ if (!NetworkUtil.isTcpPortOpen("localhost", port, 300, 200L)) {
+ throw new IllegalStateException("server is not listening on port " + port);
+ }
+ }
}
Registry.newRegistry();
HttpServletServerFactoryInstance.getServerFactory().destroy();
- CommonRestServer.reconfigure(false);
+ CommonRestServer.reconfigure();
final String[] papConfigParameters = {"-c", CONFIG_FILE};
final DmaapSimCommandLineArguments arguments = new DmaapSimCommandLineArguments(papConfigParameters);
private Main main;
/**
- * Set up.
+ * Sets up.
*/
@Before
public void setUp() {
@Test
public void testMain() throws Exception {
- CommonRestServer.reconfigure(false);
+ CommonRestServer.reconfigure();
final String[] NormalParameters = {"-c", CONFIG_FILE};
main = new Main(NormalParameters);
assertTrue(main.getParameters().isValid());
@Test
public void testMain_InvalidArguments() throws Exception {
- CommonRestServer.reconfigure(false);
+ CommonRestServer.reconfigure();
// note: this is missing the "-c" argument, thus the ARGUMENTS are invalid
final String[] NormalParameters = {CONFIG_FILE};