Skip to content

Commit

Permalink
[JENKINS-22395] Taking advantage of BuildReference.clear (just introd…
Browse files Browse the repository at this point in the history
…uced in b6226ad) to simplify test by not requiring a custom build reference holder just to simulate GC.

Confirmed that dropLinksAfterGC and dropLinksAfterGC2 both fail in the expected way (b1a.nextBuild == b2) after commenting out the call to createReference().clear() in dropLinks.
(Also that they fail as expected in assertNotSame if the reference is not cleared at all.)
  • Loading branch information
jglick committed Jul 11, 2014
1 parent aa8e0b4 commit b7ec857
Showing 1 changed file with 3 additions and 43 deletions.
46 changes: 3 additions & 43 deletions test/src/test/java/jenkins/model/lazy/LazyBuildMixInTest.java
Expand Up @@ -26,19 +26,14 @@

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;

import hudson.model.listeners.RunListener;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.SleepBuilder;
import org.jvnet.hudson.test.TestExtension;

public class LazyBuildMixInTest {

Expand All @@ -58,7 +53,7 @@ public class LazyBuildMixInTest {
assertEquals(null, b1.getPreviousBuild());
assertEquals(b1, b2.getPreviousBuild());
assertEquals(b2, b3.getPreviousBuild());
assertEquals(1, BRHF.drop(b1));
b1.getRunMixIn().createReference().clear();
b2.delete();
FreeStyleBuild b1a = b2.getPreviousBuild();
assertNotSame(b1, b1a);
Expand All @@ -81,48 +76,13 @@ public class LazyBuildMixInTest {
assertEquals(b1, b2.getPreviousBuild());
assertEquals(b2, b3.getPreviousBuild());
b2.delete();
assertEquals(1, BRHF.drop(b1));
b1.getRunMixIn().createReference().clear();
FreeStyleBuild b1a = b2.getPreviousBuild();
assertNotSame(b1, b1a);
assertEquals(1, b1a.getNumber());
assertEquals(b3, b1a.getNextBuild());
}

/**
* Unlike the standard {@link SoftReference} this lets us simulate a referent disappearing at a specific time.
*/
@TestExtension public static final class BRHF implements BuildReference.HolderFactory {
private static final List<BRH<?>> refs = new ArrayList<BRH<?>>();
private static final class BRH<R> implements BuildReference.Holder<R> {
R r;
BRH(R r) {this.r = r;}
@Override public R get() {return r;}
}
@Override public <R> BuildReference.Holder<R> make(R referent) {
BRH<R> ref = new BRH<R>(referent);
synchronized (refs) {
refs.add(ref);
}
return ref;
}
/**
* Simulates garbage collection of a referent.
* @return how many build references went null as a result
*/
static int drop(Object o) {
int count = 0;
synchronized (refs) {
for (BRH<?> ref : refs) {
if (ref.r == o) {
ref.r = null;
count++;
}
}
}
return count;
}
}

@Bug(20662)
@Test public void newRunningBuildRelationFromPrevious() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
Expand Down

0 comments on commit b7ec857

Please sign in to comment.