loader/src/androidTest/java/androidx/loader/content/ModernAsyncTaskTest.java
@LargeTest
    @Test
    public void testCancellationWithException() throws Throwable {
        final CountDownLatch readyToCancel = new CountDownLatch(1);
        final CountDownLatch readyToThrow = new CountDownLatch(1);
        final CountDownLatch calledOnCancelled = new CountDownLatch(1);
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mModernAsyncTask = new ModernAsyncTask() {
                    @Override
                    protected Object doInBackground(Object[] params) {
                        readyToCancel.countDown();
                        try {
                            readyToThrow.await();
                        } catch (InterruptedException e) { }
                        // This exception is expected to be caught and ignored
                        throw new RuntimeException();
                    }

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

        mModernAsyncTask.execute();
        if (!readyToCancel.await(5, TimeUnit.SECONDS)) {
            fail("Test failure: doInBackground did not run in time.");
        }
        mModernAsyncTask.cancel(false);
        readyToThrow.countDown();
        if (!calledOnCancelled.await(5, TimeUnit.SECONDS)) {
            fail("onCancelled not called!");
        }
    }
/**
     * Test to ensure that onCancelled is always called, even if doInBackground throws an exception.
     *
     * @throws Throwable
     */
Test to ensure that onCancelled is always called, even if doInBackground throws an exception.
@throws Throwable
Test to ensure that onCancelled is always called even if doInBackground throws an exception . @throws Throwable
@LargeTest
    @Test
    public void testCancellationWithException() throws Throwable {
        final CountDownLatch readyToCancel = new CountDownLatch(1);
        final CountDownLatch readyToThrow = new CountDownLatch(1);
        final CountDownLatch calledOnCancelled = new CountDownLatch(1);
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mModernAsyncTask = new ModernAsyncTask() {
                    @Override
                    protected Object doInBackground(Object[] params) {
                        readyToCancel.countDown();
                        try {
                            readyToThrow.await();
                        } catch (InterruptedException e) { }
                        throw new RuntimeException();
                    }
                    @Override
                    protected void onCancelled(Object o) {
                        calledOnCancelled.countDown();
                    }
                };
            }
        });
        mModernAsyncTask.execute();
        if (!readyToCancel.await(5, TimeUnit.SECONDS)) {
            fail("Test failure: doInBackground did not run in time.");
        }
        mModernAsyncTask.cancel(false);
        readyToThrow.countDown();
        if (!calledOnCancelled.await(5, TimeUnit.SECONDS)) {
            fail("onCancelled not called!");
        }
    }
@ LargeTest @ Test public void testCancellationWithException ( ) throws Throwable { final CountDownLatch readyToCancel = new CountDownLatch ( 1 ) ; final CountDownLatch readyToThrow = new CountDownLatch ( 1 ) ; final CountDownLatch calledOnCancelled = new CountDownLatch ( 1 ) ; InstrumentationRegistry . getInstrumentation ( ) . runOnMainSync ( new Runnable ( ) { @ Override public void run ( ) { mModernAsyncTask = new ModernAsyncTask ( ) { @ Override protected Object doInBackground ( Object [ ] params ) { readyToCancel . countDown ( ) ; try { readyToThrow . await ( ) ; } catch ( InterruptedException e ) { } throw new RuntimeException ( ) ; } @ Override protected void onCancelled ( Object o ) { calledOnCancelled . countDown ( ) ; } } ; } } ) ; mModernAsyncTask . execute ( ) ; if ( ! readyToCancel . await ( 5 , TimeUnit . SECONDS ) ) { fail ( "Test failure: doInBackground did not run in time." ) ; } mModernAsyncTask . cancel ( false ) ; readyToThrow . countDown ( ) ; if ( ! calledOnCancelled . await ( 5 , TimeUnit . SECONDS ) ) { fail ( "onCancelled not called!" ) ; } }
Test to ensure that onCancelled is always called, even if doInBackground throws an exception.
Test to ensure that onCancelled is always called even if doInBackground throws an exception .
// This exception is expected to be caught and ignored