// @Test public void testNotifyTaskForked() throws InterruptedException, Exception { System.out.println("notifyTaskForked"); final Task instance = new Task(Task.HIGH_PRIORITY) { protected Object exec(Object in) { return "result"; } }; final Task t2 = (new Task(Task.HIGH_PRIORITY) { protected Object exec(Object in) { synchronized (instance) { try { instance.wait(200); } catch (InterruptedException ex) { ex.printStackTrace(); fail("Interrupt while waiting notifyTaskForked notification semaphore"); } assertEquals(Task.PENDING, instance.getStatus()); } return "result"; } }) .fork(); Thread.sleep(100); assertEquals("Forked task instance is PENDING", Task.PENDING, instance.getStatus()); t2.cancel("testing"); }
// @Test public void testGetStatus() { System.out.println("getStatus"); final Task instanceA = new Task(Task.FASTLANE_PRIORITY) { protected Object exec(Object in) { return in; } }; final Task instance2 = new Task(Task.FASTLANE_PRIORITY) { protected Object exec(Object in) { try { Thread.sleep(200); } catch (InterruptedException ex) { } return in; } }; final Task instance3 = new Task(Task.FASTLANE_PRIORITY) { protected Object exec(Object in) { try { Thread.sleep(200); } catch (InterruptedException ex) { } return in; } }; assertEquals(Task.PENDING, instanceA.getStatus()); instance2.fork(); instance3.fork(); instanceA.fork(); assertEquals(Task.PENDING, instanceA.getStatus()); instance2.cancel("testing"); try { Thread.sleep(200); } catch (InterruptedException ex) { ex.printStackTrace(); } assertEquals(Task.FINISHED, instanceA.getStatus()); }
// @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); } }