/** * Test to verify that {@link ExecutorsUtils#parallelize(List, Function, int, int, Optional)} throws * {@link ExecutionException} when any of the threads throw and exception */ @Test(expectedExceptions = ExecutionException.class) public void testParallelizeException() throws Exception { List<Integer> nums = ImmutableList.of(3, 5); final int factor = 5; Function<Integer, String> exceptionFunction = new Function<Integer, String>() { @Override public String apply(Integer input) { if (input == 3) { throw new RuntimeException("testParallelizeException thrown for testing"); } return Integer.toString(input * factor); } }; ExecutorsUtils.parallelize(nums, exceptionFunction, 2, 1, Optional.<Logger> absent()); }
/** * Test to verify that {@link ExecutorsUtils#parallelize(List, Function, int, int, Optional)} returns the result in * the same order as the input * */ @Test public void testParallelize() throws Exception { List<Integer> nums = ImmutableList.of(3, 5, 10, 5, 20); final int factor = 5; Function<Integer, String> multiply = new Function<Integer, String>() { @Override public String apply(Integer input) { return Integer.toString(input * factor); } }; List<String> result = ExecutorsUtils.parallelize(nums, multiply, 2, 60, Optional.<Logger> absent()); Assert.assertEquals(Arrays.asList("15", "25", "50", "25", "100"), result); }
/** * Test to verify that {@link ExecutorsUtils#parallelize(List, Function, int, int, Optional)} throws * {@link ExecutionException} when any of the threads timesout. */ @Test(expectedExceptions = ExecutionException.class) public void testParallelizeTimeout() throws Exception { List<Integer> nums = ImmutableList.of(3, 5); final int factor = 5; Function<Integer, String> sleepAndMultiply = new Function<Integer, String>() { @Override public String apply(Integer input) { try { if (input == 5) { TimeUnit.SECONDS.sleep(2); } } catch (InterruptedException e) { throw new RuntimeException(e); } return Integer.toString(input * factor); } }; ExecutorsUtils.parallelize(nums, sleepAndMultiply, 2, 1, Optional.<Logger> absent()); } }