1 /* |
|
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package java.net.http.internal.common; |
|
25 |
|
26 import org.testng.annotations.Test; |
|
27 |
|
28 import java.util.concurrent.CountDownLatch; |
|
29 import java.util.concurrent.CyclicBarrier; |
|
30 import java.util.concurrent.atomic.AtomicReference; |
|
31 |
|
32 import static org.testng.Assert.assertEquals; |
|
33 import static org.testng.Assert.assertFalse; |
|
34 import static org.testng.Assert.assertTrue; |
|
35 |
|
36 public class DemandTest { |
|
37 |
|
38 @Test |
|
39 public void test01() { |
|
40 assertTrue(new Demand().isFulfilled()); |
|
41 } |
|
42 |
|
43 @Test |
|
44 public void test011() { |
|
45 Demand d = new Demand(); |
|
46 d.increase(3); |
|
47 d.decreaseAndGet(3); |
|
48 assertTrue(d.isFulfilled()); |
|
49 } |
|
50 |
|
51 @Test |
|
52 public void test02() { |
|
53 Demand d = new Demand(); |
|
54 d.increase(1); |
|
55 assertFalse(d.isFulfilled()); |
|
56 } |
|
57 |
|
58 @Test |
|
59 public void test03() { |
|
60 Demand d = new Demand(); |
|
61 d.increase(3); |
|
62 assertEquals(d.decreaseAndGet(3), 3); |
|
63 } |
|
64 |
|
65 @Test |
|
66 public void test04() { |
|
67 Demand d = new Demand(); |
|
68 d.increase(3); |
|
69 assertEquals(d.decreaseAndGet(5), 3); |
|
70 } |
|
71 |
|
72 @Test |
|
73 public void test05() { |
|
74 Demand d = new Demand(); |
|
75 d.increase(7); |
|
76 assertEquals(d.decreaseAndGet(4), 4); |
|
77 } |
|
78 |
|
79 @Test |
|
80 public void test06() { |
|
81 Demand d = new Demand(); |
|
82 assertEquals(d.decreaseAndGet(3), 0); |
|
83 } |
|
84 |
|
85 @Test(expectedExceptions = IllegalArgumentException.class) |
|
86 public void test07() { |
|
87 Demand d = new Demand(); |
|
88 d.increase(0); |
|
89 } |
|
90 |
|
91 @Test(expectedExceptions = IllegalArgumentException.class) |
|
92 public void test08() { |
|
93 Demand d = new Demand(); |
|
94 d.increase(-1); |
|
95 } |
|
96 |
|
97 @Test(expectedExceptions = IllegalArgumentException.class) |
|
98 public void test09() { |
|
99 Demand d = new Demand(); |
|
100 d.increase(10); |
|
101 d.decreaseAndGet(0); |
|
102 } |
|
103 |
|
104 @Test(expectedExceptions = IllegalArgumentException.class) |
|
105 public void test10() { |
|
106 Demand d = new Demand(); |
|
107 d.increase(13); |
|
108 d.decreaseAndGet(-3); |
|
109 } |
|
110 |
|
111 @Test |
|
112 public void test11() { |
|
113 Demand d = new Demand(); |
|
114 d.increase(1); |
|
115 assertTrue(d.tryDecrement()); |
|
116 } |
|
117 |
|
118 @Test |
|
119 public void test12() { |
|
120 Demand d = new Demand(); |
|
121 d.increase(2); |
|
122 assertTrue(d.tryDecrement()); |
|
123 } |
|
124 |
|
125 @Test |
|
126 public void test14() { |
|
127 Demand d = new Demand(); |
|
128 assertFalse(d.tryDecrement()); |
|
129 } |
|
130 |
|
131 @Test |
|
132 public void test141() { |
|
133 Demand d = new Demand(); |
|
134 d.increase(Long.MAX_VALUE); |
|
135 assertFalse(d.isFulfilled()); |
|
136 } |
|
137 |
|
138 @Test |
|
139 public void test142() { |
|
140 Demand d = new Demand(); |
|
141 d.increase(Long.MAX_VALUE); |
|
142 d.increase(1); |
|
143 assertFalse(d.isFulfilled()); |
|
144 } |
|
145 |
|
146 @Test |
|
147 public void test143() { |
|
148 Demand d = new Demand(); |
|
149 d.increase(Long.MAX_VALUE); |
|
150 d.increase(1); |
|
151 assertFalse(d.isFulfilled()); |
|
152 } |
|
153 |
|
154 @Test |
|
155 public void test144() { |
|
156 Demand d = new Demand(); |
|
157 d.increase(Long.MAX_VALUE); |
|
158 d.increase(Long.MAX_VALUE); |
|
159 d.decreaseAndGet(3); |
|
160 d.decreaseAndGet(5); |
|
161 assertFalse(d.isFulfilled()); |
|
162 } |
|
163 |
|
164 @Test |
|
165 public void test145() { |
|
166 Demand d = new Demand(); |
|
167 d.increase(Long.MAX_VALUE); |
|
168 d.decreaseAndGet(Long.MAX_VALUE); |
|
169 assertTrue(d.isFulfilled()); |
|
170 } |
|
171 |
|
172 @Test(invocationCount = 32) |
|
173 public void test15() throws InterruptedException { |
|
174 int N = Math.max(2, Runtime.getRuntime().availableProcessors() + 1); |
|
175 int M = ((N + 1) * N) / 2; // 1 + 2 + 3 + ... N |
|
176 Demand d = new Demand(); |
|
177 d.increase(M); |
|
178 CyclicBarrier start = new CyclicBarrier(N); |
|
179 CountDownLatch stop = new CountDownLatch(N); |
|
180 AtomicReference<Throwable> error = new AtomicReference<>(); |
|
181 for (int i = 0; i < N; i++) { |
|
182 int j = i + 1; |
|
183 new Thread(() -> { |
|
184 try { |
|
185 start.await(); |
|
186 } catch (Exception e) { |
|
187 error.compareAndSet(null, e); |
|
188 } |
|
189 try { |
|
190 assertEquals(d.decreaseAndGet(j), j); |
|
191 } catch (Throwable t) { |
|
192 error.compareAndSet(null, t); |
|
193 } finally { |
|
194 stop.countDown(); |
|
195 } |
|
196 }).start(); |
|
197 } |
|
198 stop.await(); |
|
199 assertTrue(d.isFulfilled()); |
|
200 assertEquals(error.get(), null); |
|
201 } |
|
202 } |
|