From abf7680940d9965011660dc432d44e002d17c1d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Sun, 2 Jul 2017 21:04:08 +0200 Subject: [PATCH] fix: sometimes various testcases fail 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 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/club/wpia/gigi/testUtils/ManagedTest.java b/tests/club/wpia/gigi/testUtils/ManagedTest.java index fd602b83..3eeda2ca 100644 --- a/tests/club/wpia/gigi/testUtils/ManagedTest.java +++ b/tests/club/wpia/gigi/testUtils/ManagedTest.java @@ -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) { -- 2.39.2