11package com .path .android .jobqueue .test .jobmanager ;
22
3+ import com .path .android .jobqueue .AsyncAddCallback ;
4+ import com .path .android .jobqueue .BaseJob ;
35import 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 ;
49import com .path .android .jobqueue .Params ;
510import com .path .android .jobqueue .test .jobs .DummyJob ;
11+ import org .fest .reflect .core .*;
612import org .hamcrest .*;
713import org .junit .Test ;
814import org .junit .runner .RunWith ;
1420@ RunWith (RobolectricTestRunner .class )
1521public 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