AsyncTaskUtils { @NonNull @SafeVarargs @SuppressWarnings("unchecked") public static <Params, Type extends AsyncTask<Params, ?, ?>> Type execute(String logTag, @NonNull Type asyncTask, Params... params) { try { return (Type) asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); } catch (RejectedExecutionException e) { AppCenterLog.warn(logTag, "THREAD_POOL_EXECUTOR saturated, fall back on SERIAL_EXECUTOR which has an unbounded queue", e); return (Type) asyncTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, params); } } @VisibleForTesting  AsyncTaskUtils(); @NonNull @SafeVarargs @SuppressWarnings("unchecked") static Type execute(String logTag, @NonNull Type asyncTask, Params... params);  }
@Test public void execute() { @SuppressWarnings("unchecked") AsyncTask<Integer, Void, Void> task = mock(AsyncTask.class); when(task.executeOnExecutor(any(Executor.class), anyInt(), anyInt())).thenReturn(task); assertSame(task, AsyncTaskUtils.execute("", task, 1, 2)); verify(task).executeOnExecutor(any(Executor.class), eq(1), eq(2)); }
@Test public void executeFallback() { @SuppressWarnings("unchecked") AsyncTask<Integer, Void, Void> task = mock(AsyncTask.class); mockStatic(AppCenterLog.class); RejectedExecutionException exception = new RejectedExecutionException(); when(task.executeOnExecutor(any(Executor.class), anyInt(), anyInt())).thenThrow(exception).thenReturn(task); assertSame(task, AsyncTaskUtils.execute("", task, 1, 2)); verify(task, times(2)).executeOnExecutor(any(Executor.class), eq(1), eq(2)); verifyStatic(); AppCenterLog.warn(eq(""), anyString(), eq(exception)); }