From 538b1c3cae2945422f3825fdf7af8bd9b76cfa18 Mon Sep 17 00:00:00 2001 From: Harrison Date: Wed, 29 Apr 2020 19:53:02 -0500 Subject: [PATCH] Changed some multithreaded tests to fail if exception is thrown. --- .../UniBiomeIslandGeneratorTest.java | 56 +++++++++++-------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/src/test/java/ca/recrown/islandsurvivalcraft/world/generation/UniBiomeIslandGeneratorTest.java b/src/test/java/ca/recrown/islandsurvivalcraft/world/generation/UniBiomeIslandGeneratorTest.java index 1eada0f..496a29a 100644 --- a/src/test/java/ca/recrown/islandsurvivalcraft/world/generation/UniBiomeIslandGeneratorTest.java +++ b/src/test/java/ca/recrown/islandsurvivalcraft/world/generation/UniBiomeIslandGeneratorTest.java @@ -1,11 +1,13 @@ package ca.recrown.islandsurvivalcraft.world.generation; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.LinkedList; import java.util.Random; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.bukkit.block.Biome; @@ -69,6 +71,7 @@ public class UniBiomeIslandGeneratorTest { } @Test + @Timeout(value = 1, unit = TimeUnit.MINUTES) public void testBiomeGenerationMultithread1608Chunks() { int chunksToDoEach = 268; Runnable g1 = new BiomeGenTask(chunksToDoEach, 0); @@ -79,18 +82,21 @@ public class UniBiomeIslandGeneratorTest { Runnable g6 = new BiomeGenTask(chunksToDoEach, 5); ExecutorService ex = Executors.newFixedThreadPool(6); - ex.execute(g1); - ex.execute(g2); - ex.execute(g3); - ex.execute(g4); - ex.execute(g5); - ex.execute(g6); + LinkedList> tasks = new LinkedList<>(); + tasks.add(ex.submit(g1)); + tasks.add(ex.submit(g2)); + tasks.add(ex.submit(g3)); + tasks.add(ex.submit(g4)); + tasks.add(ex.submit(g5)); + tasks.add(ex.submit(g6)); - try { - ex.shutdown(); - assertTrue(ex.awaitTermination(1, TimeUnit.MINUTES), "timed out."); - } catch (InterruptedException e) { - assertFalse(false); + while (!tasks.isEmpty()) { + try { + tasks.pop().get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + assertFalse(false, e.getCause().getMessage()); + } } } @@ -102,6 +108,7 @@ public class UniBiomeIslandGeneratorTest { } @Test + @Timeout(value = 1, unit = TimeUnit.MINUTES) public void testBiomeGenerationMultithread1608ChunksScatteredColumns() { int chunksToDoEach = 268; Runnable g1 = new BiomeGenTask(chunksToDoEach, 0); @@ -112,18 +119,21 @@ public class UniBiomeIslandGeneratorTest { Runnable g6 = new BiomeGenTask(chunksToDoEach, 10); ExecutorService ex = Executors.newFixedThreadPool(6); - ex.execute(g1); - ex.execute(g2); - ex.execute(g3); - ex.execute(g4); - ex.execute(g5); - ex.execute(g6); + LinkedList> tasks = new LinkedList<>(); + tasks.add(ex.submit(g1)); + tasks.add(ex.submit(g2)); + tasks.add(ex.submit(g3)); + tasks.add(ex.submit(g4)); + tasks.add(ex.submit(g5)); + tasks.add(ex.submit(g6)); - try { - ex.shutdown(); - assertTrue(ex.awaitTermination(1, TimeUnit.MINUTES), "timed out."); - } catch (InterruptedException e) { - assertFalse(false); + while (!tasks.isEmpty()) { + try { + tasks.pop().get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + assertFalse(false, e.getCause().getMessage()); + } } } } \ No newline at end of file