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

1 package hudson.model;
2
3 import com.gargoylesoftware.htmlunit.html.HtmlPage;
4 import hudson.Launcher;
5 import hudson.util.OneShotEvent;
6 import jenkins.model.CauseOfInterruption.UserInterruption;
7 import jenkins.model.InterruptedBuildAction;
8 import jenkins.model.Jenkins;
9 import org.jvnet.hudson.test.Bug;
10 import org.jvnet.hudson.test.HudsonTestCase;
11 import org.jvnet.hudson.test.TestBuilder;
12
13 import java.io.IOException;
14 import java.util.concurrent.Future;
15
16 /**
17 * @author Kohsuke Kawaguchi
18 */
19 public class ExecutorTest extends HudsonTestCase {
20 public void testYank() throws Exception {
21 Computer c = Jenkins.getInstance().toComputer();
22 Executor e = c.getExecutors().get(0);
23
24 // kill an executor
25 kill(e);
26
27 // make sure it's dead
28 assertTrue(c.getExecutors().contains(e));
29 assertTrue(e.getCauseOfDeath()!=null);
30
31 // test the UI
32 HtmlPage p = createWebClient().goTo("/");
33 p = p.getAnchorByText("Dead (!)").click();
34 assertTrue(p.getWebResponse().getContentAsString().contains(ThreadDeath.class.getName()));
35 submit(p.getFormByName("yank"));
36
37 assertFalse(c.getExecutors().contains(e));
38 waitUntilExecutorSizeIs(c, 2);
39 }
40
41 @Bug(4756)
42 public void testWhenAnExecuterIsYankedANewExecuterTakesItsPlace() throws Exception {
43 Computer c = jenkins.toComputer();
44 Executor e = getExecutorByNumber(c, 0);
45
46 kill(e);
47 e.doYank();
48
49 waitUntilExecutorSizeIs(c, 2);
50
51 assertNotNull(getExecutorByNumber(c, 0));
52 assertNotNull(getExecutorByNumber(c, 1));
53 }
54
55 private void waitUntilExecutorSizeIs(Computer c, int executorCollectionSize) throws InterruptedException {
56 int timeOut = 10;
57 while (c.getExecutors().size() != executorCollectionSize) {
58 Thread.sleep(10);
59 if (timeOut-- == 0) fail("executor collection size was not " + executorCollectionSize);
60 }
61 }
62
63 private void kill(Executor e) throws InterruptedException {
64 e.killHard();
65 while (e.isAlive())
66 Thread.sleep(10);
67 }
68
69 private Executor getExecutorByNumber(Computer c, int executorNumber) {
70 for (Executor executor : c.getExecutors()) {
71 if (executor.getNumber() == executorNumber) {
72 return executor;
73 }
74 }
75 return null;
76 }
77
78 /**
79 * Makes sure that the cause of interruption is properly recorded.
80 */
81 public void testAbortCause() throws Exception {
82 final OneShotEvent e = new OneShotEvent();
83
84 FreeStyleProject p = createFreeStyleProject();
85 p.getBuildersList().add(new TestBuilder() {
86 @Override
87 public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
88 e.signal(); // we are safe to be interrupted
89 synchronized (this) {
90 wait();
91 }
92 throw new AssertionError();
93 }
94 });
95
96 Future<FreeStyleBuild> r = p.scheduleBuild2(0);
97 e.block(); // wait until we are safe to interrupt
98 assertTrue(p.getLastBuild().isBuilding());
99 User johnny = User.get("Johnny");
100 p.getLastBuild().getExecutor().interrupt(Result.FAILURE,
101 new UserInterruption(johnny), // test the merge semantics
102 new UserInterruption(johnny));
103
104 FreeStyleBuild b = r.get();
105
106 // make sure this information is recorded
107 assertEquals(b.getResult(),Result.FAILURE);
108 InterruptedBuildAction iba = b.getAction(InterruptedBuildAction.class);
109 assertEquals(1,iba.getCauses().size());
110 assertEquals(((UserInterruption) iba.getCauses().get(0)).getUser(),johnny);
111
112 // make sure it shows up in the log
113 assertTrue(b.getLog().contains("Johnny"));
114 }
115
116 }
117