Skip to content

Commit

Permalink
Flake in `QuietDownCommandTest.quietDownShouldSuccessWithBlockPlusNon…
Browse files Browse the repository at this point in the history
…ExpiredTimeoutAndRunningExecutor` (jenkinsci#8261)
  • Loading branch information
jglick authored Jul 14, 2023
1 parent 71a100d commit 0d9705b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 95 deletions.
41 changes: 18 additions & 23 deletions test/src/test/java/hudson/cli/CancelQuietDownCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.util.OneShotEvent;
import java.util.concurrent.Future;
import jenkins.model.Jenkins;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;

/**
Expand All @@ -50,6 +50,9 @@ public class CancelQuietDownCommandTest {

private CLICommandInvoker command;

@ClassRule
public static final BuildWatcher buildWatcher = new BuildWatcher();

@Rule
public final JenkinsRule j = new JenkinsRule();

Expand Down Expand Up @@ -110,28 +113,24 @@ public void cancelQuietDownShouldResetQuietReason() throws Exception {
public void cancelQuietDownShouldSuccessOnNoQuietDownedJenkinsAndRunningExecutor() throws Exception {
final FreeStyleProject project = j.createFreeStyleProject("aProject");
OneShotEvent finish = new OneShotEvent();
Future<FreeStyleBuild> build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish);
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
FreeStyleBuild build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish);

final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.invoke();
assertThat(result, succeededSilently());
QuietDownCommandTest.assertJenkinsNotInQuietMode(j);
finish.signal();
build.get();
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
assertThat(project.isBuilding(), equalTo(false));
j.waitForCompletion(build);
assertThat(build.isBuilding(), equalTo(false));
j.assertBuildStatusSuccess(build);
QuietDownCommandTest.assertJenkinsNotInQuietMode(j);
finish = new OneShotEvent();
build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish);
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(2));
assertThat(project.isBuilding(), equalTo(true));
assertThat(build.isBuilding(), equalTo(true));
finish.signal();
build.get();
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(2));
assertThat(project.isBuilding(), equalTo(false));
j.waitForCompletion(build);
assertThat(build.isBuilding(), equalTo(false));
j.assertBuildStatusSuccess(build);
QuietDownCommandTest.assertJenkinsNotInQuietMode(j);
}
Expand All @@ -144,9 +143,8 @@ public void cancelQuietDownShouldSuccessOnNoQuietDownedJenkinsAndRunningExecutor
public void cancelQuietDownShouldSuccessOnQuietDownedJenkinsAndRunningExecutor() throws Exception {
final FreeStyleProject project = j.createFreeStyleProject("aProject");
OneShotEvent finish = new OneShotEvent();
Future<FreeStyleBuild> build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish);
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
assertThat(project.isBuilding(), equalTo(true));
FreeStyleBuild build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish);
assertThat(build.isBuilding(), equalTo(true));
j.jenkins.doQuietDown();
QuietDownCommandTest.assertJenkinsInQuietMode(j);

Expand All @@ -156,19 +154,16 @@ public void cancelQuietDownShouldSuccessOnQuietDownedJenkinsAndRunningExecutor()
assertThat(result, succeededSilently());
QuietDownCommandTest.assertJenkinsNotInQuietMode(j);
finish.signal();
build.get();
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
assertThat(project.isBuilding(), equalTo(false));
j.waitForCompletion(build);
assertThat(build.isBuilding(), equalTo(false));
j.assertBuildStatusSuccess(build);
QuietDownCommandTest.assertJenkinsNotInQuietMode(j);
finish = new OneShotEvent();
build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish);
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(2));
assertThat(project.isBuilding(), equalTo(true));
assertThat(build.isBuilding(), equalTo(true));
finish.signal();
build.get();
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(2));
assertThat(project.isBuilding(), equalTo(false));
j.waitForCompletion(build);
assertThat(build.isBuilding(), equalTo(false));
j.assertBuildStatusSuccess(build);
QuietDownCommandTest.assertJenkinsNotInQuietMode(j);
}
Expand Down
29 changes: 13 additions & 16 deletions test/src/test/java/hudson/cli/OfflineNodeCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;

Expand All @@ -41,11 +40,12 @@
import hudson.slaves.DumbSlave;
import hudson.slaves.OfflineCause;
import hudson.util.OneShotEvent;
import java.util.concurrent.Future;
import jenkins.model.Jenkins;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.InboundAgentRule;
import org.jvnet.hudson.test.JenkinsRule;

Expand All @@ -56,6 +56,9 @@ public class OfflineNodeCommandTest {

private CLICommandInvoker command;

@ClassRule
public static final BuildWatcher buildWatcher = new BuildWatcher();

@Rule
public final JenkinsRule j = new JenkinsRule();

Expand Down Expand Up @@ -216,8 +219,7 @@ public void offlineNodeShouldSucceedOnBuildingNode() throws Exception {
slave.toComputer().waitUntilOnline();
FreeStyleProject project = j.createFreeStyleProject("aProject");
project.setAssignedNode(slave);
final Future<FreeStyleBuild> build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish);
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
final FreeStyleBuild build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish);

final CLICommandInvoker.Result result = command
.authorizedTo(Computer.DISCONNECT, Jenkins.READ)
Expand All @@ -228,13 +230,11 @@ public void offlineNodeShouldSucceedOnBuildingNode() throws Exception {
assertThat(slave.toComputer().isTemporarilyOffline(), equalTo(true));
assertThat(slave.toComputer().getOfflineCause(), instanceOf(OfflineCause.ByCLI.class));
assertThat(((OfflineCause.ByCLI) slave.toComputer().getOfflineCause()).message, equalTo(null));
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
assertThat(project.isBuilding(), equalTo(true));
assertThat(build.isBuilding(), equalTo(true));

finish.signal();
build.get();
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
assertThat(project.isBuilding(), equalTo(false));
j.waitForCompletion(build);
assertThat(build.isBuilding(), equalTo(false));
j.assertBuildStatusSuccess(build);
}

Expand All @@ -245,8 +245,7 @@ public void offlineNodeShouldSucceedOnBuildingNodeWithCause() throws Exception {
slave.toComputer().waitUntilOnline();
FreeStyleProject project = j.createFreeStyleProject("aProject");
project.setAssignedNode(slave);
final Future<FreeStyleBuild> build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish);
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
final FreeStyleBuild build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish);

final CLICommandInvoker.Result result = command
.authorizedTo(Computer.DISCONNECT, Jenkins.READ)
Expand All @@ -257,13 +256,11 @@ public void offlineNodeShouldSucceedOnBuildingNodeWithCause() throws Exception {
assertThat(slave.toComputer().isTemporarilyOffline(), equalTo(true));
assertThat(slave.toComputer().getOfflineCause(), instanceOf(OfflineCause.ByCLI.class));
assertThat(((OfflineCause.ByCLI) slave.toComputer().getOfflineCause()).message, equalTo("aCause"));
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
assertThat(project.isBuilding(), equalTo(true));
assertThat(build.isBuilding(), equalTo(true));

finish.signal();
build.get();
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
assertThat(project.isBuilding(), equalTo(false));
j.waitForCompletion(build);
assertThat(build.isBuilding(), equalTo(false));
j.assertBuildStatusSuccess(build);
}

Expand Down
37 changes: 16 additions & 21 deletions test/src/test/java/hudson/cli/OnlineNodeCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,28 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Node;
import hudson.remoting.VirtualChannel;
import hudson.slaves.DumbSlave;
import hudson.tasks.Builder;
import hudson.util.OneShotEvent;
import java.io.IOException;
import java.util.concurrent.Future;
import jenkins.model.Jenkins;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.TestBuilder;

/**
* @author pjanouse
Expand All @@ -62,6 +60,9 @@ public class OnlineNodeCommandTest {

private CLICommandInvoker command;

@ClassRule
public static final BuildWatcher buildWatcher = new BuildWatcher();

@Rule public final JenkinsRule j = new JenkinsRule();

@Before public void setUp() {
Expand Down Expand Up @@ -190,8 +191,7 @@ public class OnlineNodeCommandTest {
}
FreeStyleProject project = j.createFreeStyleProject("aProject");
project.setAssignedNode(slave);
final Future<FreeStyleBuild> build = startBlockingAndFinishingBuild(project, finish);
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
final FreeStyleBuild build = startBlockingAndFinishingBuild(project, finish);

slave.toComputer().setTemporarilyOffline(true);
slave.toComputer().waitUntilOffline();
Expand All @@ -206,13 +206,11 @@ public class OnlineNodeCommandTest {
slave.toComputer().waitUntilOnline();
}
assertThat(slave.toComputer().isOnline(), equalTo(true));
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
assertThat(project.isBuilding(), equalTo(true));
assertThat(build.isBuilding(), equalTo(true));

finish.signal();
build.get();
assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1));
assertThat(project.isBuilding(), equalTo(false));
j.waitForCompletion(build);
assertThat(build.isBuilding(), equalTo(false));
j.assertBuildStatusSuccess(build);
}

Expand Down Expand Up @@ -306,24 +304,24 @@ public class OnlineNodeCommandTest {
*
* @param project {@link FreeStyleProject} to start
* @param finish {@link OneShotEvent} to signal to finish a build
* @return A {@link Future} object represents the started build
* @return the started build (the caller should wait for its completion)
* @throws Exception if somethink wrong happened
*/
public static Future<FreeStyleBuild> startBlockingAndFinishingBuild(FreeStyleProject project, OneShotEvent finish) throws Exception {
public static FreeStyleBuild startBlockingAndFinishingBuild(FreeStyleProject project, OneShotEvent finish) throws Exception {
assertFalse(finish.isSignaled());

final OneShotEvent block = new OneShotEvent();

project.getBuildersList().add(new BlockingAndFinishingBuilder(block, finish));

Future<FreeStyleBuild> r = project.scheduleBuild2(0);
FreeStyleBuild b = project.scheduleBuild2(0).waitForStart();
block.block(); // wait until we are safe to interrupt
assertTrue(project.getLastBuild().isBuilding());
assertTrue(b.isBuilding());

return r;
return b;
}

private static final class BlockingAndFinishingBuilder extends Builder {
private static final class BlockingAndFinishingBuilder extends TestBuilder {
private final OneShotEvent block;
private final OneShotEvent finish;

Expand All @@ -345,8 +343,5 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
}
return true;
}

@TestExtension("disconnectCause")
public static class DescriptorImpl extends Descriptor<Builder> {}
}
}
Loading

0 comments on commit 0d9705b

Please sign in to comment.