Skip to content

Commit 9f40917

Browse files
committed
fix: Flakiness in ServerScopeTest#runScope
1 parent db68479 commit 9f40917

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

plugin/src/test/java/jenkins/plugins/openstack/compute/JCloudsCleanupThreadTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
import static org.mockito.Matchers.eq;
4242
import static org.mockito.Mockito.doThrow;
4343
import static org.mockito.Mockito.mock;
44-
import static org.mockito.Mockito.never;
4544
import static org.mockito.Mockito.verify;
46-
import static org.mockito.Mockito.verifyNoMoreInteractions;
4745
import static org.mockito.Mockito.when;
4846

4947
/**
@@ -240,7 +238,7 @@ public void doNotTerminateNodeThatIsBeingProvisioned() throws Exception {
240238
assertThat(j.jenkins.getNodes(), Matchers.iterableWithSize(1));
241239
}
242240

243-
private static class BuildBlocker extends TestBuilder {
241+
public static class BuildBlocker extends TestBuilder {
244242
private final OneShotEvent enter = new OneShotEvent();
245243
private final OneShotEvent exit = new OneShotEvent();
246244

@@ -250,5 +248,13 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
250248
exit.block();
251249
return true;
252250
}
251+
252+
public void awaitStarted() throws InterruptedException {
253+
enter.block();
254+
}
255+
256+
public void signalDone() {
257+
exit.signal();
258+
}
253259
}
254260
}

plugin/src/test/java/jenkins/plugins/openstack/compute/ServerScopeTest.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import jenkins.util.Timer;
1010
import org.junit.Rule;
1111
import org.junit.Test;
12-
import org.jvnet.hudson.test.SleepBuilder;
1312
import org.jvnet.hudson.test.WithoutJenkins;
1413
import org.openstack4j.model.compute.Server;
1514

@@ -113,20 +112,31 @@ public void avoidRunningOutOfScopeDuringProvisioning() throws Exception {
113112
@Test
114113
public void runScope() throws Exception {
115114
FreeStyleProject asdf = j.createFreeStyleProject("asdf");
116-
asdf.getBuildersList().add(new SleepBuilder(1000000));
115+
JCloudsCleanupThreadTest.BuildBlocker bb = new JCloudsCleanupThreadTest.BuildBlocker();
116+
asdf.getBuildersList().add(bb);
117117
FreeStyleBuild build = asdf.scheduleBuild2(0).waitForStart();
118118

119-
ServerScope.Build alive = new ServerScope.Build(build);
120-
assertFalse(alive.isOutOfScope(mockServer));
121-
assertEquals("run:asdf:1", alive.getValue());
122-
123-
ServerScope.Build rotated = new ServerScope.Build("asdf:42");
124-
assertTrue(rotated.isOutOfScope(mockServer));
125-
assertEquals("run:asdf:42", rotated.getValue());
119+
bb.awaitStarted();
120+
try {
121+
122+
ServerScope.Build alive = new ServerScope.Build(build);
123+
assertFalse(alive.isOutOfScope(mockServer));
124+
assertEquals("run:asdf:1", alive.getValue());
125+
126+
ServerScope.Build rotated = new ServerScope.Build("asdf:42");
127+
assertTrue(rotated.isOutOfScope(mockServer));
128+
assertEquals("run:asdf:42", rotated.getValue());
129+
130+
ServerScope.Build jobGone = new ServerScope.Build("nonono:1");
131+
assertTrue(jobGone.isOutOfScope(mockServer));
132+
assertEquals("run:nonono:1", jobGone.getValue());
133+
} finally {
134+
// Make sure the build terminates before ending the test.
135+
// Active build is causing test harness to fail when deleting log files
136+
bb.signalDone();
137+
j.waitForCompletion(build);
138+
}
126139

127-
ServerScope.Build jobGone = new ServerScope.Build("nonono:1");
128-
assertTrue(jobGone.isOutOfScope(mockServer));
129-
assertEquals("run:nonono:1", jobGone.getValue());
130140
}
131141

132142
@Test @WithoutJenkins

0 commit comments

Comments
 (0)