Search in sources :

Example 61 with JobHolder

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

the class JobQueueTestBase method testDelayUntilWithUnmeteredNetworkRequirement2.

@Test
public void testDelayUntilWithUnmeteredNetworkRequirement2() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(3000).requireUnmeteredNetwork().delayInMs(2000));
    jobQueue.insert(holder1);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.UNMETERED);
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(2000000000L));
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(3000000000L));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Example 62 with JobHolder

use of com.birbit.android.jobqueue.JobHolder 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

JobHolder (com.birbit.android.jobqueue.JobHolder)62 Test (org.junit.Test)50 Params (com.birbit.android.jobqueue.Params)46 JobQueue (com.birbit.android.jobqueue.JobQueue)42 TestConstraint (com.birbit.android.jobqueue.TestConstraint)39 Constraint (com.birbit.android.jobqueue.Constraint)8 TagConstraint (com.birbit.android.jobqueue.TagConstraint)7 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)7 JobManager (com.birbit.android.jobqueue.JobManager)6 Job (com.birbit.android.jobqueue.Job)4 Cursor (android.database.Cursor)2 Consumer (com.birbit.android.jobqueue.ConsumerManager.Consumer)2 Configuration (com.birbit.android.jobqueue.config.Configuration)2 MessageFactory (com.birbit.android.jobqueue.messaging.MessageFactory)2 MessageQueueConsumer (com.birbit.android.jobqueue.messaging.MessageQueueConsumer)2 PriorityMessageQueue (com.birbit.android.jobqueue.messaging.PriorityMessageQueue)2 SafeMessageQueue (com.birbit.android.jobqueue.messaging.SafeMessageQueue)2 RunJobMessage (com.birbit.android.jobqueue.messaging.message.RunJobMessage)2 MockTimer (com.birbit.android.jobqueue.test.timer.MockTimer)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2