Search in sources :

Example 31 with Params

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

the class SqliteJobQueueTest method testCustomSerializer.

@Test
public void testCustomSerializer() throws Exception {
    final CountDownLatch calledForSerialize = new CountDownLatch(1);
    final CountDownLatch calledForDeserialize = new CountDownLatch(1);
    SqliteJobQueue.JobSerializer jobSerializer = new SqliteJobQueue.JavaSerializer() {

        @Override
        public byte[] serialize(Object object) throws IOException {
            calledForSerialize.countDown();
            return super.serialize(object);
        }

        @Override
        public <T extends Job> T deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
            calledForDeserialize.countDown();
            return super.deserialize(bytes);
        }
    };
    SqliteJobQueue jobQueue = new SqliteJobQueue(new Configuration.Builder(RuntimeEnvironment.application).id("__" + mockTimer.nanoTime()).jobSerializer(jobSerializer).inTestMode().timer(mockTimer).build(), mockTimer.nanoTime(), jobSerializer);
    jobQueue.insert(createNewJobHolder(new Params(0)));
    calledForSerialize.await(1, TimeUnit.SECONDS);
    MatcherAssert.assertThat("custom serializer should be called for serialize", (int) calledForSerialize.getCount(), CoreMatchers.equalTo(0));
    MatcherAssert.assertThat("custom serializer should NOT be called for deserialize", (int) calledForDeserialize.getCount(), CoreMatchers.equalTo(1));
    jobQueue.nextJobAndIncRunCount(new TestConstraint(mockTimer));
    MatcherAssert.assertThat("custom serializer should be called for deserialize", (int) calledForDeserialize.getCount(), CoreMatchers.equalTo(0));
}
Also used : TestConstraint(com.birbit.android.jobqueue.TestConstraint) SqliteJobQueue(com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue) Params(com.birbit.android.jobqueue.Params) CountDownLatch(java.util.concurrent.CountDownLatch) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test)

Example 32 with Params

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

the class JobQueueTestBase method testDelayUntilWithNetworkRequirementAndRegularDelayedJob.

@Test
public void testDelayUntilWithNetworkRequirementAndRegularDelayedJob() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(1000).requireNetwork());
    JobHolder holder2 = createNewJobHolder(new Params(2).delayInMs(500));
    jobQueue.insert(holder1);
    jobQueue.insert(holder2);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(500000000L));
}
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 33 with Params

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

the class JobQueueTestBase method testDelayUntilWithNetworkRequirement4.

@Test
public void testDelayUntilWithNetworkRequirement4() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(3000).delayInMs(2000).requireNetwork());
    jobQueue.insert(holder1);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.UNMETERED);
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(2000000000L));
}
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 34 with Params

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

the class JobQueueTestBase method testFindByTags.

@Test
public void testFindByTags() {
    JobQueue jobQueue = createNewJobQueue();
    assertThat("empty queue should return 0", jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), "abc")).size(), is(0));
    jobQueue.insert(createNewJobHolder());
    Set<JobHolder> result = jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), "blah"));
    assertThat("if job does not have a tag, it should return 0", result.size(), is(0));
    final String tag1 = UUID.randomUUID().toString();
    JobHolder holder = createNewJobHolder(new Params(0).addTags(tag1));
    jobQueue.insert(holder);
    assertTags("holder with 1 tag", jobQueue, holder);
    jobQueue.insertOrReplace(holder);
    assertTags("holder with 1 tag reinserted", jobQueue, holder);
    jobQueue.remove(holder);
    assertThat("when job is removed, it should return none", jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), tag1)).size(), is(0));
    JobHolder holder2 = createNewJobHolder(new Params(0).addTags(tag1));
    jobQueue.insert(holder2);
    assertThat("it should return the job", jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), tag1)).size(), is(1));
    jobQueue.onJobCancelled(holder2);
    assertThat("when queried w/ exclude cancelled, it should not return the job", jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), tag1)).size(), is(0));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Example 35 with Params

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

the class JobQueueTestBase method testDelayUntilWithNetworkRequirementAndRegularDelayedJob4.

@Test
public void testDelayUntilWithNetworkRequirementAndRegularDelayedJob4() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(500).requireNetwork());
    JobHolder holder2 = createNewJobHolder(new Params(2).delayInMs(1000));
    jobQueue.insert(holder2);
    jobQueue.insert(holder1);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(500000000L));
}
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)

Aggregations

Params (com.birbit.android.jobqueue.Params)95 Test (org.junit.Test)75 JobManager (com.birbit.android.jobqueue.JobManager)47 JobHolder (com.birbit.android.jobqueue.JobHolder)46 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)41 JobQueue (com.birbit.android.jobqueue.JobQueue)40 TestConstraint (com.birbit.android.jobqueue.TestConstraint)37 Job (com.birbit.android.jobqueue.Job)31 CountDownLatch (java.util.concurrent.CountDownLatch)31 Configuration (com.birbit.android.jobqueue.config.Configuration)17 RetryConstraint (com.birbit.android.jobqueue.RetryConstraint)12 TargetApi (android.annotation.TargetApi)9 CancelResult (com.birbit.android.jobqueue.CancelResult)9 Constraint (com.birbit.android.jobqueue.Constraint)8 TagConstraint (com.birbit.android.jobqueue.TagConstraint)8 JobManagerCallbackAdapter (com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ArrayList (java.util.ArrayList)6 JobManagerCallback (com.birbit.android.jobqueue.callback.JobManagerCallback)4 SqliteJobQueue (com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue)3