Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3eab82e7 authored by Garfield Tan's avatar Garfield Tan
Browse files

Fix the use of WakeLock.

WakeLock is reference counted, so we need to call release() once for
each acquire(). Otherwise when WakeLock is GC'ed it kills DocumentsUI
by Log.wtf(). Unit tests will then fail to run on eng build.

Test: Tests pass.
Change-Id: I98889e1d99c9b814abf0746de1cfbc59737f05d2
(cherry picked from commit 8b98ab7d)
parent e77419c6
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -183,12 +183,12 @@ public class FileOperationService extends Service implements Job.Listener {
                return;
            }

            mWakeLock.acquire();

            assert (job != null);
            if (DEBUG) Log.d(TAG, "Scheduling job " + job.id + ".");
            Future<?> future = getExecutorService(operation.getOpType()).submit(job);
            mRunning.put(jobId, new JobRecord(job, future));

            mWakeLock.acquire();
        }
    }

@@ -245,6 +245,11 @@ public class FileOperationService extends Service implements Job.Listener {
        assert(record != null);
        record.job.cleanup();

        mWakeLock.release();
        if (!mWakeLock.isHeld()) {
            mWakeLock = null;
        }

        if (mRunning.isEmpty()) {
            shutdown();
        }
@@ -256,8 +261,6 @@ public class FileOperationService extends Service implements Job.Listener {
     */
    private void shutdown() {
        if (DEBUG) Log.d(TAG, "Shutting down. Last serviceId was " + mLastServiceId);
        mWakeLock.release();
        mWakeLock = null;

        // Turns out, for us, stopSelfResult always returns false in tests,
        // so we can't guard executor shutdown. For this reason we move
+5 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static junit.framework.Assert.fail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Delayed;
@@ -137,8 +138,11 @@ public class TestScheduledExecutorService implements ScheduledExecutorService {
    }

    public void runAll() {
        for (TestFuture future : scheduled) {
        final Iterator<TestFuture> iter = scheduled.iterator();
        while (iter.hasNext()) {
            TestFuture future = iter.next();
            future.runnable.run();
            iter.remove();
        }
    }

+7 −1
Original line number Diff line number Diff line
@@ -90,10 +90,16 @@ public class FileOperationServiceTest extends ServiceTestCase<FileOperationServi

    @Override
    protected void tearDown() {
        // Release all possibly held wake lock here
        mExecutor.runAll();
        mDeletionExecutor.runAll();

        // There are lots of progress notifications generated in this test case.
        // Dismiss all of them here.
        while (mHandler.hasScheduledMessage()) {
            mHandler.dispatchAllMessages();
        }
    }

    public void testRunsCopyJobs() throws Exception {
        startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC));