Awex { public <Result, Progress> Promise<Result, Progress> submit(final Task<Result, Progress> task) { task.initialize(this); PoolStateImpl poolState = extractPoolState(); mPoolPolicy.onTaskAdded(poolState, task); poolState.recycle(); return task.getPromise(); } Awex(ThreadHelper threadHelper, Logger logger, PoolPolicy poolPolicy); Promise<Result, Progress> submit(final Task<Result, Progress> task); void cancel(Task<Result, Progress> task, boolean mayInterrupt); @SafeVarargs final Promise<Result, Progress> sequentiallyUntilFirstDone(final
    Task<Result, Progress>... tasks); Promise<Result, Progress> sequentiallyUntilFirstDone(final
    Collection<Task<Result, Progress>> tasks); @SafeVarargs final Promise<Collection<Result>, Progress> allOf(Promise<Result, Progress>... promises); Promise<Collection<Result>, Progress> allOf(Collection<Promise<Result, Progress>> promises); @SafeVarargs final Promise<Result, Progress> anyOf(Promise<Result, Progress>... promises); Promise<Result, Progress> anyOf(Collection<Promise<Result, Progress>> promises); @SafeVarargs final Promise<MultipleResult<Result, Progress>, Progress> afterAll(Promise<Result, Progress>... promises); Promise<MultipleResult<Result, Progress>, Progress> afterAll(Collection<Promise<Result, Progress>> promises); @SuppressWarnings("unchecked") Promise<Result, Progress> of(Result value); ResolvablePromise<Result, Progress> newAwexPromise(); @SuppressWarnings("unchecked") Promise<Result, Progress> absent(); @Override String toString();  }
@Test public void shouldExecuteSimpleTask() throws Exception { setUpAwex(); mTaskPromise = mAwex.submit(new Task<Integer, Float>() { @Override protected Integer run() throws InterruptedException { return SOME_VALUE; } }); assertEquals(SOME_VALUE, mTaskPromise.getResult()); }
@Test public void shouldExecuteSimpleVoidTask() throws Exception { setUpAwex(); Promise<Void, Void> promise = mAwex.submit(new VoidTask() { @Override protected void runWithoutResult() throws InterruptedException { mResult = SOME_VALUE; } }); promise.getResult(); assertEquals(SOME_VALUE, mResult); }
@Test(expected = IllegalArgumentException.class) public void shouldExecuteAndRejectPromiseOfFailingTask() throws Exception { setUpAwex(); mTaskPromise = mAwex.submit(new Task<Integer, Float>() { @Override protected Integer run() throws InterruptedException { throw new IllegalArgumentException("Argument not valid!"); } }); mTaskPromise.getResult(); }
@Test public void shouldInterruptTaskOnCancel() { setUpAwex(); final Semaphore workIsRunning = new Semaphore(0); Promise<Integer, Float> promise = mAwex.submit(new Task<Integer, Float>() { @Override protected Integer run() throws InterruptedException { workIsRunning.release(); Thread.sleep(10000); return null; } }); workIsRunning.acquireUninterruptibly(); promise.cancelTask(true); assertEquals(Promise.STATE_CANCELLED, promise.getState()); assertFalse(mExecutionFlag); }
@Test public void shouldExecuteFirstlyTaskWithMorePriority() throws Exception { setUpAwex(); mExceptedCollection = new ArrayList<>(); mResultCollection = new ArrayList<>(); final Semaphore blocker = new Semaphore(0); mAwex.submit(new VoidTask() { @Override protected void runWithoutResult() throws InterruptedException { blocker.acquireUninterruptibly(); } }); VoidTask lowPriorityTask = new VoidTask(Task.PRIORITY_LOW) { @Override protected void runWithoutResult() throws InterruptedException { mResultCollection.add(getId()); } }; mAwex.submit(lowPriorityTask); VoidTask highPriorityTask = new VoidTask(Task.PRIORITY_HIGH) { @Override protected void runWithoutResult() throws InterruptedException { mResultCollection.add(getId()); } }; mAwex.submit(highPriorityTask); mExceptedCollection.add(highPriorityTask.getId()); mExceptedCollection.add(lowPriorityTask.getId()); blocker.release(); lowPriorityTask.getPromise().getResult(); highPriorityTask.getPromise().getResult(); assertEquals(mExceptedCollection.get(0), mResultCollection.get(0)); assertEquals(mExceptedCollection.get(1), mResultCollection.get(1)); }
@Test(expected = IllegalStateException.class) public void shouldFailToSubmitTwoTimesTheSameTask() { setUpAwex(); VoidTask someTask = new VoidTask() { @Override protected void runWithoutResult() throws InterruptedException { } }; mAwex.submit(someTask); mAwex.submit(someTask); }
@Test(timeout = 500) public void shouldLetToSubmitAgainTheSameTaskIfResetWhenFinished() throws Exception { setUpAwex(); VoidTask someTask = new VoidTask() { @Override protected void runWithoutResult() throws InterruptedException { } }; mAwex.submit(someTask); someTask.getPromise().getResult(); someTask.reset(); mAwex.submit(someTask); someTask.getPromise().getResult(); assertEquals(Promise.STATE_RESOLVED, someTask.getPromise().getState()); }
@Test(timeout = 1000) public void shouldExecuteAsRealTimeWhenAllThreadsBusy() throws Exception { setUpAwex(); final Semaphore semaphore = new Semaphore(0); VoidTask someWaitingTask = new VoidTask() { @Override protected void runWithoutResult() throws InterruptedException { semaphore.acquire(); } }; VoidTask realTimeTask = new VoidTask(Task.PRIORITY_REAL_TIME) { @Override protected void runWithoutResult() throws InterruptedException { } }; Promise<Void, Void> waitingPromise = mAwex.submit(someWaitingTask); Promise<Void, Void> realTimePromise = mAwex.submit(realTimeTask); realTimePromise.getResult(); semaphore.release(); waitingPromise.getResult(); }
@Test(timeout = 1000) public void shouldExecuteAsRealTimeWhenWorkQueueHasQueuedTasks() throws Exception { setUpAwex(); final Semaphore semaphore = new Semaphore(0); VoidTask someWaitingTask = new VoidTask() { @Override protected void runWithoutResult() throws InterruptedException { semaphore.acquire(); } }; VoidTask realTimeTask = new VoidTask(Task.PRIORITY_REAL_TIME) { @Override protected void runWithoutResult() throws InterruptedException { } }; Promise<Void, Void> waitingPromise = mAwex.submit(someWaitingTask); mAwex.submit(new VoidTask() { @Override protected void runWithoutResult() throws InterruptedException { } }); Promise<Void, Void> realTimePromise = mAwex.submit(realTimeTask); realTimePromise.getResult(); semaphore.release(); waitingPromise.getResult(); }
@Test public void shouldAbortTaskInQueueIfTimeoutExpires() { setUpAwex(); final Semaphore semaphore = new Semaphore(0); mAwex.submit(new Task<Integer, Float>() { @Override protected Integer run() throws InterruptedException { semaphore.acquire(); return null; } }); mTaskPromise = mAwex.submit(new Task<Integer, Float>(Task.PRIORITY_NORMAL, 500, 500) { @Override protected Integer run() throws InterruptedException { return SOME_VALUE; } }); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } assertTrue(mTaskPromise.isCancelled()); semaphore.release(); }
@Test public void shouldAbortTaskInExecutionIfTimeoutExpires() { setUpAwex(); final Semaphore semaphore = new Semaphore(0); mTaskPromise = mAwex.submit(new Task<Integer, Float>(Task.PRIORITY_NORMAL, 500, 500) { @Override protected Integer run() throws InterruptedException { semaphore.acquire(); return SOME_VALUE; } }); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } assertTrue(mTaskPromise.isCancelled()); semaphore.release(); }