From e5b0f7b67dd11a51537b51395c5d18e92f735977 Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Fri, 8 Jun 2012 17:43:57 +0200 Subject: [PATCH] SelfHoster: Wait until the server is really ready. * src/test/java/org/glom/web/server/SelfHoster.java selfHost(): Attempt the connection after starting the server, retrying a few times if necessary, so that the server is really ready already when we return from this method. The regular Glom code does this too because pg_ctl reports success too soon. --- ChangeLog | 10 ++++++++ src/test/java/org/glom/web/server/SelfHoster.java | 29 ++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index fa984d4..dc19cbc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,15 @@ 2012-06-08 Murray Cumming + SelfHoster: Wait until the server is really ready. + + * src/test/java/org/glom/web/server/SelfHoster.java + selfHost(): Attempt the connection after starting the server, retrying + a few times if necessary, so that the server is really ready already when + we return from this method. + The regular Glom code does this too because pg_ctl reports success too soon. + +2012-06-08 Murray Cumming + ConfiguredDocument: Do not add a primary key to portals each time. * src/main/java/org/glom/web/server/ConfiguredDocument.java diff --git a/src/test/java/org/glom/web/server/SelfHoster.java b/src/test/java/org/glom/web/server/SelfHoster.java index eab5d5d..63ef042 100644 --- a/src/test/java/org/glom/web/server/SelfHoster.java +++ b/src/test/java/org/glom/web/server/SelfHoster.java @@ -277,7 +277,32 @@ public class SelfHoster { // Remember the port for later: document.setConnectionPort(availablePort); - return true; // STARTUPERROR_NONE; + //Check that we can really connect: + + //pg_ctl sometimes reports success before it is really ready to let us connect, + //so in this case we can just keep trying until it works, for a while: + for(int i = 0; i < 10; i++) { + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + final String dbName = document.getConnectionDatabase(); + document.setConnectionDatabase(""); //We have not created the database yet. + final Connection connection = createConnection(); + document.setConnectionDatabase(dbName); + if(connection != null) { + return true; // STARTUPERROR_NONE; + } + + System.out.println("selfHost(): Waiting and retrying the connection due to suspected too-early success of pg_ctl. retries=" + i); + } + + System.out.println("selfHost(): Test connection failed after multiple retries."); + return false; } /** @@ -348,6 +373,7 @@ public class SelfHoster { String output = ""; String line; try { + //TODO: readLine() can hang, waiting for an end of line that never comes. while ((line = br.readLine()) != null) { output += line + "\n"; } @@ -405,6 +431,7 @@ public class SelfHoster { String output = ""; String line; try { + //TODO: readLine() can hang, waiting for an end of line that never comes. while ((line = br.readLine()) != null) { output += line + "\n"; System.out.println(line); -- 2.1.4