提交 3b4c09e4 编写于 作者: V Valentina Armenise

[JENKINS-30084] enhancing test case

上级 e33f3f62
......@@ -876,6 +876,13 @@ public class Queue extends ResourceController implements Saveable {
return new ArrayList<BuildableItem>(snapshot.pendings);
}
/**
* Gets the snapshot of all {@link BlockedItem}s.
*/
public List<BlockedItem> getBlockedItems() {
return new ArrayList<BlockedItem>(snapshot.blockedProjects);
}
/**
* Returns the snapshot of all {@link LeftItem}s.
*
......
......@@ -100,6 +100,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockQueueItemAuthenticator;
import org.jvnet.hudson.test.SequenceLock;
import org.jvnet.hudson.test.SleepBuilder;
import org.jvnet.hudson.test.TestBuilder;
......@@ -807,8 +808,9 @@ public class QueueTest {
}
@Test
public void shouldBeAbleToBlockFlyWeightTaskOnLastMinute() throws Exception {
public void shouldBeAbleToBlockFlyweightTaskAtTheLastMinute() throws Exception {
MatrixProject matrixProject = r.createMatrixProject("downstream");
matrixProject.setDisplayName("downstream");
matrixProject.setAxes(new AxisList(
new Axis("axis", "a", "b")
));
......@@ -827,19 +829,42 @@ public class QueueTest {
//we schedule the project but we pretend no executors are available thus
//the flyweight task is in the buildable queue without being executed
matrixProject.scheduleBuild2(0);
//let s wait for the Queue instance to be updated
while (Queue.getInstance().getBuildableItems().size() != 1) {
Thread.sleep(10);
}
//in this state the build is not blocked, it's just waiting for an available executor
assertFalse(matrixProject.isBuildBlocked());
assertFalse(Queue.getInstance().getItem(1).isBlocked());
//we start the upstream project that should block the downstream one
upstreamProject.getBuildersList().add(new SleepBuilder(10000));
upstreamProject.setDisplayName("upstream");
QueueTaskFuture upstream = upstreamProject.scheduleBuild2(0);
upstream.waitForStart();
//let's wait for the Queue to be updated
Thread.sleep(5000);
//the downstream project is blocked waiting for the upstream to finish
assertTrue(matrixProject.isBuildBlocked());
//let s wait for the Upstream to enter the buildable Queue
boolean enteredTheQueue = false;
while (!enteredTheQueue) {
for (Queue.BuildableItem item : Queue.getInstance().getBuildableItems()) {
if (item.task.getDisplayName() != null && item.task.getDisplayName().equals(upstreamProject.getDisplayName())) {
enteredTheQueue = true;
}
}
}
//let's wait for the upstream project to actually start so that we're sure the Queue has been updated
//when the upstream starts the downstream has already left the buildable queue and the queue is empty
while (!Queue.getInstance().getBuildableItems().isEmpty()) {
Thread.sleep(10);
}
assertTrue(Queue.getInstance().getItem(1).isBlocked());
assertTrue(Queue.getInstance().getBlockedItems().get(0).task.getDisplayName().equals(matrixProject.displayName));
r.assertBuildStatusSuccess(upstream);
//once the upstream is completed, the downstream can leave the buildable queue.
assertFalse(matrixProject.isBuildBlocked());
//once the upstream is completed, the downstream can join the buildable queue again.
while (Queue.getInstance().getBuildableItems().isEmpty()) {
Thread.sleep(10);
}
assertFalse(Queue.getInstance().getItem(1).isBlocked());
assertTrue(Queue.getInstance().getBlockedItems().isEmpty());
assertTrue(Queue.getInstance().getBuildableItems().get(0).task.getDisplayName().equals(matrixProject.displayName));
}
//let's make sure that the downstram project is not started before the upstream --> we want to simulate
......
......@@ -39,6 +39,8 @@ import hudson.util.DescribableList;
import jenkins.model.Jenkins;
import org.jvnet.hudson.test.JenkinsRule;
import javax.xml.ws.Provider;
/**
* {@link Cloud} implementation useful for testing.
*
......@@ -67,15 +69,12 @@ public class DummyCloudImpl extends Cloud {
*/
public Label label;
public void setNodeProperties(DescribableList<NodeProperty<?>, NodePropertyDescriptor> nodeProperties) {
this.nodeProperties = nodeProperties;
}
public DescribableList<NodeProperty<?>, NodePropertyDescriptor> getNodeProperties() {
public List<NodeProperty<?>> getNodeProperties() {
return this.nodeProperties;
}
DescribableList<NodeProperty<?>,NodePropertyDescriptor> nodeProperties =
new DescribableList<NodeProperty<?>,NodePropertyDescriptor>(Jenkins.getInstance().getNodesObject());
List<NodeProperty<?>> nodeProperties =
new ArrayList<NodeProperty<?>>();
public DummyCloudImpl(JenkinsRule rule, int delay) {
super("test");
......@@ -83,7 +82,7 @@ public class DummyCloudImpl extends Cloud {
this.delay = delay;
}
public DummyCloudImpl(JenkinsRule rule, int delay, DescribableList<NodeProperty<?>, NodePropertyDescriptor> nodeProperties) {
public DummyCloudImpl(JenkinsRule rule, int delay, List<NodeProperty<?>> nodeProperties) {
super("test");
this.rule = rule;
this.delay = delay;
......@@ -125,8 +124,17 @@ public class DummyCloudImpl extends Cloud {
Thread.sleep(time);
System.out.println("launching slave");
DumbSlave slave = rule.createSlave(label);
slave.getNodeProperties().addAll(nodeProperties);
final DumbSlave slave = rule.createSlave(label);
Provider<NodeProperty> nodePropertyProvider = new Provider<NodeProperty>() {
@Override
public NodeProperty invoke(NodeProperty request) {
request.setNode(slave);
return request;
}
};
for (NodeProperty nodeProperty : nodeProperties) {
slave.getNodeProperties().add(nodePropertyProvider.invoke(nodeProperty));
}
computer = slave.toComputer();
computer.connect(false).get();
synchronized (DummyCloudImpl.this) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册