Copilot commented on code in PR #2138:
URL: https://github.com/apache/maven-resolver/pull/2138#discussion_r4480596986
##########
maven-resolver-api/src/main/java/org/eclipse/aether/collection/CollectResult.java:
##########
@@ -37,9 +37,9 @@ public final class CollectResult {
private final CollectRequest request;
- private List<Exception> exceptions;
+ private final List<Exception> exceptions = new CopyOnWriteArrayList<>();
- private List<DependencyCycle> cycles;
+ private final List<DependencyCycle> cycles = new CopyOnWriteArrayList<>();
Review Comment:
The PR description says access is synchronized (`synchronize get/add`), but
the implementation uses `CopyOnWriteArrayList` instead of synchronization.
Please either (a) update the PR description to reflect the actual approach
(copy-on-write + unmodifiable views), or (b) change the implementation to
explicit synchronization to match the description.
##########
maven-resolver-api/src/main/java/org/eclipse/aether/collection/CollectResult.java:
##########
@@ -66,10 +64,10 @@ public CollectRequest getRequest() {
/**
* Gets the exceptions that occurred while building the dependency graph.
*
- * @return The exceptions that occurred, never {@code null}.
+ * @return An unmodifiable list of the exceptions that occurred, never
{@code null}.
*/
public List<Exception> getExceptions() {
- return exceptions;
+ return Collections.unmodifiableList(exceptions);
}
Review Comment:
Returning an unmodifiable list is a behavioral change: callers that
previously mutated the returned lists (e.g., `clear()`, `add(...)`) will now
get `UnsupportedOperationException`. If this API is consumed externally,
consider preserving backward compatibility by continuing to return a modifiable
thread-safe list (e.g., return the `CopyOnWriteArrayList` directly), or
introduce a new unmodifiable-view accessor while keeping the existing behavior
(and deprecate it if needed).
##########
maven-resolver-api/src/main/java/org/eclipse/aether/collection/CollectResult.java:
##########
@@ -18,9 +18,9 @@
*/
package org.eclipse.aether.collection;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
Review Comment:
The PR description says access is synchronized (`synchronize get/add`), but
the implementation uses `CopyOnWriteArrayList` instead of synchronization.
Please either (a) update the PR description to reflect the actual approach
(copy-on-write + unmodifiable views), or (b) change the implementation to
explicit synchronization to match the description.
##########
maven-resolver-api/src/main/java/org/eclipse/aether/collection/CollectResult.java:
##########
@@ -18,9 +18,9 @@
*/
package org.eclipse.aether.collection;
Review Comment:
`CopyOnWriteArrayList` makes every `add(...)` copy the backing array, which
can become expensive if exceptions/cycles are recorded frequently.
Additionally, `Collections.unmodifiableList(...)` allocates a new wrapper on
every getter call. If write frequency can be non-trivial, consider using a
different concurrency strategy (e.g., `Collections.synchronizedList(new
ArrayList<>())` with appropriate locking) and/or cache the unmodifiable views
in final fields so the getters don't repeatedly allocate wrappers.
##########
maven-resolver-api/src/main/java/org/eclipse/aether/collection/CollectResult.java:
##########
@@ -37,9 +37,9 @@ public final class CollectResult {
private final CollectRequest request;
- private List<Exception> exceptions;
+ private final List<Exception> exceptions = new CopyOnWriteArrayList<>();
Review Comment:
`CopyOnWriteArrayList` makes every `add(...)` copy the backing array, which
can become expensive if exceptions/cycles are recorded frequently.
Additionally, `Collections.unmodifiableList(...)` allocates a new wrapper on
every getter call. If write frequency can be non-trivial, consider using a
different concurrency strategy (e.g., `Collections.synchronizedList(new
ArrayList<>())` with appropriate locking) and/or cache the unmodifiable views
in final fields so the getters don't repeatedly allocate wrappers.
##########
maven-resolver-api/src/main/java/org/eclipse/aether/collection/CollectResult.java:
##########
@@ -91,23 +86,20 @@ public CollectResult addException(Exception exception) {
/**
* Gets the dependency cycles that were encountered while building the
dependency graph.
*
- * @return The dependency cycles in the (raw) graph, never {@code null}.
+ * @return An unmodifiable list of the dependency cycles, never {@code
null}.
*/
public List<DependencyCycle> getCycles() {
- return cycles;
+ return Collections.unmodifiableList(cycles);
Review Comment:
`CopyOnWriteArrayList` makes every `add(...)` copy the backing array, which
can become expensive if exceptions/cycles are recorded frequently.
Additionally, `Collections.unmodifiableList(...)` allocates a new wrapper on
every getter call. If write frequency can be non-trivial, consider using a
different concurrency strategy (e.g., `Collections.synchronizedList(new
ArrayList<>())` with appropriate locking) and/or cache the unmodifiable views
in final fields so the getters don't repeatedly allocate wrappers.
##########
maven-resolver-api/src/main/java/org/eclipse/aether/collection/CollectResult.java:
##########
@@ -91,23 +86,20 @@ public CollectResult addException(Exception exception) {
/**
* Gets the dependency cycles that were encountered while building the
dependency graph.
*
- * @return The dependency cycles in the (raw) graph, never {@code null}.
+ * @return An unmodifiable list of the dependency cycles, never {@code
null}.
*/
public List<DependencyCycle> getCycles() {
- return cycles;
+ return Collections.unmodifiableList(cycles);
}
Review Comment:
Returning an unmodifiable list is a behavioral change: callers that
previously mutated the returned lists (e.g., `clear()`, `add(...)`) will now
get `UnsupportedOperationException`. If this API is consumed externally,
consider preserving backward compatibility by continuing to return a modifiable
thread-safe list (e.g., return the `CopyOnWriteArrayList` directly), or
introduce a new unmodifiable-view accessor while keeping the existing behavior
(and deprecate it if needed).
##########
maven-resolver-api/src/test/java/org/eclipse/aether/collection/CollectResultTest.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eclipse.aether.collection;
+
+import java.util.Iterator;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.eclipse.aether.graph.DependencyCycle;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+
+class CollectResultTest {
+
+ @Test
+ void testConcurrentAddException() throws Exception {
+ CollectResult result = new CollectResult(new CollectRequest());
+
+ int threads = 10;
+ int additionsPerThread = 100;
+
+ ExecutorService executor = Executors.newFixedThreadPool(threads);
+ CountDownLatch start = new CountDownLatch(1);
+
+ try {
+ Future<?>[] futures = new Future<?>[threads];
+
+ for (int i = 0; i < threads; i++) {
+ futures[i] = executor.submit(() -> {
+ start.await();
+
+ for (int j = 0; j < additionsPerThread; j++) {
+ result.addException(new Exception());
+ }
+ return null;
+ });
+ }
+
+ start.countDown();
+
+ for (Future<?> future : futures) {
+ future.get(30, TimeUnit.SECONDS);
+ }
+
+ assertEquals(threads * additionsPerThread,
result.getExceptions().size());
+ } finally {
+ executor.shutdownNow();
+ }
Review Comment:
Using `shutdownNow()` in tests can interrupt tasks and potentially mask
issues or introduce flakiness if execution timing changes. Prefer calling
`executor.shutdown()` and then `awaitTermination(...)` (with a bounded timeout)
to ensure clean termination without forced interruption, especially in
concurrency regression tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]