|
18 | 18 | import it.unibo.alchemist.model.positions.Euclidean2DPosition; |
19 | 19 | import org.junit.jupiter.api.Test; |
20 | 20 |
|
| 21 | +import java.util.ArrayList; |
21 | 22 | import java.util.List; |
22 | 23 | import java.util.Map; |
23 | 24 | import java.util.concurrent.CountDownLatch; |
|
27 | 28 | import java.util.concurrent.Future; |
28 | 29 | import java.util.concurrent.TimeUnit; |
29 | 30 | import java.util.concurrent.TimeoutException; |
30 | | -import java.util.concurrent.atomic.AtomicBoolean; |
31 | 31 |
|
32 | 32 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
33 | 33 | import static org.junit.jupiter.api.Assertions.assertTrue; |
@@ -55,8 +55,7 @@ void testConcurrentGetContentsAndModification() throws InterruptedException { |
55 | 55 | final int numberOfOperations = 100; |
56 | 56 | final ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads); |
57 | 57 | 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); |
60 | 59 | // Start threads that modify the node while others read from it |
61 | 60 | for (int i = 0; i < numberOfThreads; i++) { |
62 | 61 | final int threadId = i; |
@@ -92,24 +91,21 @@ void testConcurrentGetContentsAndModification() throws InterruptedException { |
92 | 91 | })); |
93 | 92 | } |
94 | 93 | // 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 | + } |
109 | 105 | } |
| 106 | + } finally { |
| 107 | + executor.shutdownNow(); |
110 | 108 | } |
111 | | - // No exceptions should have occurred (especially no ConcurrentModificationException) |
112 | | - assertFalse(exceptionOccurred.get(), "No exceptions should occur during concurrent access"); |
113 | 109 | // Verify the node is still in a valid state |
114 | 110 | final Map<Molecule, List<ILsaMolecule>> finalContents = node.getContents(); |
115 | 111 | assertNotNull(finalContents); |
|
0 commit comments