Skip to content

Commit

Permalink
fix: Flakiness in ServerScopeTest#runScope
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed Nov 1, 2023
1 parent db68479 commit 9f40917
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();

Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9f40917

Please sign in to comment.