loader/src/androidTest/java/androidx/loader/content/ModernAsyncTaskTest.java
@LargeTest
    @Test
    public void testException() throws Throwable {
        final CountDownLatch calledOnCancelled = new CountDownLatch(1);
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mModernAsyncTask = new ModernAsyncTask() {
                    @Override
                    protected Object doInBackground(Object[] params) {
                        throw new RuntimeException();
                    }

                    @Override
                    protected void onPostExecute(Object o) {
                        fail("onPostExecute should not be called");
                    }

                    @Override
                    protected void onCancelled(Object o) {
                        calledOnCancelled.countDown();
                    }
                };
            }
        });

        mModernAsyncTask.executeOnExecutor(new Executor() {
            @Override
            public void execute(@NonNull Runnable command) {
                try {
                    command.run();
                    fail("Exception not thrown");
                } catch (Throwable tr) {
                    // expected
                }
            }
        });

        if (!calledOnCancelled.await(5, TimeUnit.SECONDS)) {
            fail("onCancelled not called");
        }
    }
/**
     * Test to ensure that onCancelled is always called instead of onPostExecute when the exception
     * is not suppressed by cancelling the task.
     *
     * @throws Throwable
     */
Test to ensure that onCancelled is always called instead of onPostExecute when the exception
is not suppressed by cancelling the task.

@throws Throwable
Test to ensure that onCancelled is always called instead of onPostExecute when the exception is not suppressed by cancelling the task . @throws Throwable
@LargeTest
    @Test
    public void testException() throws Throwable {
        final CountDownLatch calledOnCancelled = new CountDownLatch(1);
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mModernAsyncTask = new ModernAsyncTask() {
                    @Override
                    protected Object doInBackground(Object[] params) {
                        throw new RuntimeException();
                    }
                    @Override
                    protected void onPostExecute(Object o) {
                        fail("onPostExecute should not be called");
                    }
                    @Override
                    protected void onCancelled(Object o) {
                        calledOnCancelled.countDown();
                    }
                };
            }
        });
        mModernAsyncTask.executeOnExecutor(new Executor() {
            @Override
            public void execute(@NonNull Runnable command) {
                try {
                    command.run();
                    fail("Exception not thrown");
                } catch (Throwable tr) {
                }
            }
        });
        if (!calledOnCancelled.await(5, TimeUnit.SECONDS)) {
            fail("onCancelled not called");
        }
    }
@ LargeTest @ Test public void testException ( ) throws Throwable { final CountDownLatch calledOnCancelled = new CountDownLatch ( 1 ) ; InstrumentationRegistry . getInstrumentation ( ) . runOnMainSync ( new Runnable ( ) { @ Override public void run ( ) { mModernAsyncTask = new ModernAsyncTask ( ) { @ Override protected Object doInBackground ( Object [ ] params ) { throw new RuntimeException ( ) ; } @ Override protected void onPostExecute ( Object o ) { fail ( "onPostExecute should not be called" ) ; } @ Override protected void onCancelled ( Object o ) { calledOnCancelled . countDown ( ) ; } } ; } } ) ; mModernAsyncTask . executeOnExecutor ( new Executor ( ) { @ Override public void execute ( @ NonNull Runnable command ) { try { command . run ( ) ; fail ( "Exception not thrown" ) ; } catch ( Throwable tr ) { } } } ) ; if ( ! calledOnCancelled . await ( 5 , TimeUnit . SECONDS ) ) { fail ( "onCancelled not called" ) ; } }
Test to ensure that onCancelled is always called instead of onPostExecute when the exception
 is not suppressed by cancelling the task.
Test to ensure that onCancelled is always called instead of onPostExecute when the exception is not suppressed by cancelling the task .
// expected