Skip to content

Commit

Permalink
DerbyController: hopefully fix for getConnection() + test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Aug 9, 2022
1 parent 70eb34a commit a9d1c72
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
package com.evolveum.midpoint.test.util;

import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;

import org.apache.derby.drda.NetworkServerControl;
import org.testng.TestException;

import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
Expand Down Expand Up @@ -72,10 +70,14 @@ public String getPassword() {
public Connection getConnection() throws SQLException {
try {
return DriverManager.getConnection(jdbcUrl, "", "");
} catch (Exception e) {
// Adding more info for the occasional problem with TestDBTable on k8s:
throw new SQLException("DerbyController.getConnection error: " + e + "\n"
+ "Current state: " + this, e);
} catch (SQLTransientException e) {
try {
// Let's try one more time after a second.
Thread.sleep(1000L);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
return DriverManager.getConnection(jdbcUrl, "", "");
}
}

Expand Down Expand Up @@ -109,32 +111,30 @@ public void start() throws Exception {
System.setProperty("derby.stream.error.file", "target/derby.log");
server.start(null);
boolean dbServerOk = false;
int maxPings = 600; // times 100 ms lower => 1 min max
do {
try {
Thread.sleep(100);
server.ping();
dbServerOk = true;
// There is slight possibility that getConnection() still times out if called immediately.
Thread.sleep(50);
return; // OK, started, let's go out of the method
} catch (Exception e) {
LOGGER.debug("Derby embedded network server not ready yet...");
}
} while (!dbServerOk);

maxPings -= 1;
} while (!dbServerOk && maxPings > 0);

// Negative scenario, we ran out of maxPings attempts.
LOGGER.warn("Derby DB did not start on time, trying to shut it down.");
stop();
throw new TestException("Derby DB did not start on time");
}

public void stop() throws Exception {
LOGGER.info("Stopping Derby embedded network server");
server.shutdown();
}

@Override
public String toString() {
return "DerbyController{" +
"server=" + server +
", listenHostname='" + listenHostname + '\'' +
", listenPort=" + listenPort +
", jdbcUrl='" + jdbcUrl + '\'' +
", dbName='" + dbName + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2015 Evolveum and contributors
* Copyright (C) 2010-2022 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
Expand Down Expand Up @@ -47,7 +47,7 @@ public void testStartStopDerby() throws Exception {
conn.commit();

// Try to connect over the "network" (localhost)
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Class.forName("org.apache.derby.jdbc.ClientDriver").getDeclaredConstructor().newInstance();
String networkJdbcUrl = "jdbc:derby://" + controller.getListenHostname() + ":" + controller.getListenPort() + "/" + controller.getDbName();
Properties props = new Properties();
props.setProperty("user", controller.getUsername());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
import java.sql.ResultSet;
import java.sql.Statement;

import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.CapabilityCollectionType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.util.PrismAsserts;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
Expand All @@ -40,6 +37,7 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.CapabilityCollectionType;
import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.CredentialsCapabilityType;
import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.PasswordCapabilityType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;
Expand Down Expand Up @@ -155,7 +153,7 @@ public void test002AddAccount() throws Exception {
// Check database content

Connection conn = derbyController.getConnection();
// Check if it empty
// Check if empty
Statement stmt = conn.createStatement();
stmt.execute("select * from users");
ResultSet rs = stmt.getResultSet();
Expand Down

0 comments on commit a9d1c72

Please sign in to comment.