Skip to content

Commit

Permalink
[FIXED JENKINS-4409] Disable URLConnection.useCache in tests on Windo…
Browse files Browse the repository at this point in the history
…ws. It prevents deleting temporary directories when tests finish.

Originally-Committed-As: c4e5fbdbc9c644f1afcae031b3b4638ad5297238
  • Loading branch information
ikedam committed Feb 14, 2015
1 parent 9e3aa98 commit cb5bb38
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java
Expand Up @@ -101,6 +101,7 @@
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.net.URLConnection;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
Expand Down Expand Up @@ -276,6 +277,8 @@ public abstract class HudsonTestCase extends TestCase implements RootAction {
*/ */
protected File explodedWarDir; protected File explodedWarDir;


private boolean origDefaultUseCache = true;

protected HudsonTestCase(String name) { protected HudsonTestCase(String name) {
super(name); super(name);
} }
Expand All @@ -298,6 +301,18 @@ public void runBare() throws Throwable {


@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
if(Functions.isWindows()) {
// JENKINS-4409.
// URLConnection caches handles to jar files by default,
// and it prevents delete temporary directories on Windows.
// Disables caching here.
// Though defaultUseCache is a static field,
// its setter and getter are provided as instance methods.
URLConnection aConnection = new File(".").toURI().toURL().openConnection();
origDefaultUseCache = aConnection.getDefaultUseCaches();
aConnection.setDefaultUseCaches(false);
}

env.pin(); env.pin();
recipe(); recipe();
for (Runner r : recipes) { for (Runner r : recipes) {
Expand Down Expand Up @@ -420,6 +435,12 @@ protected void tearDown() throws Exception {
// at some later point, leading to possible file descriptor overflow. So encourage GC now. // at some later point, leading to possible file descriptor overflow. So encourage GC now.
// see http://bugs.sun.com/view_bug.do?bug_id=4950148 // see http://bugs.sun.com/view_bug.do?bug_id=4950148
System.gc(); System.gc();

// restore defaultUseCache
if(Functions.isWindows()) {
URLConnection aConnection = new File(".").toURI().toURL().openConnection();
aConnection.setDefaultUseCaches(origDefaultUseCache);
}
} }
} }


Expand Down
21 changes: 21 additions & 0 deletions test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java
Expand Up @@ -128,6 +128,7 @@
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.net.URLConnection;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
Expand Down Expand Up @@ -303,6 +304,8 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction {


public JenkinsComputerConnectorTester computerConnectorTester = new JenkinsComputerConnectorTester(this); public JenkinsComputerConnectorTester computerConnectorTester = new JenkinsComputerConnectorTester(this);


private boolean origDefaultUseCache = true;

public Jenkins getInstance() { public Jenkins getInstance() {
return jenkins; return jenkins;
} }
Expand All @@ -312,6 +315,18 @@ public Jenkins getInstance() {
* @throws Throwable if setup fails (which will disable {@code after} * @throws Throwable if setup fails (which will disable {@code after}
*/ */
public void before() throws Throwable { public void before() throws Throwable {
if(Functions.isWindows()) {
// JENKINS-4409.
// URLConnection caches handles to jar files by default,
// and it prevents delete temporary directories on Windows.
// Disables caching here.
// Though defaultUseCache is a static field,
// its setter and getter are provided as instance methods.
URLConnection aConnection = new File(".").toURI().toURL().openConnection();
origDefaultUseCache = aConnection.getDefaultUseCaches();
aConnection.setDefaultUseCaches(false);
}

// Not ideal (https://github.com/junit-team/junit/issues/116) but basically works. // Not ideal (https://github.com/junit-team/junit/issues/116) but basically works.
if (Boolean.getBoolean("ignore.random.failures")) { if (Boolean.getBoolean("ignore.random.failures")) {
RandomlyFails rf = testDescription.getAnnotation(RandomlyFails.class); RandomlyFails rf = testDescription.getAnnotation(RandomlyFails.class);
Expand Down Expand Up @@ -458,6 +473,12 @@ public void after() throws Exception {
// see http://bugs.sun.com/view_bug.do?bug_id=4950148 // see http://bugs.sun.com/view_bug.do?bug_id=4950148
// TODO use URLClassLoader.close() in Java 7 // TODO use URLClassLoader.close() in Java 7
System.gc(); System.gc();

// restore defaultUseCache
if(Functions.isWindows()) {
URLConnection aConnection = new File(".").toURI().toURL().openConnection();
aConnection.setDefaultUseCaches(origDefaultUseCache);
}
} }
} }


Expand Down

0 comments on commit cb5bb38

Please sign in to comment.