@Test @PrepareForTest(HandlerUtils.class) public void rejectedAsyncTask() throws Exception { final Semaphore semaphore = new Semaphore(0); mockStatic(HandlerUtils.class); doAnswer(new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocation) { new Thread("rejectedAsyncTask.handler") { @Override public void run() { ((Runnable) invocation.getArguments()[0]).run(); semaphore.release(); } }.start(); return null; } }).when(HandlerUtils.class); HandlerUtils.runOnUiThread(any(Runnable.class)); DefaultHttpClientCallTask call = mock(DefaultHttpClientCallTask.class); whenNew(DefaultHttpClientCallTask.class).withAnyArguments().thenReturn(call); RejectedExecutionException exception = new RejectedExecutionException(); when(call.executeOnExecutor(any(Executor.class))).thenThrow(exception); DefaultHttpClient httpClient = new DefaultHttpClient(); ServiceCallback serviceCallback = mock(ServiceCallback.class); assertNotNull(httpClient.callAsync("", "", new HashMap<String, String>(), mock(HttpClient.CallTemplate.class), serviceCallback)); semaphore.acquireUninterruptibly(); verify(serviceCallback).onCallFailed(exception); verify(serviceCallback, never()).onCallSucceeded(any(HttpResponse.class)); }
@Override public ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, final ServiceCallback serviceCallback) { final DefaultHttpClientCallTask task = new DefaultHttpClientCallTask(url, method, headers, callTemplate, serviceCallback, this, mCompressionEnabled); try { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } catch (final RejectedExecutionException e) { HandlerUtils.runOnUiThread(new Runnable() { @Override public void run() { serviceCallback.onCallFailed(e); } }); } return new ServiceCall() { @Override public void cancel() { task.cancel(true); } }; }
DefaultHttpClient implements HttpClient, DefaultHttpClientCallTask.Tracker { @Override public ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, final ServiceCallback serviceCallback) { final DefaultHttpClientCallTask task = new DefaultHttpClientCallTask(url, method, headers, callTemplate, serviceCallback, this, mCompressionEnabled); try { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } catch (final RejectedExecutionException e) { HandlerUtils.runOnUiThread(new Runnable() { @Override public void run() { serviceCallback.onCallFailed(e); } }); } return new ServiceCall() { @Override public void cancel() { task.cancel(true); } }; } }
DefaultHttpClient implements HttpClient, DefaultHttpClientCallTask.Tracker { @Override public ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, final ServiceCallback serviceCallback) { final DefaultHttpClientCallTask task = new DefaultHttpClientCallTask(url, method, headers, callTemplate, serviceCallback, this, mCompressionEnabled); try { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } catch (final RejectedExecutionException e) { HandlerUtils.runOnUiThread(new Runnable() { @Override public void run() { serviceCallback.onCallFailed(e); } }); } return new ServiceCall() { @Override public void cancel() { task.cancel(true); } }; } DefaultHttpClient();  DefaultHttpClient(boolean compressionEnabled); }
DefaultHttpClient implements HttpClient, DefaultHttpClientCallTask.Tracker { @Override public ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, final ServiceCallback serviceCallback) { final DefaultHttpClientCallTask task = new DefaultHttpClientCallTask(url, method, headers, callTemplate, serviceCallback, this, mCompressionEnabled); try { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } catch (final RejectedExecutionException e) { HandlerUtils.runOnUiThread(new Runnable() { @Override public void run() { serviceCallback.onCallFailed(e); } }); } return new ServiceCall() { @Override public void cancel() { task.cancel(true); } }; } DefaultHttpClient();  DefaultHttpClient(boolean compressionEnabled); @Override ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, final ServiceCallback serviceCallback); @Override synchronized void onStart(DefaultHttpClientCallTask task); @Override synchronized void onFinish(DefaultHttpClientCallTask task); @Override synchronized void close(); @Override void reopen(); }
DefaultHttpClient implements HttpClient, DefaultHttpClientCallTask.Tracker { @Override public ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, final ServiceCallback serviceCallback) { final DefaultHttpClientCallTask task = new DefaultHttpClientCallTask(url, method, headers, callTemplate, serviceCallback, this, mCompressionEnabled); try { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } catch (final RejectedExecutionException e) { HandlerUtils.runOnUiThread(new Runnable() { @Override public void run() { serviceCallback.onCallFailed(e); } }); } return new ServiceCall() { @Override public void cancel() { task.cancel(true); } }; } DefaultHttpClient();  DefaultHttpClient(boolean compressionEnabled); @Override ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, final ServiceCallback serviceCallback); @Override synchronized void onStart(DefaultHttpClientCallTask task); @Override synchronized void onFinish(DefaultHttpClientCallTask task); @Override synchronized void close(); @Override void reopen(); static final String METHOD_GET; static final String METHOD_POST; static final String METHOD_DELETE; static final String CONTENT_TYPE_KEY; }