From b7ec8578f938864af9c8dbfb73a54d67f972d694 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Fri, 11 Jul 2014 10:55:51 -0400 Subject: [PATCH] [JENKINS-22395] Taking advantage of BuildReference.clear (just introduced 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.) --- .../model/lazy/LazyBuildMixInTest.java | 46 ++----------------- 1 file changed, 3 insertions(+), 43 deletions(-) diff --git a/test/src/test/java/jenkins/model/lazy/LazyBuildMixInTest.java b/test/src/test/java/jenkins/model/lazy/LazyBuildMixInTest.java index 36b3890ee5..ce6dee7b36 100644 --- a/test/src/test/java/jenkins/model/lazy/LazyBuildMixInTest.java +++ b/test/src/test/java/jenkins/model/lazy/LazyBuildMixInTest.java @@ -26,19 +26,14 @@ package jenkins.model.lazy; 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 { @@ -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); @@ -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> refs = new ArrayList>(); - private static final class BRH implements BuildReference.Holder { - R r; - BRH(R r) {this.r = r;} - @Override public R get() {return r;} - } - @Override public BuildReference.Holder make(R referent) { - BRH ref = new BRH(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(); -- GitLab