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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model.queue;
25
26 import hudson.model.Action;
27 import hudson.model.Computer;
28 import hudson.model.Executor;
29 import hudson.model.Node;
30 import hudson.model.Queue;
31 import hudson.model.Queue.BuildableItem;
32 import hudson.model.Queue.JobOffer;
33 import hudson.model.Queue.Task;
34 import hudson.model.Queue.WaitingItem;
35 import org.jvnet.hudson.test.HudsonTestCase;
36 import org.jvnet.hudson.test.TestExtension;
37
38 import java.lang.reflect.Field;
39 import java.util.*;
40 import java.util.concurrent.CopyOnWriteArrayList;
41
42 import static java.util.Arrays.*;
43 import static org.mockito.Mockito.*;
44
45 /**
46 * @author Kohsuke Kawaguchi
47 */
48 public class LoadPredictorTest extends HudsonTestCase {
49 @TestExtension
50 public static class LoadPredictorImpl extends LoadPredictor {
51 @Override
52 public Iterable<FutureLoad> predict(MappingWorksheet plan, Computer computer, long start, long end) {
53 return asList(new FutureLoad(start+5000, end-(start+5000), 1));
54 }
55 }
56
57 /**
58 * Makes sure that {@link LoadPredictor} is taken into account when building {@link MappingWorksheet}.
59 * The scenario is:
60 *
61 * - a computer with 1 executor, idle.
62 * - a future load of size 1 is predicted
63 * - hence the consideration of the current task at hand shall fail, as it'll collide with the estimated future load.
64 */
65 public void test1() throws Exception {
66 Task t = mock(Task.class);
67 when(t.getEstimatedDuration()).thenReturn(10000L);
68 when(t.getSubTasks()).thenReturn((Collection) asList(t));
69
70
71 Computer c = createMockComputer(1);
72
73 JobOffer o = createMockOffer(c.getExecutors().get(0));
74
75 MappingWorksheet mw = new MappingWorksheet(wrap(t), asList(o));
76
77 // the test load predictor should have pushed down the executor count to 0
78 assertTrue(mw.executors.isEmpty());
79 assertEquals(1,mw.works.size());
80 }
81
82 private BuildableItem wrap(Queue.Task t) {
83 return new BuildableItem(new WaitingItem(new GregorianCalendar(),t,new ArrayList<Action>()));
84 }
85
86 /**
87 * Test scenario is:
88 *
89 * - a computer with two executors, one is building something now
90 * - a future load of size 1 is predicted but it'll start after the currently building something is completed.
91 * - hence the currently available executor should be considered available (unlike in test1)
92 */
93 public void test2() throws Exception {
94 Task t = mock(Task.class);
95 when(t.getEstimatedDuration()).thenReturn(10000L);
96 when(t.getSubTasks()).thenReturn((Collection) asList(t));
97
98
99 Computer c = createMockComputer(2);
100 Executor e = c.getExecutors().get(0);
101
102 when(e.isIdle()).thenReturn(false);
103 when(e.getEstimatedRemainingTimeMillis()).thenReturn(300L);
104
105 JobOffer o = createMockOffer(c.getExecutors().get(1));
106
107 MappingWorksheet mw = new MappingWorksheet(wrap(t), asList(o));
108
109 // since the currently busy executor will free up before a future predicted load starts,
110 // we should have a valid executor remain in the queue
111 assertEquals(1,mw.executors.size());
112 assertEquals(1,mw.works.size());
113 }
114
115 private JobOffer createMockOffer(Executor e) throws NoSuchFieldException, IllegalAccessException {
116 JobOffer o = mock(JobOffer.class);
117 when(o.getExecutor()).thenReturn(e);
118 return o;
119 }
120
121 private Computer createMockComputer(int nExecutors) throws Exception {
122 Node n = mock(Node.class);
123 Computer c = mock(Computer.class);
124 when(c.getNode()).thenReturn(n);
125
126 List executors = new CopyOnWriteArrayList();
127
128 for (int i=0; i<nExecutors; i++) {
129 Executor e = mock(Executor.class);
130 when(e.isIdle()).thenReturn(true);
131 when(e.getOwner()).thenReturn(c);
132 executors.add(e);
133 }
134
135 Field f = Computer.class.getDeclaredField("executors");
136 f.setAccessible(true);
137 f.set(c, executors);
138
139 when(c.getExecutors()).thenReturn(executors);
140
141 return c;
142 }
143 }
144