From d41fe75f09fa09fc708d389572639511d745c20e Mon Sep 17 00:00:00 2001 From: Alon Albert Date: Wed, 22 Dec 2010 11:07:54 -0800 Subject: Improved ignore-backoff handling Allow a non-epidited ignore-backoff op to pass through an expidited backed off op. To do this, I first refactored the complicated if statement: if (best == null || ((bestSyncableIsUnknownAndNotARetry == syncableIsUnknownAndNotARetry) ? (best.expedited == op.expedited ? opRunTime < bestRunTime : op.expedited) : syncableIsUnknownAndNotARetry)) { best = op; bestSyncableIsUnknownAndNotARetry = syncableIsUnknownAndNotARetry; bestRunTime = opRunTime; } Into a more readable: boolean setBest = false; if (best == null) { setBest = true; } else if (bestSyncableIsUnknownAndNotARetry == syncableIsUnknownAndNotARetry) { if (best.expedited == op.expedited) { if (opRunTime < bestRunTime) { // if both have same level, earlier time wins setBest = true; } } else { if (op.expedited) { setBest = true; } } } else { if (syncableIsUnknownAndNotARetry) { setBest = true; } } if (setBest) { best = op; bestSyncableIsUnknownAndNotARetry = syncableIsUnknownAndNotARetry; bestRunTime = opRunTime; } The refactoring was all done automatically with IntelliJ to avoid human error in the conversion. After verifying this code still behaved as expected including the error condition in the bug, I added handling for the cases when a non-expidited op may override an expedited op if certain conditions occur, specificaly, if the expidited op is backed off and the non-expidited op is not. Finally, refactored to make it testable and added tests and logging. Bug: 3128963 Change-Id: I131cbcec6073ea5fe425f6b5aa88ca56c02b6598 --- .../src/android/content/SyncQueueTest.java | 41 +++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'core/tests') diff --git a/core/tests/coretests/src/android/content/SyncQueueTest.java b/core/tests/coretests/src/android/content/SyncQueueTest.java index 1da59d1..07133b6 100644 --- a/core/tests/coretests/src/android/content/SyncQueueTest.java +++ b/core/tests/coretests/src/android/content/SyncQueueTest.java @@ -24,6 +24,8 @@ import android.accounts.Account; import android.os.Bundle; import android.os.SystemClock; +import java.io.File; + public class SyncQueueTest extends AndroidTestCase { private static final Account ACCOUNT1 = new Account("test.account1", "test.type1"); private static final Account ACCOUNT2 = new Account("test.account2", "test.type2"); @@ -138,6 +140,38 @@ public class SyncQueueTest extends AndroidTestCase { mSyncQueue.remove(op3); } + public void testExpeditedVsBackoff() throws Exception { + + final SyncOperation op1 = new SyncOperation( + ACCOUNT1, SyncStorageEngine.SOURCE_USER, AUTHORITY1, newTestBundle("1"), 0); + final SyncOperation op2 = new SyncOperation( + ACCOUNT1, SyncStorageEngine.SOURCE_USER, AUTHORITY1, newTestBundle("2"), 0); + + // op1 is expedited but backed off + mSettings.setBackoff(ACCOUNT1, AUTHORITY1, SystemClock.elapsedRealtime() + 1000000, 5); + op1.expedited = true; + + // op2 is not expidaited but ignores back off so it should run + op2.extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, true); + + // First test top level method + mSyncQueue.remove(ACCOUNT1, AUTHORITY1); + mSyncQueue.add(op1); + mSyncQueue.add(op2); + assertEquals(op2, mSyncQueue.nextReadyToRun(SystemClock.elapsedRealtime()).first); + + // Since the queue is implemented as a hash, we cannot control the order the op's get + // fed into the algorithm so test the inner method in both cases + final long opTime1 = mSyncQueue.getOpTime(op1); + final boolean isInitial1 = mSyncQueue.getIsInitial(op1); + final long opTime2 = mSyncQueue.getOpTime(op2); + final boolean opIsInitial2 = mSyncQueue.getIsInitial(op2); + + assertTrue(mSyncQueue.isOpBetter(op1, opTime1, isInitial1, op2, opTime2, opIsInitial2)); + assertFalse(mSyncQueue.isOpBetter(op2, opTime2, opIsInitial2, op1, opTime1, isInitial1)); + } + + Bundle newTestBundle(String val) { Bundle bundle = new Bundle(); bundle.putString("test", val); @@ -148,7 +182,12 @@ public class SyncQueueTest extends AndroidTestCase { ContentResolver mResolver; public TestContext(ContentResolver resolver, Context realContext) { - super(new RenamingDelegatingContext(new MockContext(), realContext, "test.")); + super(new RenamingDelegatingContext(new MockContext() { + @Override + public File getFilesDir() { + return new File("/data"); + } + }, realContext, "test.")); mResolver = resolver; } -- cgit v1.1