Skip to content

Commit 52a9bab

Browse files
committed
query interface test for delayed jobs. #18
1 parent eb09552 commit 52a9bab

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

jobqueue/src/com/path/android/jobqueue/JobManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ private void reAddJob(JobHolder jobHolder) {
308308
* <p>
309309
* You should not call this method on UI thread because it may make a db request.
310310
* </p>
311+
* <p>
312+
* This is not a very fast call so try not to make it unless necessary. Consider using events if you need to be
313+
* informed about a job's lifecycle.
314+
* </p>
311315
* @param id the ID, returned by the addJob method
312316
* @param isPersistent Jobs are added to different queues depending on if they are persistent or not. This is necessary
313317
* because each queue has independent id sets.
Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package com.path.android.jobqueue.test.jobmanager;
22

3+
import com.path.android.jobqueue.AsyncAddCallback;
4+
import com.path.android.jobqueue.BaseJob;
35
import com.path.android.jobqueue.Job;
6+
import com.path.android.jobqueue.JobHolder;
7+
import com.path.android.jobqueue.JobManager;
8+
import com.path.android.jobqueue.JobQueue;
49
import com.path.android.jobqueue.Params;
510
import com.path.android.jobqueue.test.jobs.DummyJob;
11+
import org.fest.reflect.core.*;
612
import org.hamcrest.*;
713
import org.junit.Test;
814
import org.junit.runner.RunWith;
@@ -14,15 +20,19 @@
1420
@RunWith(RobolectricTestRunner.class)
1521
public class AddInBackgroundTest extends JobManagerTestBase {
1622
@Test
17-
public void testAddInBackground() {
18-
addInBackground(false);
19-
addInBackground(true);
20-
23+
public void testAddInBackground() throws InterruptedException {
24+
for(boolean delay : new boolean[]{true, false}) {
25+
for(boolean useCallback : new boolean[]{true, false}) {
26+
addInBackground(delay, useCallback);
27+
}
28+
}
2129
}
22-
public void addInBackground(boolean delayed) {
30+
31+
public void addInBackground(boolean delayed, boolean useCallback) throws InterruptedException {
2332
long currentThreadId = Thread.currentThread().getId();
2433
final AtomicLong onAddedThreadId = new AtomicLong();
2534
final CountDownLatch addedLatch = new CountDownLatch(2);
35+
2636
Job dummyJob = new DummyJob(new Params(1).setDelayMs(delayed ? 1000 : 0)) {
2737
@Override
2838
public void onAdded() {
@@ -31,9 +41,33 @@ public void onAdded() {
3141
addedLatch.countDown();
3242
}
3343
};
34-
createJobManager().addJobInBackground(dummyJob);
35-
36-
addedLatch.countDown();
44+
JobManager jobManager = createJobManager();
45+
jobManager.stop();
46+
final AtomicLong jobId = new AtomicLong(0);
47+
if(useCallback) {
48+
jobManager.addJobInBackground(dummyJob, new AsyncAddCallback() {
49+
@Override
50+
public void onAdded(long id) {
51+
jobId.set(id);
52+
addedLatch.countDown();
53+
}
54+
});
55+
} else {
56+
addedLatch.countDown();
57+
jobManager.addJobInBackground(dummyJob);
58+
}
59+
addedLatch.await();
3760
MatcherAssert.assertThat("thread ids should be different. delayed:" + delayed, currentThreadId, CoreMatchers.not(onAddedThreadId.get()));
61+
if(useCallback) {
62+
JobQueue queue = getNonPersistentQueue(jobManager);
63+
JobHolder holder = queue.findJobById(jobId.longValue());
64+
MatcherAssert.assertThat("there should be a job in the holder. id:" + jobId.longValue() +", delayed:" + delayed + ", use cb:" + useCallback
65+
, holder, CoreMatchers.notNullValue());
66+
MatcherAssert.assertThat("id callback should have the proper id:", holder.getBaseJob(), CoreMatchers.is((BaseJob) dummyJob));
67+
}
68+
}
69+
70+
protected JobQueue getNonPersistentQueue(JobManager jobManager) {
71+
return Reflection.field("nonPersistentJobQueue").ofType(JobQueue.class).in(jobManager).get();
3872
}
3973
}

0 commit comments

Comments
 (0)