|
| 1 | +/* |
| 2 | + * Copyright 2000-2025 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.server.frontend; |
| 17 | + |
| 18 | +import java.nio.file.Paths; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.CompletableFuture; |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | +import java.util.concurrent.ForkJoinPool; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +import org.junit.After; |
| 27 | +import org.junit.Assert; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import com.vaadin.flow.internal.Pair; |
| 31 | + |
| 32 | +/** |
| 33 | + * Regression test for https://github.com/vaadin/flow/issues/22756 |
| 34 | + * <p> |
| 35 | + * Tests that {@link FrontendUtils#consumeProcessStreams(Process)} does not |
| 36 | + * depend on {@link ForkJoinPool#commonPool()} and can complete even when the |
| 37 | + * common pool is exhausted. |
| 38 | + */ |
| 39 | +public class ForkJoinPoolExhaustionTest { |
| 40 | + |
| 41 | + private final List<CompletableFuture<?>> blockingTasks = new ArrayList<>(); |
| 42 | + |
| 43 | + @After |
| 44 | + public void cleanup() { |
| 45 | + // Cancel all blocking tasks to avoid affecting other tests |
| 46 | + blockingTasks.forEach(f -> f.cancel(true)); |
| 47 | + blockingTasks.clear(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Test that consumeProcessStreams can complete even when |
| 52 | + * ForkJoinPool.commonPool() is exhausted by other blocking tasks. |
| 53 | + * <p> |
| 54 | + * This test verifies that consumeProcessStreams uses a dedicated executor |
| 55 | + * (virtual threads) instead of the common pool. |
| 56 | + */ |
| 57 | + @Test |
| 58 | + public void consumeProcessStreams_shouldNotBeBlockedByExhaustedCommonPool() |
| 59 | + throws Exception { |
| 60 | + // Step 1: Saturate the ForkJoinPool.commonPool() with blocking tasks |
| 61 | + // We need to submit more blocking tasks than the pool's parallelism |
| 62 | + // to ensure all threads are occupied |
| 63 | + int parallelism = ForkJoinPool.commonPool().getParallelism(); |
| 64 | + int numBlockingTasks = parallelism + 2; |
| 65 | + |
| 66 | + // Use a latch to ensure the pool is saturated. We wait for |
| 67 | + // 'parallelism' |
| 68 | + // tasks to start (the max that can run concurrently). The +2 extra |
| 69 | + // tasks |
| 70 | + // will be queued, ensuring the pool is fully occupied. |
| 71 | + CountDownLatch poolSaturated = new CountDownLatch(parallelism); |
| 72 | + |
| 73 | + // Submit (parallelism + 2) blocking tasks to ensure the pool is fully |
| 74 | + // saturated. The +2 accounts for potential compensation threads. |
| 75 | + for (int i = 0; i < numBlockingTasks; i++) { |
| 76 | + CompletableFuture<?> blocker = CompletableFuture.runAsync(() -> { |
| 77 | + try { |
| 78 | + poolSaturated.countDown(); // Signal that this task has |
| 79 | + // started |
| 80 | + // Block for 10 seconds (longer than our test timeout) |
| 81 | + Thread.sleep(10_000); |
| 82 | + } catch (InterruptedException e) { |
| 83 | + Thread.currentThread().interrupt(); |
| 84 | + } |
| 85 | + }); // Uses commonPool by default |
| 86 | + blockingTasks.add(blocker); |
| 87 | + } |
| 88 | + |
| 89 | + // Wait until the pool is saturated (more reliable than fixed sleep) |
| 90 | + Assert.assertTrue("Pool didn't saturate in time", |
| 91 | + poolSaturated.await(5, TimeUnit.SECONDS)); |
| 92 | + |
| 93 | + // Step 2: Start a fast process that outputs immediately and exits |
| 94 | + List<String> cmd = List.of( |
| 95 | + Paths.get(System.getProperty("java.home"), "bin", "java") |
| 96 | + .toFile().getAbsolutePath(), |
| 97 | + "-cp", System.getProperty("java.class.path"), |
| 98 | + FastTestExecutable.class.getName()); |
| 99 | + |
| 100 | + Process process = new ProcessBuilder(cmd).start(); |
| 101 | + |
| 102 | + // Step 3: Call consumeProcessStreams and wait with a reasonable timeout |
| 103 | + // If the implementation uses commonPool, this will timeout because all |
| 104 | + // threads are blocked. |
| 105 | + // If the implementation uses a dedicated executor, this will complete |
| 106 | + // quickly. |
| 107 | + CompletableFuture<Pair<String, String>> streamsFuture = FrontendUtils |
| 108 | + .consumeProcessStreams(process); |
| 109 | + |
| 110 | + // Wait for the process to complete (should be nearly instant) |
| 111 | + boolean processCompleted = process.waitFor(2, TimeUnit.SECONDS); |
| 112 | + Assert.assertTrue("Process should complete within 2 seconds", |
| 113 | + processCompleted); |
| 114 | + |
| 115 | + // Step 4: Try to get the streams with a 2-second timeout |
| 116 | + // This is the key assertion - with the buggy code this will timeout |
| 117 | + try { |
| 118 | + Pair<String, String> streams = streamsFuture.get(2, |
| 119 | + TimeUnit.SECONDS); |
| 120 | + |
| 121 | + // Verify the output was captured correctly |
| 122 | + String stdOut = streams.getFirst(); |
| 123 | + Assert.assertTrue( |
| 124 | + "Expected stdout to contain test output, but was: " |
| 125 | + + stdOut, |
| 126 | + stdOut.contains("FastTestExecutable completed")); |
| 127 | + String stdErr = streams.getSecond(); |
| 128 | + Assert.assertTrue( |
| 129 | + "Expected stdout to contain test output, but was: " |
| 130 | + + stdErr, |
| 131 | + stdErr.contains("FastTestExecutable writing to stderr")); |
| 132 | + |
| 133 | + } catch (java.util.concurrent.TimeoutException e) { |
| 134 | + Assert.fail("consumeProcessStreams should not be blocked by " |
| 135 | + + "exhausted ForkJoinPool.commonPool(). " |
| 136 | + + "This indicates the implementation incorrectly uses " |
| 137 | + + "the common pool instead of a dedicated executor. " |
| 138 | + + "See https://github.com/vaadin/flow/issues/22756"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * A simple test executable that outputs to stdout and exits immediately. |
| 144 | + * Used to test that consumeProcessStreams can capture output quickly. |
| 145 | + */ |
| 146 | + public static class FastTestExecutable { |
| 147 | + public static void main(String... args) { |
| 148 | + System.err.println("FastTestExecutable writing to stderr"); |
| 149 | + System.out.println("FastTestExecutable completed"); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments