1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
/* |
8 |
* @test |
9 |
* @bug 6941130 |
10 |
* @summary Numeric overflow/underflow of permits causes Error throw |
11 |
*/ |
12 |
|
13 |
import java.util.concurrent.Semaphore; |
14 |
|
15 |
public class PermitOverflow { |
16 |
|
17 |
public static void main(String[] args) throws Throwable { |
18 |
for (boolean fair : new boolean[] { true, false }) { |
19 |
Semaphore sem = new Semaphore(Integer.MAX_VALUE - 1, fair); |
20 |
if (sem.availablePermits() != Integer.MAX_VALUE - 1) |
21 |
throw new RuntimeException(); |
22 |
try { |
23 |
sem.release(2); |
24 |
throw new RuntimeException(); |
25 |
} catch (Error expected) { |
26 |
} |
27 |
sem.release(1); |
28 |
if (sem.availablePermits() != Integer.MAX_VALUE) |
29 |
throw new RuntimeException(); |
30 |
try { |
31 |
sem.release(1); |
32 |
throw new RuntimeException(); |
33 |
} catch (Error expected) { |
34 |
} |
35 |
try { |
36 |
sem.release(Integer.MAX_VALUE); |
37 |
throw new RuntimeException(); |
38 |
} catch (Error expected) { |
39 |
} |
40 |
} |
41 |
|
42 |
class Sem extends Semaphore { |
43 |
public Sem(int permits, boolean fair) { |
44 |
super(permits, fair); |
45 |
} |
46 |
public void reducePermits(int reduction) { |
47 |
super.reducePermits(reduction); |
48 |
} |
49 |
} |
50 |
|
51 |
for (boolean fair : new boolean[] { true, false }) { |
52 |
Sem sem = new Sem(Integer.MIN_VALUE + 1, fair); |
53 |
if (sem.availablePermits() != Integer.MIN_VALUE + 1) |
54 |
throw new RuntimeException(); |
55 |
try { |
56 |
sem.reducePermits(2); |
57 |
throw new RuntimeException(); |
58 |
} catch (Error expected) { |
59 |
} |
60 |
sem.reducePermits(1); |
61 |
if (sem.availablePermits() != Integer.MIN_VALUE) |
62 |
throw new RuntimeException(); |
63 |
try { |
64 |
sem.reducePermits(1); |
65 |
throw new RuntimeException(); |
66 |
} catch (Error expected) { |
67 |
} |
68 |
try { |
69 |
sem.reducePermits(Integer.MAX_VALUE); |
70 |
throw new RuntimeException(); |
71 |
} catch (Error expected) { |
72 |
} |
73 |
} |
74 |
} |
75 |
} |