Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'tsartarzan/JENKINS-15156' into JENKINS-…
  • Loading branch information
johnou committed Feb 6, 2013
2 parents 92d87f0 + 2feb19e commit 8f62901
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
8 changes: 7 additions & 1 deletion core/src/main/java/hudson/model/AbstractProject.java
Expand Up @@ -162,7 +162,7 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A
* {@link Run#getPreviousBuild()}
*/
@Restricted(NoExternalUse.class)
protected transient RunMap<R> builds = new RunMap<R>();
protected transient RunMap<R> builds;

/**
* The quiet period. Null to delegate to the system default.
Expand Down Expand Up @@ -271,6 +271,12 @@ public void onCreatedFromScratch() {
super.onCreatedFromScratch();
// solicit initial contributions, especially from TransientProjectActionFactory
updateTransientActions();
assert builds==null;
builds = new RunMap<R>(getBuildDir(), new Constructor<R>() {
public R create(File dir) throws IOException {
return loadBuild(dir);
}
});
}

@Override
Expand Down
Expand Up @@ -203,8 +203,8 @@ protected void initBaseDir(File dir) {
private void loadIdOnDisk() {
String[] buildDirs = dir.list(createDirectoryFilter());
if (buildDirs==null) {
// the job may just have been created
buildDirs=EMPTY_STRING_ARRAY;
LOGGER.log(Level.WARNING, "failed to load list of builds from {0}", dir);
}
// wrap into ArrayList to enable mutation
Arrays.sort(buildDirs);
Expand Down Expand Up @@ -625,6 +625,7 @@ protected R load(int n, Index editInPlace) {


protected R load(String id, Index editInPlace) {
assert dir != null;
R v = load(new File(dir, id), editInPlace);
if (v==null && editInPlace!=null) {
// remember the failure.
Expand Down
46 changes: 46 additions & 0 deletions test/src/test/java/hudson/model/AbstractProjectTest.java
Expand Up @@ -47,6 +47,9 @@
import java.io.File;
import java.util.concurrent.Future;
import org.apache.commons.io.FileUtils;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

/**
* @author Kohsuke Kawaguchi
Expand Down Expand Up @@ -280,4 +283,47 @@ public void testSymlinkForPostBuildFailure() throws Exception {
assertSymlinkForBuild(lastSuccessful, 1);
assertSymlinkForBuild(lastStable, 1);
}

// Force garbage collection of ref's referent. Taken from NetBeans code.
public static void forceGc(final Reference<?> ref) {
ArrayList<byte[]> alloc = new ArrayList<byte[]>();
int size = 100000;
System.out.print("forcing garbage collection...");
for (int i = 0; i < 50; i++) {
if (ref.get() == null) {
System.out.println("succeeded");
return;
}
try {
System.gc();
System.runFinalization();
} catch (OutOfMemoryError error) {
// OK
}
try {
alloc.add(new byte[size]);
size = (int)(((double)size) * 1.3);
} catch (OutOfMemoryError error) {
size = size / 2;
}
try {
if (i % 3 == 0) {
Thread.sleep(321);
}
} catch (InterruptedException t) {
// ignore
}
}
System.out.println("failed");
}

@Bug(15156)
public void testGetBuildAfterGc() throws Exception {
FreeStyleProject job = createFreeStyleProject();
job.scheduleBuild2(0, new Cause.UserCause()).get();
// If this fails to garbage-collect, the test run will not have any value, but it will
// at least not fail.
forceGc(new WeakReference(job.getLastBuild()));
assertTrue(job.getLastBuild() != null);
}
}

0 comments on commit 8f62901

Please sign in to comment.