Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 1 | package com.android.server.job; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 2 | |
| 3 | |
| 4 | import android.content.ComponentName; |
| 5 | import android.content.Context; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 6 | import android.app.job.JobInfo; |
| 7 | import android.app.job.JobInfo.Builder; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 8 | import android.os.PersistableBundle; |
Matthew Williams | fa8e508 | 2015-10-15 15:59:12 -0700 | [diff] [blame] | 9 | import android.os.SystemClock; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 10 | import android.test.AndroidTestCase; |
| 11 | import android.test.RenamingDelegatingContext; |
| 12 | import android.util.Log; |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 13 | import android.util.ArraySet; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 14 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 15 | import com.android.server.job.controllers.JobStatus; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 16 | |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 17 | import java.util.Iterator; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 18 | |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 19 | /** |
| 20 | * Test reading and writing correctly from file. |
| 21 | */ |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 22 | public class JobStoreTest extends AndroidTestCase { |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 23 | private static final String TAG = "TaskStoreTest"; |
| 24 | private static final String TEST_PREFIX = "_test_"; |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 25 | |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 26 | private static final int SOME_UID = 34234; |
| 27 | private ComponentName mComponent; |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 28 | private static final long IO_WAIT = 1000L; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 29 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 30 | JobStore mTaskStoreUnderTest; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 31 | Context mTestContext; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 32 | |
| 33 | @Override |
| 34 | public void setUp() throws Exception { |
| 35 | mTestContext = new RenamingDelegatingContext(getContext(), TEST_PREFIX); |
| 36 | Log.d(TAG, "Saving tasks to '" + mTestContext.getFilesDir() + "'"); |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 37 | mTaskStoreUnderTest = |
| 38 | JobStore.initAndGetForTesting(mTestContext, mTestContext.getFilesDir()); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 39 | mComponent = new ComponentName(getContext().getPackageName(), StubClass.class.getName()); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void tearDown() throws Exception { |
| 44 | mTaskStoreUnderTest.clear(); |
| 45 | } |
| 46 | |
| 47 | public void testMaybeWriteStatusToDisk() throws Exception { |
| 48 | int taskId = 5; |
| 49 | long runByMillis = 20000L; // 20s |
| 50 | long runFromMillis = 2000L; // 2s |
| 51 | long initialBackoff = 10000L; // 10s |
| 52 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 53 | final JobInfo task = new Builder(taskId, mComponent) |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 54 | .setRequiresCharging(true) |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 55 | .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) |
| 56 | .setBackoffCriteria(initialBackoff, JobInfo.BACKOFF_POLICY_EXPONENTIAL) |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 57 | .setOverrideDeadline(runByMillis) |
| 58 | .setMinimumLatency(runFromMillis) |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 59 | .setPersisted(true) |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 60 | .build(); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 61 | final JobStatus ts = JobStatus.createFromJobInfo(task, SOME_UID, null, -1); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 62 | mTaskStoreUnderTest.add(ts); |
| 63 | Thread.sleep(IO_WAIT); |
| 64 | // Manually load tasks from xml file. |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 65 | final ArraySet<JobStatus> jobStatusSet = new ArraySet<JobStatus>(); |
| 66 | mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet); |
| 67 | |
| 68 | assertEquals("Didn't get expected number of persisted tasks.", 1, jobStatusSet.size()); |
| 69 | final JobStatus loadedTaskStatus = jobStatusSet.iterator().next(); |
| 70 | assertTasksEqual(task, loadedTaskStatus.getJob()); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 71 | assertTrue("JobStore#contains invalid.", mTaskStoreUnderTest.containsJob(ts)); |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 72 | assertEquals("Different uids.", SOME_UID, loadedTaskStatus.getUid()); |
| 73 | compareTimestampsSubjectToIoLatency("Early run-times not the same after read.", |
| 74 | ts.getEarliestRunTime(), loadedTaskStatus.getEarliestRunTime()); |
| 75 | compareTimestampsSubjectToIoLatency("Late run-times not the same after read.", |
| 76 | ts.getLatestRunTimeElapsed(), loadedTaskStatus.getLatestRunTimeElapsed()); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 77 | |
| 78 | } |
| 79 | |
| 80 | public void testWritingTwoFilesToDisk() throws Exception { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 81 | final JobInfo task1 = new Builder(8, mComponent) |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 82 | .setRequiresDeviceIdle(true) |
| 83 | .setPeriodic(10000L) |
| 84 | .setRequiresCharging(true) |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 85 | .setPersisted(true) |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 86 | .build(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 87 | final JobInfo task2 = new Builder(12, mComponent) |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 88 | .setMinimumLatency(5000L) |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 89 | .setBackoffCriteria(15000L, JobInfo.BACKOFF_POLICY_LINEAR) |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 90 | .setOverrideDeadline(30000L) |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 91 | .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) |
| 92 | .setPersisted(true) |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 93 | .build(); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 94 | final JobStatus taskStatus1 = JobStatus.createFromJobInfo(task1, SOME_UID, null, -1); |
| 95 | final JobStatus taskStatus2 = JobStatus.createFromJobInfo(task2, SOME_UID, null, -1); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 96 | mTaskStoreUnderTest.add(taskStatus1); |
| 97 | mTaskStoreUnderTest.add(taskStatus2); |
| 98 | Thread.sleep(IO_WAIT); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 99 | |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 100 | final ArraySet<JobStatus> jobStatusSet = new ArraySet<JobStatus>(); |
| 101 | mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet); |
| 102 | assertEquals("Incorrect # of persisted tasks.", 2, jobStatusSet.size()); |
| 103 | Iterator<JobStatus> it = jobStatusSet.iterator(); |
| 104 | JobStatus loaded1 = it.next(); |
| 105 | JobStatus loaded2 = it.next(); |
Matthew Williams | fa8e508 | 2015-10-15 15:59:12 -0700 | [diff] [blame] | 106 | |
| 107 | // Reverse them so we know which comparison to make. |
| 108 | if (loaded1.getJobId() != 8) { |
| 109 | JobStatus tmp = loaded1; |
| 110 | loaded1 = loaded2; |
| 111 | loaded2 = tmp; |
| 112 | } |
| 113 | |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 114 | assertTasksEqual(task1, loaded1.getJob()); |
| 115 | assertTasksEqual(task2, loaded2.getJob()); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 116 | assertTrue("JobStore#contains invalid.", mTaskStoreUnderTest.containsJob(taskStatus1)); |
| 117 | assertTrue("JobStore#contains invalid.", mTaskStoreUnderTest.containsJob(taskStatus2)); |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 118 | // Check that the loaded task has the correct runtimes. |
| 119 | compareTimestampsSubjectToIoLatency("Early run-times not the same after read.", |
| 120 | taskStatus1.getEarliestRunTime(), loaded1.getEarliestRunTime()); |
| 121 | compareTimestampsSubjectToIoLatency("Late run-times not the same after read.", |
| 122 | taskStatus1.getLatestRunTimeElapsed(), loaded1.getLatestRunTimeElapsed()); |
| 123 | compareTimestampsSubjectToIoLatency("Early run-times not the same after read.", |
| 124 | taskStatus2.getEarliestRunTime(), loaded2.getEarliestRunTime()); |
| 125 | compareTimestampsSubjectToIoLatency("Late run-times not the same after read.", |
| 126 | taskStatus2.getLatestRunTimeElapsed(), loaded2.getLatestRunTimeElapsed()); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 127 | |
| 128 | } |
| 129 | |
| 130 | public void testWritingTaskWithExtras() throws Exception { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 131 | JobInfo.Builder b = new Builder(8, mComponent) |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 132 | .setRequiresDeviceIdle(true) |
| 133 | .setPeriodic(10000L) |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 134 | .setRequiresCharging(true) |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 135 | .setPersisted(true); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 136 | |
| 137 | PersistableBundle extras = new PersistableBundle(); |
| 138 | extras.putDouble("hello", 3.2); |
| 139 | extras.putString("hi", "there"); |
| 140 | extras.putInt("into", 3); |
| 141 | b.setExtras(extras); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 142 | final JobInfo task = b.build(); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 143 | JobStatus taskStatus = JobStatus.createFromJobInfo(task, SOME_UID, null, -1); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 144 | |
| 145 | mTaskStoreUnderTest.add(taskStatus); |
| 146 | Thread.sleep(IO_WAIT); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 147 | |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 148 | final ArraySet<JobStatus> jobStatusSet = new ArraySet<JobStatus>(); |
| 149 | mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet); |
| 150 | assertEquals("Incorrect # of persisted tasks.", 1, jobStatusSet.size()); |
| 151 | JobStatus loaded = jobStatusSet.iterator().next(); |
| 152 | assertTasksEqual(task, loaded.getJob()); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 153 | } |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 154 | public void testWritingTaskWithSourcePackage() throws Exception { |
| 155 | JobInfo.Builder b = new Builder(8, mComponent) |
| 156 | .setRequiresDeviceIdle(true) |
| 157 | .setPeriodic(10000L) |
| 158 | .setRequiresCharging(true) |
| 159 | .setPersisted(true); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 160 | JobStatus taskStatus = JobStatus.createFromJobInfo(b.build(), SOME_UID, |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 161 | "com.google.android.gms", 0); |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 162 | |
| 163 | mTaskStoreUnderTest.add(taskStatus); |
| 164 | Thread.sleep(IO_WAIT); |
| 165 | |
| 166 | final ArraySet<JobStatus> jobStatusSet = new ArraySet<JobStatus>(); |
| 167 | mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet); |
| 168 | assertEquals("Incorrect # of persisted tasks.", 1, jobStatusSet.size()); |
| 169 | JobStatus loaded = jobStatusSet.iterator().next(); |
| 170 | assertEquals("Source package not equal.", loaded.getSourcePackageName(), |
| 171 | taskStatus.getSourcePackageName()); |
| 172 | assertEquals("Source user not equal.", loaded.getSourceUserId(), |
| 173 | taskStatus.getSourceUserId()); |
| 174 | } |
| 175 | |
| 176 | public void testWritingTaskWithFlex() throws Exception { |
| 177 | JobInfo.Builder b = new Builder(8, mComponent) |
| 178 | .setRequiresDeviceIdle(true) |
| 179 | .setPeriodic(5*60*60*1000, 1*60*60*1000) |
| 180 | .setRequiresCharging(true) |
| 181 | .setPersisted(true); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 182 | JobStatus taskStatus = JobStatus.createFromJobInfo(b.build(), SOME_UID, null, -1); |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 183 | |
| 184 | mTaskStoreUnderTest.add(taskStatus); |
| 185 | Thread.sleep(IO_WAIT); |
| 186 | |
| 187 | final ArraySet<JobStatus> jobStatusSet = new ArraySet<JobStatus>(); |
| 188 | mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet); |
| 189 | assertEquals("Incorrect # of persisted tasks.", 1, jobStatusSet.size()); |
| 190 | JobStatus loaded = jobStatusSet.iterator().next(); |
| 191 | assertEquals("Period not equal.", loaded.getJob().getIntervalMillis(), |
| 192 | taskStatus.getJob().getIntervalMillis()); |
| 193 | assertEquals("Flex not equal.", loaded.getJob().getFlexMillis(), |
| 194 | taskStatus.getJob().getFlexMillis()); |
| 195 | } |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 196 | |
Matthew Williams | fa8e508 | 2015-10-15 15:59:12 -0700 | [diff] [blame] | 197 | public void testMassivePeriodClampedOnRead() throws Exception { |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 198 | final long ONE_HOUR = 60*60*1000L; // flex |
| 199 | final long TWO_HOURS = 2 * ONE_HOUR; // period |
Matthew Williams | fa8e508 | 2015-10-15 15:59:12 -0700 | [diff] [blame] | 200 | JobInfo.Builder b = new Builder(8, mComponent) |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 201 | .setPeriodic(TWO_HOURS, ONE_HOUR) |
Matthew Williams | fa8e508 | 2015-10-15 15:59:12 -0700 | [diff] [blame] | 202 | .setPersisted(true); |
| 203 | final long invalidLateRuntimeElapsedMillis = |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 204 | SystemClock.elapsedRealtime() + (TWO_HOURS * ONE_HOUR) + TWO_HOURS; // > period+flex |
Matthew Williams | fa8e508 | 2015-10-15 15:59:12 -0700 | [diff] [blame] | 205 | final long invalidEarlyRuntimeElapsedMillis = |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 206 | invalidLateRuntimeElapsedMillis - TWO_HOURS; // Early is (late - period). |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 207 | final JobStatus js = new JobStatus(b.build(), SOME_UID, "somePackage", |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 208 | 0 /* sourceUserId */, |
Matthew Williams | fa8e508 | 2015-10-15 15:59:12 -0700 | [diff] [blame] | 209 | invalidEarlyRuntimeElapsedMillis, invalidLateRuntimeElapsedMillis); |
| 210 | |
| 211 | mTaskStoreUnderTest.add(js); |
| 212 | Thread.sleep(IO_WAIT); |
| 213 | |
| 214 | final ArraySet<JobStatus> jobStatusSet = new ArraySet<JobStatus>(); |
| 215 | mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet); |
| 216 | assertEquals("Incorrect # of persisted tasks.", 1, jobStatusSet.size()); |
| 217 | JobStatus loaded = jobStatusSet.iterator().next(); |
| 218 | |
| 219 | // Assert early runtime was clamped to be under now + period. We can do <= here b/c we'll |
| 220 | // call SystemClock.elapsedRealtime after doing the disk i/o. |
| 221 | final long newNowElapsed = SystemClock.elapsedRealtime(); |
| 222 | assertTrue("Early runtime wasn't correctly clamped.", |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 223 | loaded.getEarliestRunTime() <= newNowElapsed + TWO_HOURS); |
| 224 | // Assert late runtime was clamped to be now + period + flex. |
Matthew Williams | fa8e508 | 2015-10-15 15:59:12 -0700 | [diff] [blame] | 225 | assertTrue("Early runtime wasn't correctly clamped.", |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 226 | loaded.getEarliestRunTime() <= newNowElapsed + TWO_HOURS + ONE_HOUR); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | public void testPriorityPersisted() throws Exception { |
| 230 | JobInfo.Builder b = new Builder(92, mComponent) |
| 231 | .setOverrideDeadline(5000) |
| 232 | .setPriority(42) |
| 233 | .setPersisted(true); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 234 | final JobStatus js = JobStatus.createFromJobInfo(b.build(), SOME_UID, null, -1); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 235 | mTaskStoreUnderTest.add(js); |
| 236 | Thread.sleep(IO_WAIT); |
| 237 | final ArraySet<JobStatus> jobStatusSet = new ArraySet<JobStatus>(); |
| 238 | mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet); |
| 239 | JobStatus loaded = jobStatusSet.iterator().next(); |
| 240 | assertEquals("Priority not correctly persisted.", 42, loaded.getPriority()); |
Matthew Williams | fa8e508 | 2015-10-15 15:59:12 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 243 | /** |
Shreyas Basarge | 7ef490f | 2015-12-03 16:45:22 +0000 | [diff] [blame] | 244 | * Test that non persisted job is not written to disk. |
| 245 | */ |
| 246 | public void testNonPersistedTaskIsNotPersisted() throws Exception { |
| 247 | JobInfo.Builder b = new Builder(42, mComponent) |
| 248 | .setOverrideDeadline(10000) |
| 249 | .setPersisted(false); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 250 | JobStatus jsNonPersisted = JobStatus.createFromJobInfo(b.build(), SOME_UID, null, -1); |
Shreyas Basarge | 7ef490f | 2015-12-03 16:45:22 +0000 | [diff] [blame] | 251 | mTaskStoreUnderTest.add(jsNonPersisted); |
| 252 | b = new Builder(43, mComponent) |
| 253 | .setOverrideDeadline(10000) |
| 254 | .setPersisted(true); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 255 | JobStatus jsPersisted = JobStatus.createFromJobInfo(b.build(), SOME_UID, null, -1); |
Shreyas Basarge | 7ef490f | 2015-12-03 16:45:22 +0000 | [diff] [blame] | 256 | mTaskStoreUnderTest.add(jsPersisted); |
| 257 | Thread.sleep(IO_WAIT); |
| 258 | final ArraySet<JobStatus> jobStatusSet = new ArraySet<JobStatus>(); |
| 259 | mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet); |
| 260 | assertEquals("Job count is incorrect.", 1, jobStatusSet.size()); |
| 261 | JobStatus jobStatus = jobStatusSet.iterator().next(); |
| 262 | assertEquals("Wrong job persisted.", 43, jobStatus.getJobId()); |
| 263 | } |
| 264 | |
| 265 | /** |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 266 | * Helper function to throw an error if the provided task and TaskStatus objects are not equal. |
| 267 | */ |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 268 | private void assertTasksEqual(JobInfo first, JobInfo second) { |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 269 | assertEquals("Different task ids.", first.getId(), second.getId()); |
| 270 | assertEquals("Different components.", first.getService(), second.getService()); |
| 271 | assertEquals("Different periodic status.", first.isPeriodic(), second.isPeriodic()); |
| 272 | assertEquals("Different period.", first.getIntervalMillis(), second.getIntervalMillis()); |
| 273 | assertEquals("Different inital backoff.", first.getInitialBackoffMillis(), |
| 274 | second.getInitialBackoffMillis()); |
| 275 | assertEquals("Different backoff policy.", first.getBackoffPolicy(), |
| 276 | second.getBackoffPolicy()); |
| 277 | |
| 278 | assertEquals("Invalid charging constraint.", first.isRequireCharging(), |
| 279 | second.isRequireCharging()); |
| 280 | assertEquals("Invalid idle constraint.", first.isRequireDeviceIdle(), |
| 281 | second.isRequireDeviceIdle()); |
| 282 | assertEquals("Invalid unmetered constraint.", |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 283 | first.getNetworkType() == JobInfo.NETWORK_TYPE_UNMETERED, |
| 284 | second.getNetworkType() == JobInfo.NETWORK_TYPE_UNMETERED); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 285 | assertEquals("Invalid connectivity constraint.", |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 286 | first.getNetworkType() == JobInfo.NETWORK_TYPE_ANY, |
| 287 | second.getNetworkType() == JobInfo.NETWORK_TYPE_ANY); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 288 | assertEquals("Invalid deadline constraint.", |
| 289 | first.hasLateConstraint(), |
| 290 | second.hasLateConstraint()); |
| 291 | assertEquals("Invalid delay constraint.", |
| 292 | first.hasEarlyConstraint(), |
| 293 | second.hasEarlyConstraint()); |
| 294 | assertEquals("Extras don't match", |
| 295 | first.getExtras().toString(), second.getExtras().toString()); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * When comparing timestamps before and after DB read/writes (to make sure we're saving/loading |
| 300 | * the correct values), there is some latency involved that terrorises a naive assertEquals(). |
| 301 | * We define a <code>DELTA_MILLIS</code> as a function variable here to make this comparision |
| 302 | * more reasonable. |
| 303 | */ |
| 304 | private void compareTimestampsSubjectToIoLatency(String error, long ts1, long ts2) { |
| 305 | final long DELTA_MILLIS = 700L; // We allow up to 700ms of latency for IO read/writes. |
| 306 | assertTrue(error, Math.abs(ts1 - ts2) < DELTA_MILLIS + IO_WAIT); |
| 307 | } |
| 308 | |
| 309 | private static class StubClass {} |
| 310 | |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 311 | } |