This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 90c16f185 Clarify initializer status Javadoc and expand tests
90c16f185 is described below
commit 90c16f185a20c3442b01bcc6e20f33ecb8e96e71
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Nov 18 07:56:33 2026 -0500
Clarify initializer status Javadoc and expand tests
Document that interrupted status queries may return false after
successful
initialization, including queries through MultiBackgroundInitializer.
Cover repeated interrupted queries, cancellation, worker interruption,
and
the default executor's handling of an interrupted caller.
Assisted-By: OpenAI Codex
---
src/changes/changes.xml | 1 +
.../lang3/concurrent/BackgroundInitializer.java | 16 ++++++--
.../concurrent/MultiBackgroundInitializer.java | 10 +++--
.../concurrent/BackgroundInitializerTest.java | 48 ++++++++++++++++++++++
4 files changed, 68 insertions(+), 7 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f87a4efe0..4e459d26e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -100,6 +100,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Prevent search-bound overflow in CharSequenceUtils.lastIndexOf,
including calls through Strings.CS.lastIndexOf.</action>
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Bound speculative initial capacity in Strings.replace by the possible
match count and a 20% growth limit, avoiding premature memory exhaustion for
large replacements.</action>
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Correct Strings Javadoc for case sensitivity, null ordering, prefix
and suffix matching, and search bounds.</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory,
Amar Ghugare">Preserve interrupt status in BackgroundInitializer.isInitialized
(#1879).</action>
<!-- ADD -->
<!-- UPDATE -->
</release>
diff --git
a/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java
b/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java
index 63b9d8ab3..15f7b1048 100644
---
a/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java
+++
b/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java
@@ -330,11 +330,19 @@ protected Exception getTypedException(final Exception e) {
}
/**
- * Tests whether this instance is initialized. Once initialized, always
returns true.
- * If initialization failed then the failure will be cached and this will
never return
- * true.
+ * Tests whether initialization has completed successfully.
*
- * @return true if initialization completed successfully, otherwise false.
+ * <p>
+ * Returns {@code false} if initialization has not started, is still
running, failed, or was canceled.
+ * </p>
+ *
+ * <p>
+ * This method retrieves the result of the completed {@link Future} to
check for failure. Depending on the future implementation, this retrieval may
throw
+ * {@link InterruptedException} even if the result is already available.
In that case, this method returns {@code false} and preserves the calling
thread's
+ * interrupt status. Consequently, a call can return {@code false} even
after a previous call returned {@code true}.
+ * </p>
+ *
+ * @return {@code true} if initialization completed successfully and its
result could be retrieved, otherwise {@code false}.
* @since 3.14.0
*/
@Override
diff --git
a/src/main/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializer.java
b/src/main/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializer.java
index 1eee86716..956897ab9 100644
---
a/src/main/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializer.java
+++
b/src/main/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializer.java
@@ -346,10 +346,14 @@ protected MultiBackgroundInitializerResults initialize()
throws Exception {
}
/**
- * Tests whether this all child {@code BackgroundInitializer} objects are
initialized. Once initialized, always returns true.
+ * Tests whether all child initializers have completed initialization
successfully.
*
- * @return Whether all child {@code BackgroundInitializer} objects
instance are initialized. Once initialized, always returns true. If there are
no child
- * {@code BackgroundInitializer} objects return false.
+ * <p>
+ * Returns {@code false} if there are no child initializers or if any
child's {@link BackgroundInitializer#isInitialized()} returns {@code false}. A
child
+ * query may return {@code false} when interrupted, even after
initialization completed successfully.
+ * </p>
+ *
+ * @return {@code true} if there is at least one child initializer and all
child queries return {@code true}, otherwise {@code false}.
* @since 3.14.0
*/
@Override
diff --git
a/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java
b/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java
index f5e954843..70c63371d 100644
---
a/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java
+++
b/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java
@@ -26,6 +26,7 @@
import java.time.Duration;
import java.util.concurrent.Callable;
+import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
@@ -350,6 +351,37 @@ void testIsInitialized() throws ConcurrentException {
assertTrue(init.isInitialized(), "Not initialized after releasing
latch");
}
+ @Test
+ void testIsInitializedCancelled() throws InterruptedException {
+ final ExecutorService exec = Executors.newSingleThreadExecutor();
+ try {
+ final AbstractBackgroundInitializerTestImpl init =
getBackgroundInitializerTestImpl(exec);
+ init.enableLatch();
+ init.start();
+ assertTrue(init.getFuture().cancel(true));
+ assertThrows(CancellationException.class, init::get);
+ assertFalse(init.isInitialized());
+ } finally {
+ exec.shutdownNow();
+ exec.awaitTermination(1, TimeUnit.SECONDS);
+ }
+ }
+
+ @Test
+ void testIsInitializedFailed() {
+ final AbstractBackgroundInitializerTestImpl init =
getBackgroundInitializerTestImpl();
+ final InterruptedException failure = new
InterruptedException("Background initialization interrupted");
+ init.ex = failure;
+ init.start();
+ try {
+ assertSame(failure, assertThrows(ConcurrentException.class,
init::get).getCause());
+ assertFalse(init.isInitialized());
+ assertFalse(Thread.currentThread().isInterrupted(), "Background
failure interrupted the caller");
+ } finally {
+ Thread.interrupted();
+ }
+ }
+
@Test
void testIsInitializedInterrupted() throws ConcurrentException,
InterruptedException {
final ExecutorService exec = new ThreadPoolExecutor(1, 1, 0,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()) {
@@ -375,6 +407,8 @@ public T get() throws InterruptedException,
ExecutionException {
Thread.currentThread().interrupt();
assertFalse(init.isInitialized());
assertTrue(Thread.currentThread().isInterrupted(), "Interrupt
status was cleared");
+ assertFalse(init.isInitialized(), "Initialized while the caller
remained interrupted");
+ assertTrue(Thread.currentThread().isInterrupted(), "Repeated query
cleared interrupt status");
Thread.interrupted();
assertTrue(init.isInitialized());
} finally {
@@ -384,6 +418,20 @@ public T get() throws InterruptedException,
ExecutionException {
}
}
+ @Test
+ void testIsInitializedInterruptedDefaultExecutor() throws
ConcurrentException {
+ final AbstractBackgroundInitializerTestImpl init =
getBackgroundInitializerTestImpl();
+ init.start();
+ init.get();
+ try {
+ Thread.currentThread().interrupt();
+ assertTrue(init.isInitialized());
+ assertTrue(Thread.currentThread().isInterrupted(), "Interrupt
status was cleared");
+ } finally {
+ Thread.interrupted();
+ }
+ }
+
/**
* Tests isStarted() after the background task has finished.
*/