Search in sources :

Example 1 with CompletionException

use of java.util.concurrent.CompletionException in project hbase by apache.

the class TestAsyncTableAdminApi method testCreateTableWithEmptyRowInTheSplitKeys.

@Test(timeout = 300000)
public void testCreateTableWithEmptyRowInTheSplitKeys() throws IOException {
    byte[] tableName = Bytes.toBytes(name.getMethodName());
    byte[][] splitKeys = new byte[3][];
    splitKeys[0] = "region1".getBytes();
    splitKeys[1] = HConstants.EMPTY_BYTE_ARRAY;
    splitKeys[2] = "region2".getBytes();
    HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
    desc.addFamily(new HColumnDescriptor("col"));
    try {
        admin.createTable(desc, splitKeys).join();
        fail("Test case should fail as empty split key is passed.");
    } catch (CompletionException e) {
        assertTrue(e.getCause() instanceof IllegalArgumentException);
    }
}
Also used : HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) CompletionException(java.util.concurrent.CompletionException) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Test(org.junit.Test)

Example 2 with CompletionException

use of java.util.concurrent.CompletionException in project jdk8u_jdk by JetBrains.

the class Basic method test.

private static void test(ExecutorService executor) throws Throwable {
    Thread.currentThread().setName("mainThread");
    //----------------------------------------------------------------
    try {
        CompletableFuture<String> cf = supplyAsync(() -> "a test string");
        checkCompletedNormally(cf, cf.join());
        cf = supplyAsync(() -> "a test string", commonPool());
        checkCompletedNormally(cf, cf.join());
        cf = supplyAsync(() -> "a test string", executor);
        checkCompletedNormally(cf, cf.join());
        cf = supplyAsync(() -> {
            throw new RuntimeException();
        });
        checkCompletedExceptionally(cf);
        cf = supplyAsync(() -> {
            throw new RuntimeException();
        }, commonPool());
        checkCompletedExceptionally(cf);
        cf = supplyAsync(() -> {
            throw new RuntimeException();
        }, executor);
        checkCompletedExceptionally(cf);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Void> cf = runAsync(() -> {
        });
        checkCompletedNormally(cf, cf.join());
        cf = runAsync(() -> {
        }, commonPool());
        checkCompletedNormally(cf, cf.join());
        cf = runAsync(() -> {
        }, executor);
        checkCompletedNormally(cf, cf.join());
        cf = runAsync(() -> {
            throw new RuntimeException();
        });
        checkCompletedExceptionally(cf);
        cf = runAsync(() -> {
            throw new RuntimeException();
        }, commonPool());
        checkCompletedExceptionally(cf);
        cf = runAsync(() -> {
            throw new RuntimeException();
        }, executor);
        checkCompletedExceptionally(cf);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        final Phaser phaser = new Phaser(1);
        final int phase = phaser.getPhase();
        CompletableFuture<Integer> cf;
        cf = supplyAsync(() -> {
            phaser.awaitAdvance(phase);
            return 1;
        });
        cf.complete(2);
        phaser.arrive();
        checkCompletedNormally(cf, 2);
        cf = supplyAsync(() -> {
            phaser.awaitAdvance(phase + 1);
            return 1;
        });
        cf.completeExceptionally(new Throwable());
        phaser.arrive();
        checkCompletedExceptionally(cf);
        cf = supplyAsync(() -> {
            phaser.awaitAdvance(phase + 2);
            return 1;
        });
        cf.cancel(true);
        phaser.arrive();
        checkCompletedExceptionally(cf, true);
        cf = supplyAsync(() -> {
            phaser.awaitAdvance(phase + 3);
            return 1;
        });
        check(cf.getNow(2) == 2);
        phaser.arrive();
        checkCompletedNormally(cf, 1);
        check(cf.getNow(2) == 1);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Integer> cf2;
        CompletableFuture<String> cf1 = supplyAsync(() -> "a test string");
        cf2 = cf1.thenApply((x) -> {
            if (x.equals("a test string"))
                return 1;
            else
                return 0;
        });
        checkCompletedNormally(cf1, "a test string");
        checkCompletedNormally(cf2, 1);
        cf1 = supplyAsync(() -> "a test string");
        cf2 = cf1.thenApplyAsync((x) -> {
            if (x.equals("a test string"))
                return 1;
            else
                return 0;
        });
        checkCompletedNormally(cf1, "a test string");
        checkCompletedNormally(cf2, 1);
        cf1 = supplyAsync(() -> "a test string");
        cf2 = cf1.thenApplyAsync((x) -> {
            if (x.equals("a test string"))
                return 1;
            else
                return 0;
        }, executor);
        checkCompletedNormally(cf1, "a test string");
        checkCompletedNormally(cf2, 1);
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = cf1.thenApply((x) -> {
            return 0;
        });
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = cf1.thenApplyAsync((x) -> {
            return 0;
        });
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = cf1.thenApplyAsync((x) -> {
            return 0;
        }, executor);
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Void> cf2;
        int before = atomicInt.get();
        CompletableFuture<String> cf1 = supplyAsync(() -> "a test string");
        cf2 = cf1.thenAccept((x) -> {
            if (x.equals("a test string")) {
                atomicInt.incrementAndGet();
                return;
            }
            throw new RuntimeException();
        });
        checkCompletedNormally(cf1, "a test string");
        checkCompletedNormally(cf2, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = supplyAsync(() -> "a test string");
        cf2 = cf1.thenAcceptAsync((x) -> {
            if (x.equals("a test string")) {
                atomicInt.incrementAndGet();
                return;
            }
            throw new RuntimeException();
        });
        checkCompletedNormally(cf1, "a test string");
        checkCompletedNormally(cf2, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = supplyAsync(() -> "a test string");
        cf2 = cf1.thenAcceptAsync((x) -> {
            if (x.equals("a test string")) {
                atomicInt.incrementAndGet();
                return;
            }
            throw new RuntimeException();
        }, executor);
        checkCompletedNormally(cf1, "a test string");
        checkCompletedNormally(cf2, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = cf1.thenAccept((x) -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        check(atomicInt.get() == before);
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = cf1.thenAcceptAsync((x) -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        check(atomicInt.get() == before);
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = cf1.thenAcceptAsync((x) -> {
            atomicInt.incrementAndGet();
        }, executor);
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        check(atomicInt.get() == before);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Void> cf2;
        int before = atomicInt.get();
        CompletableFuture<String> cf1 = supplyAsync(() -> "a test string");
        cf2 = cf1.thenRun(() -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf1, "a test string");
        checkCompletedNormally(cf2, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = supplyAsync(() -> "a test string");
        cf2 = cf1.thenRunAsync(() -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf1, "a test string");
        checkCompletedNormally(cf2, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = supplyAsync(() -> "a test string");
        cf2 = cf1.thenRunAsync(() -> {
            atomicInt.incrementAndGet();
        }, executor);
        checkCompletedNormally(cf1, "a test string");
        checkCompletedNormally(cf2, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = cf1.thenRun(() -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        check(atomicInt.get() == before);
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = cf1.thenRunAsync(() -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        check(atomicInt.get() == before);
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = cf1.thenRunAsync(() -> {
            atomicInt.incrementAndGet();
        }, executor);
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        check(atomicInt.get() == before);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Integer> cf3;
        CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
        CompletableFuture<Integer> cf2 = supplyAsync(() -> 1);
        cf3 = cf1.thenCombine(cf2, (x, y) -> {
            return x + y;
        });
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 1);
        checkCompletedNormally(cf3, 2);
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> 1);
        cf3 = cf1.thenCombineAsync(cf2, (x, y) -> {
            return x + y;
        });
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 1);
        checkCompletedNormally(cf3, 2);
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> 1);
        cf3 = cf1.thenCombineAsync(cf2, (x, y) -> {
            return x + y;
        }, executor);
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 1);
        checkCompletedNormally(cf3, 2);
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = supplyAsync(() -> 1);
        cf3 = cf1.thenCombine(cf2, (x, y) -> {
            return 0;
        });
        checkCompletedExceptionally(cf1);
        checkCompletedNormally(cf2, 1);
        checkCompletedExceptionally(cf3);
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf1.thenCombineAsync(cf2, (x, y) -> {
            return 0;
        });
        checkCompletedNormally(cf1, 1);
        checkCompletedExceptionally(cf2);
        checkCompletedExceptionally(cf3);
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf1.thenCombineAsync(cf2, (x, y) -> {
            return 0;
        }, executor);
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        checkCompletedExceptionally(cf3);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Void> cf3;
        int before = atomicInt.get();
        CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
        CompletableFuture<Integer> cf2 = supplyAsync(() -> 1);
        cf3 = cf1.thenAcceptBoth(cf2, (x, y) -> {
            check(x + y == 2);
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 1);
        checkCompletedNormally(cf3, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> 1);
        cf3 = cf1.thenAcceptBothAsync(cf2, (x, y) -> {
            check(x + y == 2);
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 1);
        checkCompletedNormally(cf3, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> 1);
        cf3 = cf1.thenAcceptBothAsync(cf2, (x, y) -> {
            check(x + y == 2);
            atomicInt.incrementAndGet();
        }, executor);
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 1);
        checkCompletedNormally(cf3, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = supplyAsync(() -> 1);
        cf3 = cf1.thenAcceptBoth(cf2, (x, y) -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedExceptionally(cf1);
        checkCompletedNormally(cf2, 1);
        checkCompletedExceptionally(cf3);
        check(atomicInt.get() == before);
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf1.thenAcceptBothAsync(cf2, (x, y) -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf1, 1);
        checkCompletedExceptionally(cf2);
        checkCompletedExceptionally(cf3);
        check(atomicInt.get() == before);
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf1.thenAcceptBothAsync(cf2, (x, y) -> {
            atomicInt.incrementAndGet();
        }, executor);
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        checkCompletedExceptionally(cf3);
        check(atomicInt.get() == before);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Void> cf3;
        int before = atomicInt.get();
        CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
        CompletableFuture<Integer> cf2 = supplyAsync(() -> 1);
        cf3 = cf1.runAfterBoth(cf2, () -> {
            check(cf1.isDone());
            check(cf2.isDone());
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 1);
        checkCompletedNormally(cf3, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        CompletableFuture<Integer> cfa = supplyAsync(() -> 1);
        CompletableFuture<Integer> cfb = supplyAsync(() -> 1);
        cf3 = cfa.runAfterBothAsync(cfb, () -> {
            check(cfa.isDone());
            check(cfb.isDone());
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cfa, 1);
        checkCompletedNormally(cfb, 1);
        checkCompletedNormally(cf3, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        CompletableFuture<Integer> cfx = supplyAsync(() -> 1);
        CompletableFuture<Integer> cfy = supplyAsync(() -> 1);
        cf3 = cfy.runAfterBothAsync(cfx, () -> {
            check(cfx.isDone());
            check(cfy.isDone());
            atomicInt.incrementAndGet();
        }, executor);
        checkCompletedNormally(cfx, 1);
        checkCompletedNormally(cfy, 1);
        checkCompletedNormally(cf3, null);
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        CompletableFuture<Integer> cf4 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        CompletableFuture<Integer> cf5 = supplyAsync(() -> 1);
        cf3 = cf5.runAfterBothAsync(cf4, () -> {
            atomicInt.incrementAndGet();
        }, executor);
        checkCompletedExceptionally(cf4);
        checkCompletedNormally(cf5, 1);
        checkCompletedExceptionally(cf3);
        check(atomicInt.get() == before);
        before = atomicInt.get();
        cf4 = supplyAsync(() -> 1);
        cf5 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf5.runAfterBothAsync(cf4, () -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf4, 1);
        checkCompletedExceptionally(cf5);
        checkCompletedExceptionally(cf3);
        check(atomicInt.get() == before);
        before = atomicInt.get();
        cf4 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf5 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf5.runAfterBoth(cf4, () -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedExceptionally(cf4);
        checkCompletedExceptionally(cf5);
        checkCompletedExceptionally(cf3);
        check(atomicInt.get() == before);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Integer> cf3;
        CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
        CompletableFuture<Integer> cf2 = supplyAsync(() -> 2);
        cf3 = cf1.applyToEither(cf2, (x) -> {
            check(x == 1 || x == 2);
            return x;
        });
        checkCompletedNormally(cf3, new Object[] { 1, 2 });
        check(cf1.isDone() || cf2.isDone());
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> 2);
        cf3 = cf1.applyToEitherAsync(cf2, (x) -> {
            check(x == 1 || x == 2);
            return x;
        });
        checkCompletedNormally(cf3, new Object[] { 1, 2 });
        check(cf1.isDone() || cf2.isDone());
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> 2);
        cf3 = cf1.applyToEitherAsync(cf2, (x) -> {
            check(x == 1 || x == 2);
            return x;
        }, executor);
        checkCompletedNormally(cf3, new Object[] { 1, 2 });
        check(cf1.isDone() || cf2.isDone());
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = supplyAsync(() -> 2);
        cf3 = cf1.applyToEither(cf2, (x) -> {
            check(x == 2);
            return x;
        });
        try {
            check(cf3.join() == 2);
        } catch (CompletionException x) {
            pass();
        }
        check(cf3.isDone());
        check(cf1.isDone() || cf2.isDone());
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf1.applyToEitherAsync(cf2, (x) -> {
            check(x == 1);
            return x;
        });
        try {
            check(cf3.join() == 1);
        } catch (CompletionException x) {
            pass();
        }
        check(cf3.isDone());
        check(cf1.isDone() || cf2.isDone());
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf1.applyToEitherAsync(cf2, (x) -> {
            fail();
            return x;
        });
        checkCompletedExceptionally(cf3);
        check(cf1.isDone() || cf2.isDone());
        final Phaser cf3Done = new Phaser(2);
        cf1 = supplyAsync(() -> {
            cf3Done.arriveAndAwaitAdvance();
            return 1;
        });
        cf2 = supplyAsync(() -> 2);
        cf3 = cf1.applyToEither(cf2, (x) -> {
            check(x == 2);
            return x;
        });
        checkCompletedNormally(cf3, 2);
        checkCompletedNormally(cf2, 2);
        check(!cf1.isDone());
        cf3Done.arrive();
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf3, 2);
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> {
            cf3Done.arriveAndAwaitAdvance();
            return 2;
        });
        cf3 = cf1.applyToEitherAsync(cf2, (x) -> {
            check(x == 1);
            return x;
        });
        checkCompletedNormally(cf3, 1);
        checkCompletedNormally(cf1, 1);
        check(!cf2.isDone());
        cf3Done.arrive();
        checkCompletedNormally(cf2, 2);
        checkCompletedNormally(cf3, 1);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Void> cf3;
        int before = atomicInt.get();
        CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
        CompletableFuture<Integer> cf2 = supplyAsync(() -> 2);
        cf3 = cf1.acceptEither(cf2, (x) -> {
            check(x == 1 || x == 2);
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf3, null);
        check(cf1.isDone() || cf2.isDone());
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> 2);
        cf3 = cf1.acceptEitherAsync(cf2, (x) -> {
            check(x == 1 || x == 2);
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf3, null);
        check(cf1.isDone() || cf2.isDone());
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> 2);
        cf3 = cf2.acceptEitherAsync(cf1, (x) -> {
            check(x == 1 || x == 2);
            atomicInt.incrementAndGet();
        }, executor);
        checkCompletedNormally(cf3, null);
        check(cf1.isDone() || cf2.isDone());
        check(atomicInt.get() == (before + 1));
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = supplyAsync(() -> 2);
        cf3 = cf2.acceptEitherAsync(cf1, (x) -> {
            check(x == 2);
        }, executor);
        try {
            check(cf3.join() == null);
        } catch (CompletionException x) {
            pass();
        }
        check(cf3.isDone());
        check(cf1.isDone() || cf2.isDone());
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf2.acceptEitherAsync(cf1, (x) -> {
            check(x == 1);
        });
        try {
            check(cf3.join() == null);
        } catch (CompletionException x) {
            pass();
        }
        check(cf3.isDone());
        check(cf1.isDone() || cf2.isDone());
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf2.acceptEitherAsync(cf1, (x) -> {
            fail();
        });
        checkCompletedExceptionally(cf3);
        check(cf1.isDone() || cf2.isDone());
        final Phaser cf3Done = new Phaser(2);
        cf1 = supplyAsync(() -> {
            cf3Done.arriveAndAwaitAdvance();
            return 1;
        });
        cf2 = supplyAsync(() -> 2);
        cf3 = cf1.acceptEither(cf2, (x) -> {
            check(x == 2);
        });
        checkCompletedNormally(cf3, null);
        checkCompletedNormally(cf2, 2);
        check(!cf1.isDone());
        cf3Done.arrive();
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf3, null);
        cf1 = supplyAsync(() -> 1);
        cf2 = supplyAsync(() -> {
            cf3Done.arriveAndAwaitAdvance();
            return 2;
        });
        cf3 = cf1.acceptEitherAsync(cf2, (x) -> {
            check(x == 1);
        });
        checkCompletedNormally(cf3, null);
        checkCompletedNormally(cf1, 1);
        check(!cf2.isDone());
        cf3Done.arrive();
        checkCompletedNormally(cf2, 2);
        checkCompletedNormally(cf3, null);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Void> cf3;
        int before = atomicInt.get();
        CompletableFuture<Void> cf1 = runAsync(() -> {
        });
        CompletableFuture<Void> cf2 = runAsync(() -> {
        });
        cf3 = cf1.runAfterEither(cf2, () -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf3, null);
        check(cf1.isDone() || cf2.isDone());
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = runAsync(() -> {
        });
        cf2 = runAsync(() -> {
        });
        cf3 = cf1.runAfterEitherAsync(cf2, () -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf3, null);
        check(cf1.isDone() || cf2.isDone());
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = runAsync(() -> {
        });
        cf2 = runAsync(() -> {
        });
        cf3 = cf2.runAfterEitherAsync(cf1, () -> {
            atomicInt.incrementAndGet();
        }, executor);
        checkCompletedNormally(cf3, null);
        check(cf1.isDone() || cf2.isDone());
        check(atomicInt.get() == (before + 1));
        before = atomicInt.get();
        cf1 = runAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = runAsync(() -> {
        });
        cf3 = cf2.runAfterEither(cf1, () -> {
            atomicInt.incrementAndGet();
        });
        try {
            check(cf3.join() == null);
            check(atomicInt.get() == (before + 1));
        } catch (CompletionException x) {
            pass();
        }
        check(cf3.isDone());
        check(cf1.isDone() || cf2.isDone());
        before = atomicInt.get();
        cf1 = runAsync(() -> {
        });
        cf2 = runAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf1.runAfterEitherAsync(cf2, () -> {
            atomicInt.incrementAndGet();
        });
        try {
            check(cf3.join() == null);
            check(atomicInt.get() == (before + 1));
        } catch (CompletionException x) {
            pass();
        }
        check(cf3.isDone());
        check(cf1.isDone() || cf2.isDone());
        before = atomicInt.get();
        cf1 = runAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = runAsync(() -> {
            throw new RuntimeException();
        });
        cf3 = cf2.runAfterEitherAsync(cf1, () -> {
            atomicInt.incrementAndGet();
        }, executor);
        checkCompletedExceptionally(cf3);
        check(cf1.isDone() || cf2.isDone());
        check(atomicInt.get() == before);
        final Phaser cf3Done = new Phaser(2);
        before = atomicInt.get();
        cf1 = runAsync(() -> {
            cf3Done.arriveAndAwaitAdvance();
        });
        cf2 = runAsync(() -> {
        });
        cf3 = cf1.runAfterEither(cf2, () -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf3, null);
        checkCompletedNormally(cf2, null);
        check(!cf1.isDone());
        check(atomicInt.get() == (before + 1));
        cf3Done.arrive();
        checkCompletedNormally(cf1, null);
        checkCompletedNormally(cf3, null);
        before = atomicInt.get();
        cf1 = runAsync(() -> {
        });
        cf2 = runAsync(() -> {
            cf3Done.arriveAndAwaitAdvance();
        });
        cf3 = cf1.runAfterEitherAsync(cf2, () -> {
            atomicInt.incrementAndGet();
        });
        checkCompletedNormally(cf3, null);
        checkCompletedNormally(cf1, null);
        check(!cf2.isDone());
        check(atomicInt.get() == (before + 1));
        cf3Done.arrive();
        checkCompletedNormally(cf2, null);
        checkCompletedNormally(cf3, null);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Integer> cf2;
        CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
        cf2 = cf1.thenCompose((x) -> {
            check(x == 1);
            return CompletableFuture.completedFuture(2);
        });
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 2);
        cf1 = supplyAsync(() -> 1);
        cf2 = cf1.thenComposeAsync((x) -> {
            check(x == 1);
            return CompletableFuture.completedFuture(2);
        });
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 2);
        cf1 = supplyAsync(() -> 1);
        cf2 = cf1.thenComposeAsync((x) -> {
            check(x == 1);
            return CompletableFuture.completedFuture(2);
        }, executor);
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 2);
        int before = atomicInt.get();
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = cf1.thenCompose((x) -> {
            atomicInt.incrementAndGet();
            return CompletableFuture.completedFuture(2);
        });
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        check(atomicInt.get() == before);
        cf1 = supplyAsync(() -> {
            throw new RuntimeException();
        });
        cf2 = cf1.thenComposeAsync((x) -> {
            atomicInt.incrementAndGet();
            return CompletableFuture.completedFuture(2);
        });
        checkCompletedExceptionally(cf1);
        checkCompletedExceptionally(cf2);
        check(atomicInt.get() == before);
        cf1 = supplyAsync(() -> 1);
        cf2 = cf1.thenComposeAsync((x) -> {
            throw new RuntimeException();
        }, executor);
        checkCompletedNormally(cf1, 1);
        checkCompletedExceptionally(cf2);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Object> cf3;
        for (int k = 0; k < 10; k++) {
            CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
            CompletableFuture<Integer> cf2 = supplyAsync(() -> 2);
            cf3 = CompletableFuture.anyOf(cf1, cf2);
            checkCompletedNormally(cf3, new Object[] { 1, 2 });
            check(cf1.isDone() || cf2.isDone());
        }
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<?> cf3;
        for (int k = 0; k < 10; k++) {
            CompletableFuture<Integer>[] cfs = (CompletableFuture<Integer>[]) Array.newInstance(CompletableFuture.class, 10);
            for (int j = 0; j < 10; j++) {
                final int v = j;
                cfs[j] = supplyAsync(() -> v);
            }
            cf3 = CompletableFuture.allOf(cfs);
            for (int j = 0; j < 10; j++) checkCompletedNormally(cfs[j], j);
            checkCompletedNormally(cf3, null);
        }
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Integer> cf2;
        CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
        cf2 = cf1.exceptionally((t) -> {
            fail("function should never be called");
            return 2;
        });
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 1);
        final RuntimeException t = new RuntimeException();
        cf1 = supplyAsync(() -> {
            throw t;
        });
        cf2 = cf1.exceptionally((x) -> {
            check(x.getCause() == t);
            return 2;
        });
        checkCompletedExceptionally(cf1);
        checkCompletedNormally(cf2, 2);
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        CompletableFuture<Integer> cf2;
        CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
        cf2 = cf1.handle((x, t) -> x + 1);
        checkCompletedNormally(cf1, 1);
        checkCompletedNormally(cf2, 2);
        final RuntimeException ex = new RuntimeException();
        cf1 = supplyAsync(() -> {
            throw ex;
        });
        cf2 = cf1.handle((x, t) -> {
            check(t.getCause() == ex);
            return 2;
        });
        checkCompletedExceptionally(cf1);
        checkCompletedNormally(cf2, 2);
    } catch (Throwable t) {
        unexpected(t);
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) ExecutionException(java.util.concurrent.ExecutionException) Array(java.lang.reflect.Array) CancellationException(java.util.concurrent.CancellationException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForkJoinPool(java.util.concurrent.ForkJoinPool) Phaser(java.util.concurrent.Phaser) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) ExecutorService(java.util.concurrent.ExecutorService) Executors(java.util.concurrent.Executors) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Phaser(java.util.concurrent.Phaser)

Example 3 with CompletionException

use of java.util.concurrent.CompletionException in project jdk8u_jdk by JetBrains.

the class ConcurrentAssociateTest method testOnce.

private static void testOnce(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) {
    ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<>();
    CountDownLatch s = new CountDownLatch(1);
    Supplier<Runnable> sr = () -> () -> {
        try {
            s.await();
        } catch (InterruptedException e) {
        }
        for (int i = 0; i < N; i++) {
            Object o = new X();
            associator.accept(m, o);
            if (!m.containsKey(o)) {
                throw new AssociationFailure(desc + " failed: entry does not exist");
            }
        }
    };
    int ps = Runtime.getRuntime().availableProcessors();
    Stream<CompletableFuture> runners = IntStream.range(0, ps).mapToObj(i -> sr.get()).map(CompletableFuture::runAsync);
    CompletableFuture all = CompletableFuture.allOf(runners.toArray(CompletableFuture[]::new));
    // Trigger the runners to start associating
    s.countDown();
    try {
        all.join();
    } catch (CompletionException e) {
        Throwable t = e.getCause();
        if (t instanceof AssociationFailure) {
            throw (AssociationFailure) t;
        } else {
            throw e;
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BiConsumer(java.util.function.BiConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with CompletionException

use of java.util.concurrent.CompletionException in project android by JetBrains.

the class SubmitFeedback method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    if (project == null) {
        Logger.getInstance(SubmitFeedback.class).info("Unable to identify current project");
        return;
    }
    if (!InstantRunSettings.isInstantRunEnabled() || !InstantRunSettings.isRecorderEnabled()) {
        int result = Messages.showYesNoDialog(project, AndroidBundle.message("instant.run.flr.would.you.like.to.enable"), AndroidBundle.message("instant.run.flr.dialog.title"), "Yes, I'd like to help", "Cancel", Messages.getQuestionIcon());
        if (result == Messages.NO) {
            return;
        }
        InstantRunSettings.setInstantRunEnabled(true);
        InstantRunSettings.setRecorderEnabled(true);
        Messages.showInfoMessage(project, AndroidBundle.message("instant.run.flr.howto"), AndroidBundle.message("instant.run.flr.dialog.title"));
        return;
    }
    InstantRunFeedbackDialog dialog = new InstantRunFeedbackDialog(project);
    boolean ok = dialog.showAndGet();
    if (ok) {
        new Task.Backgroundable(project, "Submitting Instant Run Issue") {

            public CompletableFuture<String> myReport;

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                myReport = GoogleCrash.getInstance().submit(FlightRecorder.get(project), dialog.getIssueText(), dialog.getLogs());
                while (!myReport.isDone()) {
                    try {
                        myReport.get(200, TimeUnit.MILLISECONDS);
                    } catch (Exception ignored) {
                    }
                    if (indicator.isCanceled()) {
                        return;
                    }
                }
            }

            @Override
            public void onSuccess() {
                if (myReport.isDone()) {
                    String reportId;
                    try {
                        reportId = myReport.getNow("00");
                    } catch (CancellationException e) {
                        Logger.getInstance(SubmitFeedback.class).info("Submission of flight recorder logs cancelled");
                        return;
                    } catch (CompletionException e) {
                        FLR_NOTIFICATION_GROUP.createNotification("Unexpected error while submitting instant run logs: " + e.getMessage(), NotificationType.ERROR);
                        Logger.getInstance(SubmitFeedback.class).info(e);
                        return;
                    }
                    String message = String.format("<html>Thank you for submitting the bug report.<br>" + "If you would like to follow up on this report, please file a bug at <a href=\"bug\">b.android.com</a> and specify the report id '%1$s'<html>", reportId);
                    FLR_NOTIFICATION_GROUP.createNotification("", message, NotificationType.INFORMATION, (notification, event) -> {
                        Escaper escaper = UrlEscapers.urlFormParameterEscaper();
                        String comment = String.format("Build: %1$s\nInstant Run Report: %2$s", ApplicationInfo.getInstance().getFullVersion(), reportId);
                        String url = String.format("https://code.google.com/p/android/issues/entry?template=%1$s&comment=%2$s&status=New", escaper.escape("Android Studio Instant Run Bug"), escaper.escape(comment));
                        BrowserUtil.browse(url);
                    }).notify(project);
                }
            }
        }.queue();
    }
}
Also used : Task(com.intellij.openapi.progress.Task) CancellationException(java.util.concurrent.CancellationException) CompletionException(java.util.concurrent.CompletionException) Project(com.intellij.openapi.project.Project) CancellationException(java.util.concurrent.CancellationException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) CompletionException(java.util.concurrent.CompletionException) Escaper(com.google.common.escape.Escaper)

Example 5 with CompletionException

use of java.util.concurrent.CompletionException in project caffeine by ben-manes.

the class LoadingCacheTest method asyncLoad_exception.

@Test
public void asyncLoad_exception() throws Exception {
    Exception e = new Exception();
    CacheLoader<Integer, Integer> loader = key -> {
        throw e;
    };
    try {
        loader.asyncLoad(1, Runnable::run).join();
    } catch (CompletionException ex) {
        assertThat(ex.getCause(), is(sameInstance(e)));
    }
}
Also used : Arrays(java.util.Arrays) CacheContext(com.github.benmanes.caffeine.cache.testing.CacheContext) Iterables(com.google.common.collect.Iterables) Listeners(org.testng.annotations.Listeners) CacheExecutor(com.github.benmanes.caffeine.cache.testing.CacheSpec.CacheExecutor) HasStats.hasLoadFailureCount(com.github.benmanes.caffeine.cache.testing.HasStats.hasLoadFailureCount) Population(com.github.benmanes.caffeine.cache.testing.CacheSpec.Population) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) Loader(com.github.benmanes.caffeine.cache.testing.CacheSpec.Loader) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec) CacheValidationListener(com.github.benmanes.caffeine.cache.testing.CacheValidationListener) ImmutableList(com.google.common.collect.ImmutableList) HasStats.hasLoadSuccessCount(com.github.benmanes.caffeine.cache.testing.HasStats.hasLoadSuccessCount) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) CheckNoWriter(com.github.benmanes.caffeine.cache.testing.CheckNoWriter) HasStats.hasHitCount(com.github.benmanes.caffeine.cache.testing.HasStats.hasHitCount) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) HasRemovalNotifications.hasRemovalNotifications(com.github.benmanes.caffeine.cache.testing.HasRemovalNotifications.hasRemovalNotifications) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) HasStats.hasMissCount(com.github.benmanes.caffeine.cache.testing.HasStats.hasMissCount) Implementation(com.github.benmanes.caffeine.cache.testing.CacheSpec.Implementation) Ints(com.google.common.primitives.Ints) RemovalNotification(com.github.benmanes.caffeine.cache.testing.RemovalNotification) Matchers.both(org.hamcrest.Matchers.both) ExecutionException(java.util.concurrent.ExecutionException) Listener(com.github.benmanes.caffeine.cache.testing.CacheSpec.Listener) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Compute(com.github.benmanes.caffeine.cache.testing.CacheSpec.Compute) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matchers.equalTo(org.hamcrest.Matchers.equalTo) CacheProvider(com.github.benmanes.caffeine.cache.testing.CacheProvider) Matchers.is(org.hamcrest.Matchers.is) Awaits.await(com.github.benmanes.caffeine.testing.Awaits.await) Assert(org.junit.Assert) Collections(java.util.Collections) CompletionException(java.util.concurrent.CompletionException) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Aggregations

CompletionException (java.util.concurrent.CompletionException)199 Test (org.junit.Test)80 CompletableFuture (java.util.concurrent.CompletableFuture)62 List (java.util.List)52 ArrayList (java.util.ArrayList)51 IOException (java.io.IOException)45 Map (java.util.Map)39 Collection (java.util.Collection)31 ExecutionException (java.util.concurrent.ExecutionException)31 HashMap (java.util.HashMap)30 Collections (java.util.Collections)24 TimeUnit (java.util.concurrent.TimeUnit)22 Collectors (java.util.stream.Collectors)22 FlinkException (org.apache.flink.util.FlinkException)22 Before (org.junit.Before)21 Duration (java.time.Duration)19 Arrays (java.util.Arrays)19 BeforeClass (org.junit.BeforeClass)19 ExecutorService (java.util.concurrent.ExecutorService)18 Nonnull (javax.annotation.Nonnull)17