public void taskChainDelayTest() { final long t = System.currentTimeMillis(); Task sleeper3sec = new Task(Task.HIGH_PRIORITY) { protected Object exec(Object in) { try { L.i("sleeper3", "start sleep"); Thread.sleep(3000); L.i("sleeper3", "end sleep"); } catch (Exception e) { L.e("Problem with sleeper4", "", e); } return in; } }; Task sleeper3chain = new Task(Task.FASTLANE_PRIORITY) { protected Object exec(Object in) { L.i("getter3sec", "completed"); return new Long(System.currentTimeMillis() - t); } }; Task sleeper4sec = new Task(Task.HIGH_PRIORITY) { protected Object exec(Object in) { try { L.i("sleeper4", "start sleep"); Thread.sleep(4000); L.i("sleeper4", "end sleep"); } catch (Exception e) { L.e("Problem with sleeper3", "", e); } return in; } }; Task sleeper4chain = new Task(Task.HIGH_PRIORITY) { protected Object exec(Object in) { L.i("getter10sec", "completed"); in = new Long(System.currentTimeMillis() - t); return in; } }; sleeper3sec.chain(sleeper3chain); sleeper4sec.chain(sleeper4chain); sleeper4sec.fork(); sleeper3sec.fork(); Task[] sleepers = {sleeper4sec, sleeper3sec}; try { Task.joinAll(sleepers); assertTrue("joinAll() waiting >4sec", System.currentTimeMillis() - t >= 4000); Object long4 = sleeper4chain.get(); Object long3 = sleeper3chain.get(); assertTrue("sleeper4chain should be non-null", long4 != null); assertTrue("sleeper3chain should be non-null", long3 != null); assertEquals("sleeper4 should return Long", Long.class, long4.getClass()); assertEquals("sleeper3 should return Long", Long.class, long3.getClass()); final long runtime4 = ((Long) long4).longValue(); final long runtime3 = ((Long) long3).longValue(); assertTrue( "4sec chain delay task " + runtime4 + " should be slower than 3sec " + runtime3 + " delay task", runtime3 < runtime4); assertTrue( "4sec chain delay task " + runtime4 + " should be nearly 1 sec after 3sec " + runtime3 + " delay task", Math.abs(runtime4 - runtime3 - 1000) < 100); } catch (Exception e) { fail("Problem running taskChainDelayTest: " + e); } }
// @Test public void testJoinAll() { System.out.println("joinAll"); final Task task1 = new Task(Task.FASTLANE_PRIORITY, "1") { protected Object exec(Object in) { return (String) in + "2"; } }; final Task task2 = new Task(Task.FASTLANE_PRIORITY, "3") { protected Object exec(Object in) { return in + "4"; } }; final Task task3 = new Task(Task.FASTLANE_PRIORITY, "A") { protected Object exec(Object in) { return (String) in + "2"; } }; final Task task4 = new Task(Task.FASTLANE_PRIORITY, "B") { protected Object exec(Object in) { return in + "3"; } }; final Task task5a = new Task(Task.FASTLANE_PRIORITY, "fail_a") { protected Object exec(Object in) { this.cancel("testing"); return in + "fail"; } }; final Task task5b = new Task(Task.FASTLANE_PRIORITY, "fail_b") { protected Object exec(Object in) { this.cancel("testing"); return in + "fail"; } }; final Task task6 = new Task(Task.FASTLANE_PRIORITY, "slow") { protected Object exec(Object in) { double j = Double.MIN_VALUE; for (int i = 0; i < 1000000; i++) { j = j + i * 1.01; } return "" + j; } }; try { task3.fork(); Task[] tasks = {task1, task2}; Task.joinAll(tasks, 102); assertEquals("1234", (String) task1.get() + (String) task2.get()); Task[] moreTasks = {task3, task4}; Task.joinAll(moreTasks, 102); assertEquals("A2B3", (String) task3.get() + (String) task4.get()); Task[] exceptionTasks1 = {task1, task2, task3, task4, task5a}; try { Task.joinAll(exceptionTasks1, 104); // Correct execution path } catch (CancellationException e) { fail("joinAll() exceptoinTasks1 should not have thrown an CancellationException, but did"); } Task[] exceptionTasks2 = {task1, task2, task3, task4, task5b}; try { Task.joinAll(exceptionTasks2, 105); // Correct execution path } catch (CancellationException e) { fail("joinAll() exceptoinTasks2 should have thrown an CancellationException, but did not"); } Task[] slowTasks = {task1, task6, task3}; try { Task.joinAll(slowTasks, 9); fail("joinAll() should have thrown a TimeoutException, but did not"); } catch (TimeoutException e) { // Correct execution path } try { Task.joinAll(null, 10); fail("joinAll() should have thrown an IllegalArgumentException for null, but did not"); } catch (IllegalArgumentException e) { // Correct execution path } try { Task.joinAll(tasks, -1); fail( "joinAll() should have thrown an IllegalArgumentException for negative timeout, but did not"); } catch (IllegalArgumentException e) { // Correct execution path } } catch (Exception ex) { ex.printStackTrace(); fail("Can not joinAll: " + ex); } }