Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add defensive copy for Futures allOf() method #2943

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/main/java/io/lettuce/core/internal/Futures.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* without further notice.
*
* @author Mark Paluch
* @author jinkshower
* @since 5.1
*/
public abstract class Futures {
Expand All @@ -21,7 +22,7 @@ private Futures() {
}

/**
* Create a composite {@link CompletableFuture} is composed from the given {@code stages}.
* Create a composite {@link CompletableFuture} that is composed of the given {@code stages}.
*
* @param stages must not be {@code null}.
* @return the composed {@link CompletableFuture}.
Expand All @@ -32,10 +33,11 @@ public static CompletableFuture<Void> allOf(Collection<? extends CompletionStage

LettuceAssert.notNull(stages, "Futures must not be null");

CompletableFuture[] futures = new CompletableFuture[stages.size()];
CompletionStage[] copies = stages.toArray(new CompletionStage[0]);
CompletableFuture[] futures = new CompletableFuture[copies.length];

tishun marked this conversation as resolved.
Show resolved Hide resolved
int index = 0;
for (CompletionStage<?> stage : stages) {
for (CompletionStage<?> stage : copies) {
futures[index++] = stage.toCompletableFuture();
}

Expand Down
36 changes: 36 additions & 0 deletions src/test/java/io/lettuce/core/internal/FuturesUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -16,6 +22,7 @@
* Unit tests for {@link Futures}.
*
* @author Mark Paluch
* @author Tihomir Mateev
*/
class FuturesUnitTests {

Expand Down Expand Up @@ -56,4 +63,33 @@ void awaitAllShouldSetInterruptedBit() {
assertThat(Thread.currentThread().isInterrupted()).isTrue();
}

@Test
void allOfShouldNotThrow() throws InterruptedException {
int threadCount = 100;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
List<Throwable> issues = new ArrayList<>();
List<CompletableFuture<Void>> futures = Collections.synchronizedList(new ArrayList<>());
// Submit multiple threads to perform concurrent operations
CountDownLatch latch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
executorService.submit(() -> {
try {
for (int y = 0; y < 1000; y++) {
futures.add(new CompletableFuture<>());
}

Futures.allOf(futures);
} catch (Exception e) {
issues.add(e);
} finally {
latch.countDown();
}
});
}

// wait for all threads to complete
latch.await();
assertThat(issues).doesNotHaveAnyElementsOfTypes(ArrayIndexOutOfBoundsException.class);
}

}
Loading