J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6267833 |
| 27 | * @summary Tests for invokeAny, invokeAll |
| 28 | * @author Martin Buchholz |
| 29 | */ |
| 30 | |
| 31 | import java.util.*; |
| 32 | import java.util.concurrent.*; |
| 33 | import java.util.concurrent.atomic.*; |
| 34 | |
| 35 | public class Invoke { |
| 36 | static volatile int passed = 0, failed = 0; |
| 37 | |
| 38 | static void fail(String msg) { |
| 39 | failed++; |
| 40 | new AssertionError(msg).printStackTrace(); |
| 41 | } |
| 42 | |
| 43 | static void pass() { |
| 44 | passed++; |
| 45 | } |
| 46 | |
| 47 | static void unexpected(Throwable t) { |
| 48 | failed++; |
| 49 | t.printStackTrace(); |
| 50 | } |
| 51 | |
| 52 | static void check(boolean condition, String msg) { |
| 53 | if (condition) pass(); else fail(msg); |
| 54 | } |
| 55 | |
| 56 | static void check(boolean condition) { |
| 57 | check(condition, "Assertion failure"); |
| 58 | } |
| 59 | |
| 60 | public static void main(String[] args) { |
| 61 | try { |
| 62 | final AtomicLong count = new AtomicLong(0); |
| 63 | ExecutorService fixed = Executors.newFixedThreadPool(5); |
| 64 | class Inc implements Callable<Long> { |
| 65 | public Long call() throws Exception { |
| 66 | Thread.sleep(200); // Catch IE from possible cancel |
| 67 | return count.incrementAndGet(); |
| 68 | } |
| 69 | } |
| 70 | List<Inc> tasks = Arrays.asList(new Inc(), new Inc(), new Inc()); |
| 71 | List<Future<Long>> futures = fixed.invokeAll(tasks); |
| 72 | check(futures.size() == tasks.size()); |
| 73 | check(count.get() == tasks.size()); |
| 74 | |
| 75 | long gauss = 0; |
| 76 | for (Future<Long> future : futures) gauss += future.get(); |
| 77 | check(gauss == ((tasks.size()+1)*tasks.size())/2); |
| 78 | |
| 79 | ExecutorService single = Executors.newSingleThreadExecutor(); |
| 80 | long save = count.get(); |
| 81 | check(single.invokeAny(tasks) == save + 1); |
| 82 | check(count.get() == save + 1); |
| 83 | |
| 84 | fixed.shutdown(); |
| 85 | single.shutdown(); |
| 86 | |
| 87 | } catch (Throwable t) { unexpected(t); } |
| 88 | |
| 89 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 90 | if (failed > 0) throw new Error("Some tests failed"); |
| 91 | } |
| 92 | } |