From 9f40917edec7ab48db77368074d7aba7ed1fdd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20Gond=C5=BEa?= Date: Wed, 1 Nov 2023 09:11:31 +0100 Subject: [PATCH] fix: Flakiness in ServerScopeTest#runScope --- .../compute/JCloudsCleanupThreadTest.java | 12 +++++-- .../openstack/compute/ServerScopeTest.java | 34 ++++++++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/plugin/src/test/java/jenkins/plugins/openstack/compute/JCloudsCleanupThreadTest.java b/plugin/src/test/java/jenkins/plugins/openstack/compute/JCloudsCleanupThreadTest.java index ea2d73a2..17beb9a9 100644 --- a/plugin/src/test/java/jenkins/plugins/openstack/compute/JCloudsCleanupThreadTest.java +++ b/plugin/src/test/java/jenkins/plugins/openstack/compute/JCloudsCleanupThreadTest.java @@ -41,9 +41,7 @@ import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; /** @@ -240,7 +238,7 @@ public void doNotTerminateNodeThatIsBeingProvisioned() throws Exception { assertThat(j.jenkins.getNodes(), Matchers.iterableWithSize(1)); } - private static class BuildBlocker extends TestBuilder { + public static class BuildBlocker extends TestBuilder { private final OneShotEvent enter = new OneShotEvent(); private final OneShotEvent exit = new OneShotEvent(); @@ -250,5 +248,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen exit.block(); return true; } + + public void awaitStarted() throws InterruptedException { + enter.block(); + } + + public void signalDone() { + exit.signal(); + } } } diff --git a/plugin/src/test/java/jenkins/plugins/openstack/compute/ServerScopeTest.java b/plugin/src/test/java/jenkins/plugins/openstack/compute/ServerScopeTest.java index 7039797c..13cb55fa 100644 --- a/plugin/src/test/java/jenkins/plugins/openstack/compute/ServerScopeTest.java +++ b/plugin/src/test/java/jenkins/plugins/openstack/compute/ServerScopeTest.java @@ -9,7 +9,6 @@ import jenkins.util.Timer; import org.junit.Rule; import org.junit.Test; -import org.jvnet.hudson.test.SleepBuilder; import org.jvnet.hudson.test.WithoutJenkins; import org.openstack4j.model.compute.Server; @@ -113,20 +112,31 @@ public void avoidRunningOutOfScopeDuringProvisioning() throws Exception { @Test public void runScope() throws Exception { FreeStyleProject asdf = j.createFreeStyleProject("asdf"); - asdf.getBuildersList().add(new SleepBuilder(1000000)); + JCloudsCleanupThreadTest.BuildBlocker bb = new JCloudsCleanupThreadTest.BuildBlocker(); + asdf.getBuildersList().add(bb); FreeStyleBuild build = asdf.scheduleBuild2(0).waitForStart(); - ServerScope.Build alive = new ServerScope.Build(build); - assertFalse(alive.isOutOfScope(mockServer)); - assertEquals("run:asdf:1", alive.getValue()); - - ServerScope.Build rotated = new ServerScope.Build("asdf:42"); - assertTrue(rotated.isOutOfScope(mockServer)); - assertEquals("run:asdf:42", rotated.getValue()); + bb.awaitStarted(); + try { + + ServerScope.Build alive = new ServerScope.Build(build); + assertFalse(alive.isOutOfScope(mockServer)); + assertEquals("run:asdf:1", alive.getValue()); + + ServerScope.Build rotated = new ServerScope.Build("asdf:42"); + assertTrue(rotated.isOutOfScope(mockServer)); + assertEquals("run:asdf:42", rotated.getValue()); + + ServerScope.Build jobGone = new ServerScope.Build("nonono:1"); + assertTrue(jobGone.isOutOfScope(mockServer)); + assertEquals("run:nonono:1", jobGone.getValue()); + } finally { + // Make sure the build terminates before ending the test. + // Active build is causing test harness to fail when deleting log files + bb.signalDone(); + j.waitForCompletion(build); + } - ServerScope.Build jobGone = new ServerScope.Build("nonono:1"); - assertTrue(jobGone.isOutOfScope(mockServer)); - assertEquals("run:nonono:1", jobGone.getValue()); } @Test @WithoutJenkins