Search in sources :

Example 1 with AsyncAddCallback

use of com.birbit.android.jobqueue.AsyncAddCallback in project android-priority-jobqueue by yigit.

the class AddInBackgroundTest method addInBackground.

@Test
public void addInBackground() throws Throwable {
    long currentThreadId = Thread.currentThread().getId();
    final AtomicLong onAddedThreadId = new AtomicLong();
    final CountDownLatch addedLatch = new CountDownLatch(2);
    final Job dummyJob = new DummyJob(new Params(1).setDelayMs(delayed ? 1000 : 0)) {

        @Override
        public void onAdded() {
            super.onAdded();
            onAddedThreadId.set(Thread.currentThread().getId());
            addedLatch.countDown();
        }
    };
    JobManager jobManager = createJobManager();
    jobManager.stop();
    if (useCallback) {
        jobManager.addJobInBackground(dummyJob, new AsyncAddCallback() {

            @Override
            public void onAdded() {
                addedLatch.countDown();
            }
        });
    } else {
        addedLatch.countDown();
        jobManager.addJobInBackground(dummyJob);
    }
    addedLatch.await();
    MatcherAssert.assertThat("thread ids should be different. delayed:" + delayed, currentThreadId, CoreMatchers.not(onAddedThreadId.get()));
    if (useCallback) {
        JobHolder holder = new JobManagerThreadRunnable<JobHolder>(jobManager) {

            @Override
            public JobHolder onRun() {
                return findJobFromQueues(dummyJob.getId());
            }
        }.run();
        MatcherAssert.assertThat("there should be a job in the holder. id:" + dummyJob.getId() + ", delayed:" + delayed, holder, CoreMatchers.notNullValue());
        MatcherAssert.assertThat("id callback should have the proper id:", holder.getJob(), CoreMatchers.is(dummyJob));
    }
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) AtomicLong(java.util.concurrent.atomic.AtomicLong) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) AsyncAddCallback(com.birbit.android.jobqueue.AsyncAddCallback) CountDownLatch(java.util.concurrent.CountDownLatch) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test)

Aggregations

AsyncAddCallback (com.birbit.android.jobqueue.AsyncAddCallback)1 Job (com.birbit.android.jobqueue.Job)1 JobHolder (com.birbit.android.jobqueue.JobHolder)1 JobManager (com.birbit.android.jobqueue.JobManager)1 Params (com.birbit.android.jobqueue.Params)1 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Test (org.junit.Test)1