|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.eclipse.aether.util.concurrency; |
| 20 | + |
| 21 | +import java.util.concurrent.Callable; |
| 22 | +import java.util.concurrent.ExecutionException; |
| 23 | +import java.util.concurrent.ExecutorService; |
| 24 | +import java.util.concurrent.Executors; |
| 25 | +import java.util.concurrent.Future; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests that {@link Error}s thrown by callables complete the future instead of leaving it hanging. |
| 35 | + */ |
| 36 | +class SmartExecutorTest { |
| 37 | + private static final Callable<String> BOOM = () -> { |
| 38 | + throw new AssertionError("boom"); |
| 39 | + }; |
| 40 | + |
| 41 | + private static void assertErrorPropagated(SmartExecutor executor) { |
| 42 | + try (SmartExecutor ignored = executor) { |
| 43 | + Future<String> future = executor.submit(BOOM); |
| 44 | + ExecutionException e = assertThrows(ExecutionException.class, future::get); |
| 45 | + assertInstanceOf(AssertionError.class, e.getCause()); |
| 46 | + assertTrue(future.isDone()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void directPropagatesError() { |
| 52 | + assertErrorPropagated(new SmartExecutor.Direct()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void pooledPropagatesError() { |
| 57 | + ExecutorService pool = Executors.newFixedThreadPool(1); |
| 58 | + assertErrorPropagated(new SmartExecutor.Pooled(pool)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void limitedPropagatesError() { |
| 63 | + ExecutorService pool = Executors.newFixedThreadPool(1); |
| 64 | + assertErrorPropagated(new SmartExecutor.Limited(new SmartExecutor.Pooled(pool), 1)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void limitedReleasesPermitAfterError() throws Exception { |
| 69 | + ExecutorService pool = Executors.newFixedThreadPool(1); |
| 70 | + try (SmartExecutor executor = new SmartExecutor.Limited(new SmartExecutor.Pooled(pool), 1)) { |
| 71 | + assertThrows(ExecutionException.class, () -> executor.submit(BOOM).get()); |
| 72 | + // would block forever if the permit leaked |
| 73 | + executor.submit(() -> "ok").get(); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments