Skip to content

Commit edee5ff

Browse files
CopilotDanySK
andcommitted
test(incarnation-sapere): rethrow ExecutionException cause, fix ArrayList import, ensure executor shutdown in finally
Agent-Logs-Url: https://github.com/AlchemistSimulator/Alchemist/sessions/6651c551-7d20-4a2c-aebc-a2f43f4054f8 Co-authored-by: DanySK <1991673+DanySK@users.noreply.github.com>
1 parent e7545e6 commit edee5ff

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

alchemist-incarnation-sapere/src/test/java/it/unibo/alchemist/model/sapere/nodes/LsaNodeConcurrencyTest.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import it.unibo.alchemist.model.positions.Euclidean2DPosition;
1919
import org.junit.jupiter.api.Test;
2020

21+
import java.util.ArrayList;
2122
import java.util.List;
2223
import java.util.Map;
2324
import java.util.concurrent.CountDownLatch;
@@ -27,7 +28,6 @@
2728
import java.util.concurrent.Future;
2829
import java.util.concurrent.TimeUnit;
2930
import java.util.concurrent.TimeoutException;
30-
import java.util.concurrent.atomic.AtomicBoolean;
3131

3232
import static org.junit.jupiter.api.Assertions.assertNotNull;
3333
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -55,8 +55,7 @@ void testConcurrentGetContentsAndModification() throws InterruptedException {
5555
final int numberOfOperations = 100;
5656
final ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
5757
final CountDownLatch latch = new CountDownLatch(numberOfThreads);
58-
final AtomicBoolean exceptionOccurred = new AtomicBoolean(false);
59-
final List<Future<?>> tasks = new java.util.ArrayList<>(numberOfThreads);
58+
final List<Future<?>> tasks = new ArrayList<>(numberOfThreads);
6059
// Start threads that modify the node while others read from it
6160
for (int i = 0; i < numberOfThreads; i++) {
6261
final int threadId = i;
@@ -92,24 +91,21 @@ void testConcurrentGetContentsAndModification() throws InterruptedException {
9291
}));
9392
}
9493
// Wait for all threads to complete
95-
assertTrue(latch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS), "Test should complete within " + TIMEOUT_SECONDS + " seconds");
96-
executor.shutdown();
97-
for (final Future<?> task : tasks) {
98-
try {
99-
task.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
100-
} catch (final InterruptedException e) {
101-
Thread.currentThread().interrupt();
102-
exceptionOccurred.set(true);
103-
} catch (final ExecutionException e) {
104-
exceptionOccurred.set(true);
105-
} catch (final TimeoutException e) {
106-
task.cancel(true);
107-
executor.shutdownNow();
108-
exceptionOccurred.set(true);
94+
try {
95+
assertTrue(latch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS), "Test should complete within " + TIMEOUT_SECONDS + " seconds");
96+
for (final Future<?> task : tasks) {
97+
try {
98+
task.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
99+
} catch (final ExecutionException e) {
100+
throw new AssertionError("Task failed with exception", e.getCause());
101+
} catch (final TimeoutException e) {
102+
task.cancel(true);
103+
throw new AssertionError("Task timed out after " + TIMEOUT_SECONDS + " seconds", e);
104+
}
109105
}
106+
} finally {
107+
executor.shutdownNow();
110108
}
111-
// No exceptions should have occurred (especially no ConcurrentModificationException)
112-
assertFalse(exceptionOccurred.get(), "No exceptions should occur during concurrent access");
113109
// Verify the node is still in a valid state
114110
final Map<Molecule, List<ILsaMolecule>> finalContents = node.getContents();
115111
assertNotNull(finalContents);

0 commit comments

Comments
 (0)