File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\model\JobQueueTest.java.html

1 package hudson.model;
2
3
4 import hudson.model.Queue.BlockedItem;
5 import hudson.model.Queue.WaitingItem;
6 import hudson.model.listeners.RunListener;
7 import org.jvnet.hudson.test.SleepBuilder;
8 import org.jvnet.hudson.test.HudsonTestCase;
9
10 public class JobQueueTest extends HudsonTestCase {
11
12 private static volatile boolean fireCompletedFlag = false;
13 private static volatile boolean fireFinalizeFlag = false;
14
15 @Override protected void setUp() throws Exception {
16 super.setUp();
17 RunListener<Run> listener = new RunListener<Run>() {
18 @Override public void onCompleted(Run r, TaskListener listener) {
19 JobQueueTest.fireCompletedFlag = true;
20 try {
21 Thread.sleep(5000);
22 } catch (InterruptedException e) {
23 }
24 }
25 @Override public void onFinalized(Run r) {
26 JobQueueTest.fireFinalizeFlag = true;
27 try {
28 Thread.sleep(2000);
29 } catch (InterruptedException e) {
30 }
31 }
32 };
33 RunListener.all().add(listener);
34 }
35
36
37 public void testBuildPendingWhenBuildRunning() throws Exception {
38 FreeStyleProject project = createFreeStyleProject("project");
39 project.getBuildersList().add(new SleepBuilder(2000));
40
41 //Kick the first Build
42 project.scheduleBuild2(1);
43 //Schedule another build
44 project.scheduleBuild2(1);
45
46 //The project should be in Queue when Run is in BUILDING stage
47 assertTrue(project.isInQueue());
48
49 //Cancel the project from queue
50 jenkins.getQueue().cancel(project.getQueueItem());
51
52 //Verify the project is removed from Queue
53 assertTrue(jenkins.getQueue().isEmpty());
54 }
55
56 public void testBuildPendingWhenBuildInPostProduction() throws Exception {
57
58 FreeStyleProject project = createFreeStyleProject("project");
59 project.getBuildersList().add(new SleepBuilder(1000));
60 //Kick the first Build
61 project.scheduleBuild2(1);
62 int count =0;
63 //Now, Wait for run to be in POST_PRODUCTION stage
64 while(!JobQueueTest.fireCompletedFlag && count<100) {
65 Thread.sleep(100);
66 count++;
67 }
68
69 if(JobQueueTest.fireCompletedFlag) {
70 //Schedule the build for the project and this build should be in Queue since the state is POST_PRODUCTION
71 project.scheduleBuild2(0);
72 assertTrue(project.isInQueue()); //That means its pending or its waiting or blocked
73 jenkins.getQueue().maintain();
74 while(jenkins.getQueue().getItem(project) instanceof WaitingItem) {
75 System.out.println(jenkins.getQueue().getItem(project));
76 jenkins.getQueue().maintain();
77 Thread.sleep(10);
78 }
79 assertTrue(jenkins.getQueue().getItem(project) instanceof BlockedItem); //check is it is blocked
80 }
81 else {
82 fail("The maximum attemps for checking if the job is in POST_PRODUCTION State have reached");
83 }
84 count=0;
85 while(!JobQueueTest.fireFinalizeFlag && count<100) {
86 Thread.sleep(100);
87 count++;
88 }
89
90 if(JobQueueTest.fireFinalizeFlag) {
91 //Verify the build is removed from Queue since now it is in Completed state
92 //it should be scheduled for run
93 jenkins.getQueue().maintain();
94 assertFalse(jenkins.getQueue().getItem(project) instanceof BlockedItem);
95 }
96 else {
97 fail("The maximum attemps for checking if the job is in COMPLETED State have reached");
98 }
99 Thread.sleep(1000); //Sleep till job completes.
100 }
101 }
102