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

Commit 7081de59 authored by Dan Egnor's avatar Dan Egnor Committed by Android (Google) Code Review
Browse files

Merge "Add some useful methods to OperationScheduler to inquire into the...

Merge "Add some useful methods to OperationScheduler to inquire into the history, in case you want to second-guess its scheduling."
parents fe641b48 3283401e
Loading
Loading
Loading
Loading
+26 −5
Original line number Diff line number Diff line
@@ -158,11 +158,34 @@ public class OperationScheduler {
            time = Math.max(time, moratoriumTimeMillis);
        }
        time = Math.max(time, lastSuccessTimeMillis + options.minTriggerMillis);
        if (errorCount > 0) {
            time = Math.max(time, lastErrorTimeMillis + options.backoffFixedMillis +
                    options.backoffIncrementalMillis * errorCount);
        }
        return time;
    }

    /**
     * Return the last time the operation completed.  Does not modify any state.
     *
     * @return the wall clock time when {@link #onSuccess()} was last called.
     */
    public long getLastSuccessTimeMillis() {
        return mStorage.getLong(PREFIX + "lastSuccessTimeMillis", 0);
    }

    /**
     * Return the last time the operation was attempted.  Does not modify any state.
     *
     * @return the wall clock time when {@link #onSuccess()} or {@link
     * #onTransientError()} was last called.
     */
    public long getLastAttemptTimeMillis() {
        return Math.max(
                mStorage.getLong(PREFIX + "lastSuccessTimeMillis", 0),
                mStorage.getLong(PREFIX + "lastErrorTimeMillis", 0));
    }

    /**
     * Fetch a {@link SharedPreferences} property, but force it to be before
     * a certain time, updating the value if necessary.  This is to recover
@@ -273,9 +296,7 @@ public class OperationScheduler {
     * where there is reason to hope things might start working better.
     */
    public void resetTransientError() {
        mStorage.edit()
                .remove(PREFIX + "lastErrorTimeMillis")
                .remove(PREFIX + "errorCount").commit();
        mStorage.edit().remove(PREFIX + "errorCount").commit();
    }

    /**
+18 −0
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ public class OperationSchedulerTest extends AndroidTestCase {
        OperationScheduler scheduler = new OperationScheduler(storage);
        OperationScheduler.Options options = new OperationScheduler.Options();
        assertEquals(Long.MAX_VALUE, scheduler.getNextTimeMillis(options));
        assertEquals(0, scheduler.getLastSuccessTimeMillis());
        assertEquals(0, scheduler.getLastAttemptTimeMillis());

        long beforeTrigger = System.currentTimeMillis();
        scheduler.setTriggerTimeMillis(beforeTrigger + 1000000);
@@ -49,6 +51,9 @@ public class OperationSchedulerTest extends AndroidTestCase {
        long beforeError = System.currentTimeMillis();
        scheduler.onTransientError();
        long afterError = System.currentTimeMillis();
        assertEquals(0, scheduler.getLastSuccessTimeMillis());
        assertTrue(beforeError <= scheduler.getLastAttemptTimeMillis());
        assertTrue(afterError >= scheduler.getLastAttemptTimeMillis());
        assertEquals(beforeTrigger + 1500000, scheduler.getNextTimeMillis(options));
        options.backoffFixedMillis = 1000000;
        options.backoffIncrementalMillis = 500000;
@@ -59,9 +64,18 @@ public class OperationSchedulerTest extends AndroidTestCase {
        beforeError = System.currentTimeMillis();
        scheduler.onTransientError();
        afterError = System.currentTimeMillis();
        assertTrue(beforeError <= scheduler.getLastAttemptTimeMillis());
        assertTrue(afterError >= scheduler.getLastAttemptTimeMillis());
        assertTrue(beforeError + 2000000 <= scheduler.getNextTimeMillis(options));
        assertTrue(afterError + 2000000 >= scheduler.getNextTimeMillis(options));

        // Reset transient error: no backoff interval
        scheduler.resetTransientError();
        assertEquals(0, scheduler.getLastSuccessTimeMillis());
        assertEquals(beforeTrigger + 1500000, scheduler.getNextTimeMillis(options));
        assertTrue(beforeError <= scheduler.getLastAttemptTimeMillis());
        assertTrue(afterError >= scheduler.getLastAttemptTimeMillis());

        // Permanent error holds true even if transient errors are reset
        // However, we remember that the transient error was reset...
        scheduler.onPermanentError();
@@ -75,6 +89,10 @@ public class OperationSchedulerTest extends AndroidTestCase {
        long beforeSuccess = System.currentTimeMillis();
        scheduler.onSuccess();
        long afterSuccess = System.currentTimeMillis();
        assertTrue(beforeSuccess <= scheduler.getLastAttemptTimeMillis());
        assertTrue(afterSuccess >= scheduler.getLastAttemptTimeMillis());
        assertTrue(beforeSuccess <= scheduler.getLastSuccessTimeMillis());
        assertTrue(afterSuccess >= scheduler.getLastSuccessTimeMillis());
        assertEquals(Long.MAX_VALUE, scheduler.getNextTimeMillis(options));

        // The moratorium is not reset by success!