|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 jdk.internal.net.http; |
| 25 | + |
| 26 | +import jdk.internal.net.http.HttpClientImpl.DelegatingExecutor; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import java.util.concurrent.CountDownLatch; |
| 32 | +import java.util.concurrent.Executor; |
| 33 | +import java.util.concurrent.ExecutorService; |
| 34 | +import java.util.concurrent.LinkedBlockingQueue; |
| 35 | +import java.util.concurrent.RejectedExecutionException; |
| 36 | +import java.util.concurrent.ThreadPoolExecutor; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | +import java.util.concurrent.atomic.AtomicInteger; |
| 39 | +import java.util.function.BiConsumer; |
| 40 | + |
| 41 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 45 | +import static org.junit.jupiter.api.Assertions.fail; |
| 46 | + |
| 47 | +public class DelegatingExecutorTest { |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testInlineExecution() { |
| 51 | + Thread callSiteThread = Thread.currentThread(); |
| 52 | + Thread[] runSiteThreadRef = {null}; |
| 53 | + new DelegatingExecutor(() -> false, null, null) |
| 54 | + .execute(() -> runSiteThreadRef[0] = Thread.currentThread()); |
| 55 | + assertSame(callSiteThread, runSiteThreadRef[0]); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testDelegateDeferral() { |
| 60 | + ImmediateExecutor delegate = new ImmediateExecutor(); |
| 61 | + Runnable task = () -> {}; |
| 62 | + new DelegatingExecutor(() -> true, delegate, null).execute(task); |
| 63 | + delegate.assertReception(task); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testRejectedExecutionException() throws InterruptedException { |
| 68 | + |
| 69 | + // Create a deterministically throwing task |
| 70 | + RuntimeException error = new RejectedExecutionException(); |
| 71 | + FirstThrowingAndThenCompletingRunnable task = new FirstThrowingAndThenCompletingRunnable(error); |
| 72 | + |
| 73 | + // Create a recording delegate |
| 74 | + ImmediateExecutor delegate = new ImmediateExecutor(); |
| 75 | + |
| 76 | + // Create a recording error handler |
| 77 | + List<Runnable> reportedTasks = new ArrayList<>(); |
| 78 | + List<Throwable> reportedErrors = new ArrayList<>(); |
| 79 | + BiConsumer<Runnable, Throwable> errorHandler = (task_, error_) -> { |
| 80 | + synchronized (this) { |
| 81 | + reportedTasks.add(task_); |
| 82 | + reportedErrors.add(error_); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + // Verify the initial failing execution |
| 87 | + new DelegatingExecutor(() -> true, delegate, errorHandler).execute(task); |
| 88 | + delegate.assertReception(task); |
| 89 | + |
| 90 | + // Verify fallback to the async. pool |
| 91 | + assertEquals(1, reportedTasks.size()); |
| 92 | + assertSame(task, reportedTasks.getFirst()); |
| 93 | + assertEquals(1, reportedErrors.size()); |
| 94 | + assertSame(error, reportedErrors.getFirst()); |
| 95 | + boolean completed = task.completionLatch.await(5, TimeUnit.SECONDS); |
| 96 | + assertTrue(completed); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testNotRejectedExecutionException() { |
| 102 | + |
| 103 | + // Create a deterministically throwing task |
| 104 | + RuntimeException error = new RuntimeException(); |
| 105 | + FirstThrowingAndThenCompletingRunnable task = new FirstThrowingAndThenCompletingRunnable(error); |
| 106 | + |
| 107 | + // Create a recording delegate |
| 108 | + ImmediateExecutor delegate = new ImmediateExecutor(); |
| 109 | + |
| 110 | + // Verify the immediate exception propagation |
| 111 | + Throwable thrownError = assertThrows( |
| 112 | + Throwable.class, |
| 113 | + () -> new DelegatingExecutor(() -> true, delegate, null).execute(task)); |
| 114 | + delegate.assertReception(task); |
| 115 | + assertSame(error, thrownError); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + private static final class ImmediateExecutor implements Executor { |
| 120 | + |
| 121 | + private final List<Runnable> receivedTasks = new ArrayList<>(); |
| 122 | + |
| 123 | + @Override |
| 124 | + public synchronized void execute(Runnable task) { |
| 125 | + receivedTasks.add(task); |
| 126 | + task.run(); |
| 127 | + } |
| 128 | + |
| 129 | + private synchronized void assertReception(Runnable... tasks) { |
| 130 | + assertSame(tasks.length, receivedTasks.size()); |
| 131 | + for (int taskIndex = 0; taskIndex < tasks.length; taskIndex++) { |
| 132 | + assertSame(tasks[taskIndex], receivedTasks.get(taskIndex)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + private static final class FirstThrowingAndThenCompletingRunnable implements Runnable { |
| 139 | + |
| 140 | + private final AtomicInteger invocationCounter = new AtomicInteger(0); |
| 141 | + |
| 142 | + private final CountDownLatch completionLatch = new CountDownLatch(1); |
| 143 | + |
| 144 | + private final RuntimeException exception; |
| 145 | + |
| 146 | + private FirstThrowingAndThenCompletingRunnable(RuntimeException exception) { |
| 147 | + this.exception = exception; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void run() { |
| 152 | + switch (invocationCounter.getAndIncrement()) { |
| 153 | + case 0: throw exception; |
| 154 | + case 1: { completionLatch.countDown(); break; } |
| 155 | + default: fail(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testDelegateShutdown() { |
| 163 | + AtomicInteger invocationCounter = new AtomicInteger(); |
| 164 | + try (ExecutorService delegate = new ThreadPoolExecutor(1, 1, 1, TimeUnit.DAYS, new LinkedBlockingQueue<>()) { |
| 165 | + @Override |
| 166 | + public void shutdown() { |
| 167 | + invocationCounter.incrementAndGet(); |
| 168 | + super.shutdown(); |
| 169 | + } |
| 170 | + }) { |
| 171 | + new DelegatingExecutor(() -> true, delegate, null).shutdown(); |
| 172 | + assertEquals(1, invocationCounter.get()); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments