// @Test public void testChain() { System.out.println("chain"); final Task instance = new Task(Task.FASTLANE_PRIORITY, "1") { protected Object exec(Object in) { return (String) in + "2"; } }; final Task instance2 = new Task(Task.FASTLANE_PRIORITY, "bad") { protected Object exec(Object in) { return in + "3"; } }; final Task instance3 = new Task(Task.FASTLANE_PRIORITY, "ugly") { protected Object exec(Object in) { return in + "4"; } }; final Task instanceA = new Task(Task.FASTLANE_PRIORITY, "A") { @Override protected Object exec(Object in) { return (String) in + "B"; } }; final Task instance5 = new Task(Task.FASTLANE_PRIORITY, "5") { @Override protected Object exec(Object in) { return (String) in + "6"; } }; final Task instance6 = new Task(Task.FASTLANE_PRIORITY, "BAD") { @Override protected Object exec(Object in) { return in + "7"; } }; try { instance.chain(instance2).chain(instance3); instance.fork().join(200); assertEquals("12", (String) instance.get()); assertEquals("123", (String) instance2.get()); assertEquals("1234", (String) instance3.get()); instanceA.chain(instance5); instanceA.chain(null); instanceA.chain(instance6); instanceA.fork(); assertEquals("AB", (String) instanceA.get()); assertEquals("AB67", (String) instance6.get()); } catch (Exception ex) { ex.printStackTrace(); fail("Can not chain: " + ex); } }
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 testCancelSelf() { System.out.println("cancelSelf"); final Task task1a = new Task(Task.FASTLANE_PRIORITY, "1") { @Override protected Object exec(Object in) { return (String) in + "2"; } }; final Task task1aChain = new Task(Task.UI_PRIORITY) { @Override protected Object exec(Object in) { try { // TEST FOR LOGIC ERROR- you can not set() after background execution completes set("UI"); fail("set() after run should have been stopped"); } catch (Exception e) { } return in; } }; final Task task1b = new Task(Task.FASTLANE_PRIORITY, "3") { protected Object exec(Object in) { return (String) in + "4"; } public void run(Object result) { // TEST FOR LOGIC ERROR- you can not cancel() after background execution completes try { cancel("testing"); fail("cancel() after run should have been stopped"); } catch (Exception e) { } } }; final Task task2 = new Task(Task.FASTLANE_PRIORITY, "5") { @Override protected Object exec(Object in) { cancel("testing"); return in + "6"; } }; final Task task3 = new Task(Task.FASTLANE_PRIORITY, "B") { protected Object exec(Object in) { cancel("testing"); return in + "C"; } }; final Task task4 = new Task(Task.FASTLANE_PRIORITY, "D") { protected Object exec(Object in) { cancel("testing"); return in + "E"; } }; try { task1a.chain(task1aChain); task1a.fork(); Thread.sleep(300); assertEquals("task1a was FINISHED", Task.FINISHED, task1a.getStatus()); assertEquals("task1a result was 12", "12", task1a.get()); task1b.fork().join(); assertEquals("task1b was not EXEC_FINISHED", Task.FINISHED, task1b.getStatus()); task2.fork().join(); assertEquals( "task2 should not have been CANCELED- you can not cancel() yourself", Task.FINISHED, task2.getStatus()); task3.fork().join(); assertNotEquals("task4a should not have been CANCELED", Task.CANCELED, task3.getStatus()); try { task4.fork().join(); fail("Task 4b- join() should throw ClassCastException if Task (not UITask)"); } catch (ClassCastException e) { // Normal execution path } } catch (Exception ex) { ex.printStackTrace(); fail("Can not testCancelSelf: " + ex); } }