# HG changeset patch # User Zdenek Zambersky # Date 1507561740 -7200 # Node ID 0ec0aad214cbf565adf81b8d71e7d345f5f11272 # Parent d7a5efac202cc007dfe52a07866c2618aa8a0df6 tests: reworked mongodb start and stop wait conditions diff -r d7a5efac202c -r 0ec0aad214cb tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/MongoIntegrationTest.java --- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/MongoIntegrationTest.java Tue Oct 10 08:17:12 2017 -0700 +++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/MongoIntegrationTest.java Mon Oct 09 17:09:00 2017 +0200 @@ -62,9 +62,6 @@ @BeforeClass public static void beforeClassMongoIntegrationTest() throws Exception { mongodTestUtil.startMongod(); - if (!mongodTestUtil.isConnectedToDatabase()) { - fail("Unable to start mongodb database, port in use"); - } setupMongoCredentials(); } diff -r d7a5efac202c -r 0ec0aad214cb tests/keycloak-integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/keycloak/MongoKeycloakIntegrationTest.java --- a/tests/keycloak-integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/keycloak/MongoKeycloakIntegrationTest.java Tue Oct 10 08:17:12 2017 -0700 +++ b/tests/keycloak-integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/keycloak/MongoKeycloakIntegrationTest.java Mon Oct 09 17:09:00 2017 +0200 @@ -63,9 +63,6 @@ @BeforeClass public static void beforeClassMongoIntegrationTest() throws Exception { mongodTestUtil.startMongod(); - if (!mongodTestUtil.isConnectedToDatabase()) { - fail("Unable to start mongodb database, port in use"); - } setupMongoCredentials(); } diff -r d7a5efac202c -r 0ec0aad214cb tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/MongodTestUtil.java --- a/tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/MongodTestUtil.java Tue Oct 10 08:17:12 2017 -0700 +++ b/tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/MongodTestUtil.java Mon Oct 09 17:09:00 2017 +0200 @@ -50,6 +50,8 @@ import com.mongodb.client.MongoCollection; import com.redhat.thermostat.gateway.common.util.OS; + +import java.net.Socket; import java.net.ServerSocket; import java.net.SocketAddress; import java.net.InetSocketAddress; @@ -57,7 +59,7 @@ public class MongodTestUtil { private static final int WAIT_FOR_MAX_ITERATIONS = 100; - private static final long WAIT_FOR_SLEEP_DURATION = 100L; + private static final long WAIT_FOR_SLEEP_DURATION = 250L; private static final int DEFAULT_PORT = 27519; private final String databaseName = "thermostat"; @@ -91,24 +93,24 @@ String[] windowsCommand = {"cmd", "/c", "mongod", "--dbpath", tempDbDir.resolve("data/db").toAbsolutePath().toString(), "--port", String.valueOf(port), "--logpath", tempLogFile.toAbsolutePath().toString()}; ProcessBuilder builder = new ProcessBuilder(OS.IS_UNIX ? posixCommand : windowsCommand); process = builder.start(); + waitForSocketToBind(host, port); mongoClient = new MongoClient(new ServerAddress(host, port)); - connectedToDatabase = waitForMongodStart(); } public void stopMongod() throws IOException, InterruptedException { - if (connectedToDatabase) { + if (mongoClient != null) { try { mongoClient.getDatabase("admin").runCommand(new Document("shutdown", 1)); } catch (Exception ignored) { /* following exception is always thrown: com.mongodb.MongoSocketReadException: Prematurely reached end of stream - ( probably expected, connection get closed? ) + ( probably expected, connection gets closed? ) */ + } finally { + mongoClient.close(); + mongoClient = null; } - mongoClient.close(); - mongoClient = null; - waitForMongodStop(); waitForSocketToClose(port); finish(); } @@ -140,49 +142,19 @@ }); } - private boolean waitForMongodStart() throws IOException, InterruptedException { - return waitFor("waiting for connections on port", "addr already in use"); - } - - private boolean waitForMongodStop() throws IOException, InterruptedException { - // dbexit string is not logged by newer mongodb (>= 3.4 ?) - return waitFor("dbexit: rc: 0"); - } - - private boolean waitFor(String match) throws IOException, InterruptedException { - return waitFor(match, ""); - } - - /** - * Keeps checking the temporary log file to see if any of the desired - * matches are found in the file. This allows short-circuiting of checking - * the files in the event some error happens (like being unable to connect - * to the database) so it does not keep cycling for a long period of time. - * @param desiredMatch The string to find, returns true if found. Null - * should not be passed to this argument. - * @param errorMatch An error string to return false on finding. An empty - * string should be used if no error match is needed (it - * is better to call waitFor(desiredMatch) instead). Null - * should not be used. - * @return True if any of the desired matches were found, false if an error - * match was found or it timed out. - */ - private boolean waitFor(String desiredMatch, String errorMatch) throws IOException, InterruptedException { - for (int i = 0; i < WAIT_FOR_MAX_ITERATIONS; i++) { - if (Files.exists(tempLogFile)) { - String logFileText = new String(Files.readAllBytes(tempLogFile)); - if (logFileText.contains(desiredMatch)) { - return true; - } - if (!"".equals(errorMatch) && logFileText.contains(errorMatch)) { - return false; - } + private void waitForSocketToBind(String host, int port) throws InterruptedException { + for (int i = 0; i < WAIT_FOR_MAX_ITERATIONS; ++i) { + /* Try to connect to socket, if it fails, port is not bound yet */ + try (Socket socket = new Socket()) { + SocketAddress address = new InetSocketAddress(host, port); + socket.connect(address); + return; + } catch (IOException e) { + /* ignored */ } - Thread.sleep(WAIT_FOR_SLEEP_DURATION); } - - return false; + throw new RuntimeException("Timeout waiting for mongodb socket to bind reached!"); } private void waitForSocketToClose(int port) throws InterruptedException { @@ -200,8 +172,4 @@ throw new RuntimeException("Timeout waiting for mongodb socket to close reached!"); } - public boolean isConnectedToDatabase() { - return connectedToDatabase; - } - } \ No newline at end of file