Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PrepPipeline daemon thread shutdown #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableMap;

public class PreparedDbProvider
public class PreparedDbProvider implements AutoCloseable
{
private static final String JDBC_FORMAT = "jdbc:postgresql://localhost:%d/%s?user=%s";

Expand All @@ -50,6 +50,7 @@ public class PreparedDbProvider
private static final Map<ClusterKey, PrepPipeline> CLUSTERS = new HashMap<>();

private final PrepPipeline dbPreparer;
private final ClusterKey key;

public static PreparedDbProvider forPreparer(DatabasePreparer preparer) {
return forPreparer(preparer, Collections.emptyList());
Expand All @@ -61,7 +62,8 @@ public static PreparedDbProvider forPreparer(DatabasePreparer preparer, Iterable

private PreparedDbProvider(DatabasePreparer preparer, Iterable<Consumer<Builder>> customizers) {
try {
dbPreparer = createOrFindPreparer(preparer, customizers);
key = new ClusterKey(preparer, customizers);
dbPreparer = createOrFindPreparer(preparer, customizers, key);
} catch (final IOException | SQLException e) {
throw new RuntimeException(e);
}
Expand All @@ -71,9 +73,8 @@ private PreparedDbProvider(DatabasePreparer preparer, Iterable<Consumer<Builder>
* Each schema set has its own database cluster. The template1 database has the schema preloaded so that
* each test case need only create a new database and not re-invoke your preparer.
*/
private static synchronized PrepPipeline createOrFindPreparer(DatabasePreparer preparer, Iterable<Consumer<Builder>> customizers) throws IOException, SQLException
private static synchronized PrepPipeline createOrFindPreparer(DatabasePreparer preparer, Iterable<Consumer<Builder>> customizers, ClusterKey key) throws IOException, SQLException
{
final ClusterKey key = new ClusterKey(preparer, customizers);
PrepPipeline result = CLUSTERS.get(key);
if (result != null) {
return result;
Expand Down Expand Up @@ -164,14 +165,21 @@ public Map<String, String> getConfigurationTweak(String dbModuleName) throws SQL
return result;
}

@Override
public void close() throws IOException {
CLUSTERS.remove(key);
dbPreparer.close();
}

/**
* Spawns a background thread that prepares databases ahead of time for speed, and then uses a
* synchronous queue to hand the prepared databases off to test cases.
*/
private static class PrepPipeline implements Runnable
private static class PrepPipeline implements Runnable, AutoCloseable
{
private final EmbeddedPostgres pg;
private final SynchronousQueue<DbInfo> nextDatabase = new SynchronousQueue<DbInfo>();
private Thread runningThread = null;

PrepPipeline(EmbeddedPostgres pg)
{
Expand All @@ -184,6 +192,7 @@ PrepPipeline start()
final Thread t = new Thread(r);
t.setDaemon(true);
t.setName("cluster-" + pg + "-preparer");
runningThread = t;
return t;
});
service.submit(this);
Expand Down Expand Up @@ -228,6 +237,13 @@ public void run()
}
}
}

@Override
public void close() throws IOException {
if(runningThread != null){
runningThread.interrupt();
}
}
}

private static void create(final DataSource connectDb, final String dbName, final String userName) throws SQLException
Expand Down