RetryingExecutor implements Executor { @Override public void execute(@NonNull final Runnable runnable) { execute(new Operation() { @Override public int run() { runnable.run(); return RESULT_FINISHED; } }); } RetryingExecutor(@NonNull Handler scheduler, @NonNull Executor executor); static RetryingExecutor newSerialExecutor(Looper looper); @Override void execute(@NonNull final Runnable runnable); void execute(@NonNull Operation operation); void execute(@NonNull Operation... operations); void setPaused(boolean isPaused); static final int RESULT_FINISHED; static final int RESULT_RETRY; static final int RESULT_CANCEL; }
@Test public void testExecuteRunnable() { Runnable runnable = mock(Runnable.class); executor.execute(runnable); verify(runnable).run(); }
@Test public void testExecuteOperation() { TestOperation operation = new TestOperation(RetryingExecutor.RESULT_FINISHED); executor.execute(operation); assertEquals(1, operation.runCount); }
@Test public void testExecuteChainedOperations() { TestOperation fistOperation = new TestOperation(RetryingExecutor.RESULT_FINISHED); TestOperation secondOperation = new TestOperation(RetryingExecutor.RESULT_RETRY); TestOperation thirdOperation = new TestOperation(RetryingExecutor.RESULT_FINISHED); executor.execute(fistOperation, secondOperation, thirdOperation); assertEquals(1, fistOperation.runCount); assertEquals(1, secondOperation.runCount); assertEquals(0, thirdOperation.runCount); secondOperation.result = RetryingExecutor.RESULT_FINISHED; advanceLooper(30000); assertEquals(1, fistOperation.runCount); assertEquals(2, secondOperation.runCount); assertEquals(1, thirdOperation.runCount); }
@Test public void testExecuteChainedOperationsCancel() { TestOperation fistOperation = new TestOperation(RetryingExecutor.RESULT_CANCEL); TestOperation secondOperation = new TestOperation(RetryingExecutor.RESULT_FINISHED); TestOperation thirdOperation = new TestOperation(RetryingExecutor.RESULT_FINISHED); executor.execute(fistOperation, secondOperation, thirdOperation); assertEquals(1, fistOperation.runCount); assertEquals(0, secondOperation.runCount); assertEquals(0, thirdOperation.runCount); advanceLooper(30000); assertEquals(1, fistOperation.runCount); assertEquals(0, secondOperation.runCount); assertEquals(0, thirdOperation.runCount); }
@Test public void testExecuteOperationRetry() { TestOperation operation = new TestOperation(RetryingExecutor.RESULT_RETRY); executor.execute(operation); assertEquals(1, operation.runCount); advanceLooper(30000); assertEquals(2, operation.runCount); advanceLooper(60000); assertEquals(3, operation.runCount); operation.result = RetryingExecutor.RESULT_FINISHED; advanceLooper(120000); assertEquals(4, operation.runCount); Shadows.shadowOf(mainLooper).runToEndOfTasks(); assertEquals(4, operation.runCount); }