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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Stephen Connolly
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;
25
26 import junit.framework.TestCase;
27
28 import java.util.Random;
29
30 /**
31 * @author Stephen Connolly
32 */
33 public class ResourceListTest extends TestCase {
34 private Resource a1, a2, a3, a4, a;
35 private Resource b1, b2, b3, b4, b;
36 private Resource c1, c2, c3, c4, c;
37 private Resource d, e, f;
38 private int fWriteCount;
39 private Random entropy;
40 private ResourceList x;
41 private ResourceList y;
42 private ResourceList z;
43
44 public void setUp() {
45 entropy = new Random(0);
46 a = new Resource("A" + entropy.nextLong());
47 a1 = new Resource(a, "A" + entropy.nextLong());
48 a2 = new Resource(a, "A" + entropy.nextLong());
49 a3 = new Resource(a, "A" + entropy.nextLong());
50 a4 = new Resource(a, "A" + entropy.nextLong());
51 b = new Resource("B" + entropy.nextLong());
52 b1 = new Resource(b, "B" + entropy.nextLong());
53 b2 = new Resource(b, "B" + entropy.nextLong());
54 b3 = new Resource(b, "B" + entropy.nextLong());
55 b4 = new Resource(b, "B" + entropy.nextLong());
56 c = new Resource(null, "C" + entropy.nextLong(), 3);
57 c1 = new Resource(c, "C" + entropy.nextLong(), 3);
58 c2 = new Resource(c, "C" + entropy.nextLong(), 3);
59 c3 = new Resource(c, "C" + entropy.nextLong(), 3);
60 c4 = new Resource(c, "C" + entropy.nextLong(), 3);
61 d = new Resource("D" + entropy.nextLong());
62 e = new Resource(null, "E" + entropy.nextLong());
63 fWriteCount = 5 + entropy.nextInt(100);
64 f = new Resource(null, "F" + entropy.nextLong(), 5);
65 x = new ResourceList();
66 y = new ResourceList();
67 z = new ResourceList();
68 }
69
70 public void testEmptyLists() throws Exception {
71 z.r(a);
72 ResourceList w = new ResourceList();
73 w.w(a);
74 assertFalse("Empty vs Empty", x.isCollidingWith(y));
75 assertFalse("Empty vs Empty", y.isCollidingWith(x));
76 assertFalse("Empty vs Read", x.isCollidingWith(z));
77 assertFalse("Read vs Empty", z.isCollidingWith(x));
78 assertFalse("Empty vs Write", x.isCollidingWith(w));
79 assertFalse("Write vs Empty", w.isCollidingWith(x));
80 }
81
82 public void testSimpleR() throws Exception {
83 x.r(a);
84 y.r(b);
85 z.r(a);
86
87 assertFalse("Read-Read", x.isCollidingWith(y));
88 assertFalse("Read-Read", y.isCollidingWith(x));
89 assertFalse("Read-Read", x.isCollidingWith(z));
90 assertFalse("Read-Read", z.isCollidingWith(x));
91 assertFalse("Read-Read", z.isCollidingWith(y));
92 assertFalse("Read-Read", y.isCollidingWith(z));
93 }
94
95 public void testSimpleRW() throws Exception {
96 x.r(a);
97 y.r(b);
98 z.w(a);
99
100 assertFalse("Read-Read different resources", x.isCollidingWith(y));
101 assertFalse("Read-Read different resources", y.isCollidingWith(x));
102 assertTrue("Read-Write same resource", x.isCollidingWith(z));
103 assertTrue("Read-Write same resource", z.isCollidingWith(x));
104 assertFalse("Read-Write different resources", z.isCollidingWith(y));
105 assertFalse("Read-Write different resources", y.isCollidingWith(z));
106 }
107
108 public void testSimpleW() throws Exception {
109 x.w(a);
110 y.w(b);
111 z.w(a);
112
113 assertFalse(x.isCollidingWith(y));
114 assertFalse(y.isCollidingWith(x));
115 assertTrue(x.isCollidingWith(z));
116 assertTrue(z.isCollidingWith(x));
117 assertFalse(z.isCollidingWith(y));
118 assertFalse(y.isCollidingWith(z));
119
120 ResourceList w = ResourceList.union(x,y);
121 assertTrue(w.isCollidingWith(z));
122 assertTrue(z.isCollidingWith(w));
123
124 ResourceList v = new ResourceList();
125 v.w(a1);
126 assertTrue(w.isCollidingWith(v));
127 assertTrue(z.isCollidingWith(w));
128 }
129
130 public void testParentChildR() throws Exception {
131 x.r(a1);
132 x.r(a2);
133 y.r(a3);
134 y.r(a4);
135 z.r(a);
136 assertFalse("Reads should never conflict", x.isCollidingWith(y));
137 assertFalse("Reads should never conflict", y.isCollidingWith(x));
138 assertFalse("Reads should never conflict", x.isCollidingWith(z));
139 assertFalse("Reads should never conflict", z.isCollidingWith(x));
140 assertFalse("Reads should never conflict", z.isCollidingWith(y));
141 assertFalse("Reads should never conflict", y.isCollidingWith(z));
142 }
143
144 public void testParentChildW() throws Exception {
145 x.w(a1);
146 x.w(a2);
147 y.w(a3);
148 y.w(a4);
149 z.w(a);
150 assertFalse("Sibling resources should not conflict", x.isCollidingWith(y));
151 assertFalse("Sibling resources should not conflict", y.isCollidingWith(x));
152 assertTrue("Taking parent resource assumes all children are taken too", x.isCollidingWith(z));
153 assertTrue("Taking parent resource assumes all children are taken too", z.isCollidingWith(x));
154 assertTrue("Taking parent resource assumes all children are taken too", z.isCollidingWith(y));
155 assertTrue("Taking parent resource assumes all children are taken too", y.isCollidingWith(z));
156 }
157
158 public void testParentChildR3() throws Exception {
159 x.r(c1);
160 x.r(c2);
161 y.r(c3);
162 y.r(c4);
163 z.r(c);
164 assertFalse("Reads should never conflict", x.isCollidingWith(y));
165 assertFalse("Reads should never conflict", y.isCollidingWith(x));
166 assertFalse("Reads should never conflict", x.isCollidingWith(z));
167 assertFalse("Reads should never conflict", z.isCollidingWith(x));
168 assertFalse("Reads should never conflict", z.isCollidingWith(y));
169 assertFalse("Reads should never conflict", y.isCollidingWith(z));
170 }
171
172 public void testParentChildW3() throws Exception {
173 x.w(c1);
174 x.w(c2);
175 y.w(c3);
176 y.w(c4);
177 z.w(c);
178 assertFalse("Sibling resources should not conflict", x.isCollidingWith(y));
179 assertFalse("Sibling resources should not conflict", y.isCollidingWith(x));
180 assertFalse("Using less than the limit of child resources should not be a problem", x.isCollidingWith(z));
181 assertFalse("Using less than the limit of child resources should not be a problem", z.isCollidingWith(x));
182 assertFalse("Using less than the limit of child resources should not be a problem", z.isCollidingWith(y));
183 assertFalse("Using less than the limit of child resources should not be a problem", y.isCollidingWith(z));
184
185 ResourceList w = ResourceList.union(x,y);
186
187 assertFalse("Using less than the limit of child resources should not be a problem", w.isCollidingWith(z));
188 assertFalse("Using less than the limit of child resources should not be a problem", z.isCollidingWith(w));
189
190 assertFalse("Total count = 2, limit is 3", w.isCollidingWith(x));
191 assertFalse("Total count = 2, limit is 3", x.isCollidingWith(w));
192
193 ResourceList v = ResourceList.union(x,x); // write count is two
194 assertFalse("Total count = 3, limit is 3", v.isCollidingWith(x));
195 assertFalse("Total count = 3, limit is 3", x.isCollidingWith(v));
196
197 v = ResourceList.union(v,x); // write count is three
198 assertTrue("Total count = 4, limit is 3", v.isCollidingWith(x));
199 assertTrue("Total count = 4, limit is 3", x.isCollidingWith(v));
200 }
201
202 public void testMultiWrite1() throws Exception {
203 y.w(e);
204 assertFalse(x.isCollidingWith(y));
205 assertFalse(y.isCollidingWith(x));
206
207 for (int i = 0; i < fWriteCount; i++) {
208 x.w(e);
209 assertTrue("Total = W" + (i + 1) + ", Limit = W1", x.isCollidingWith(y));
210 assertTrue("Total = W" + (i + 1) + ", Limit = W1", y.isCollidingWith(x));
211 }
212 int j = entropy.nextInt(50) + 3;
213 for (int i = 1; i < j; i++) {
214 assertTrue("Total = W" + (i + fWriteCount) + ", Limit = W1", x.isCollidingWith(y));
215 assertTrue("Total = W" + (i + fWriteCount) + ", Limit = W1", y.isCollidingWith(x));
216 x.w(e);
217 }
218 }
219
220 public void testMultiWriteN() throws Exception {
221 y.w(f);
222 for (int i=0; i<f.numConcurrentWrite; i++) {
223 assertFalse("Total = W" + i + ", Limit = W" + f.numConcurrentWrite, x.isCollidingWith(y));
224 assertFalse("Total = W" + i + ", Limit = W" + f.numConcurrentWrite, y.isCollidingWith(x));
225 x.w(f);
226 }
227 int j = entropy.nextInt(50) + 3;
228 for (int i = 1; i < j; i++) {
229 assertTrue("Total = W" + (fWriteCount + i) + ", Limit = W" + fWriteCount, x.isCollidingWith(y));
230 assertTrue("Total = W" + (fWriteCount + i) + ", Limit = W" + fWriteCount, y.isCollidingWith(x));
231 x.w(f);
232 }
233 }
234
235 public void testMultiRead1() throws Exception {
236 y.r(e);
237 for (int i = 0; i < fWriteCount; i++) {
238 assertFalse("Total = R" + (i + 1) + ", Limit = W1", x.isCollidingWith(y));
239 assertFalse("Total = R" + (i + 1) + ", Limit = W1", y.isCollidingWith(x));
240 x.r(e);
241 }
242 int j = entropy.nextInt(50) + 3;
243 for (int i = 1; i < j; i++) {
244 assertFalse("Total = R" + (i + fWriteCount) + ", Limit = W1", x.isCollidingWith(y));
245 assertFalse("Total = R" + (i + fWriteCount) + ", Limit = W1", y.isCollidingWith(x));
246 x.r(e);
247 }
248 }
249
250 public void testMultiReadN() throws Exception {
251 y.r(f);
252 for (int i = 0; i < fWriteCount; i++) {
253 assertFalse("Total = R" + (i + 1) + ", Limit = W" + fWriteCount, x.isCollidingWith(y));
254 assertFalse("Total = R" + (i + 1) + ", Limit = W" + fWriteCount, y.isCollidingWith(x));
255 x.r(f);
256 }
257 int j = entropy.nextInt(50) + 3;
258 for (int i = 1; i < j; i++) {
259 assertFalse("Total = R" + (fWriteCount + i) + ", Limit = W" + fWriteCount, x.isCollidingWith(y));
260 assertFalse("Total = R" + (fWriteCount + i) + ", Limit = W" + fWriteCount, y.isCollidingWith(x));
261 x.r(f);
262 }
263 }
264
265 }
266