]> WPIA git - gigi.git/commitdiff
fix: sometimes various testcases fail
authorFelix Dörre <felix@dogcraft.de>
Sun, 2 Jul 2017 19:04:08 +0000 (21:04 +0200)
committerFelix Dörre <felix@dogcraft.de>
Wed, 5 Jul 2017 17:51:29 +0000 (19:51 +0200)
Sometimes TestCases like club.wpia.gigi.pages.admin
TestSEAdminPageUserMailSearch fail e.g. with CSRFExceptions. I debugged
this issue and have come to a conclusion:

Java's URL connection does not necessarily behave correctly, when
"keep-alive" logic and connection re-use is enabled: When a connection
is re-used and the request sometimes fails on the re-used connection
Java re-attempts to send the request. This might not be a good idea for
POST requests. Especially this is not a good idea, as we track forms
very strictly:
When a form gets requested, the instance is stored in the user's session
and can be submitted exactly once. When this POST request is repeated
the form has been submitted and a subsequent request will cause a
CSRFException. Disabling the retry-logic
is possible but also not a good alternative, as it also causes sporadic
POST-fails, which occur far more often that the retry-logic breaks a
Test Case.

The solution I chose ensures to close all HTTP-connections. Therefore
all HTTP requests use the same connection. This prevents timeouts
and does not cause POST retries. In particular I found problems with the
connection done to clear all caches. Using this change I observe that
all requests are made with the same connection and 100 subsequent
executions don't fail.

Change-Id: I1b4ebce92a431e5d35a81cecd8670b8c37eef9e1

tests/club/wpia/gigi/testUtils/ManagedTest.java

index fd602b8302750d2ad4de834182f8863097254f90..3eeda2ca705b7df263a4843f45c41c04e51be71d 100644 (file)
@@ -101,11 +101,11 @@ public class ManagedTest extends ConfiguredTest {
                 return mainProps;
             }
             inited = true;
+            url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort.https");
             purgeDatabase();
             String type = testProps.getProperty("type");
             generateMainProps(mainProps);
             if (type.equals("local")) {
-                url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort.https");
                 String[] parts = testProps.getProperty("mail").split(":", 2);
                 ter = new TestEmailReceiver(new InetSocketAddress(parts[0], Integer.parseInt(parts[1])));
                 ter.start();
@@ -114,7 +114,6 @@ public class ManagedTest extends ConfiguredTest {
                 }
                 return mainProps;
             }
-            url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort.https");
             gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
             DataOutputStream toGigi = new DataOutputStream(gigi.getOutputStream());
             System.out.println("... starting server");
@@ -168,14 +167,18 @@ public class ManagedTest extends ConfiguredTest {
 
     public static void purgeDatabase() throws SQLException, IOException {
         purgeOnlyDB();
-        clearCaches();
+        if (gigi != null) {
+            clearCaches();
+        }
     }
 
     public static void clearCaches() throws IOException {
         ObjectCache.clearAllCaches();
         // String type = testProps.getProperty("type");
         URL u = new URL("https://" + getServerName() + "/manage");
-        u.openConnection().getHeaderField("Location");
+        URLConnection connection = u.openConnection();
+        connection.getHeaderField("Location");
+        connection.getInputStream().close();
     }
 
     private static void generateMainProps(Properties mainProps) {