Search in sources :

Example 61 with Params

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

the class JobQueueTestBase method testPriority.

@Test
public void testPriority() throws Exception {
    int JOB_LIMIT = 20;
    JobQueue jobQueue = createNewJobQueue();
    //create and add JOB_LIMIT jobs with random priority
    for (int i = 0; i < JOB_LIMIT; i++) {
        jobQueue.insert(createNewJobHolder(new Params((int) (Math.random() * 10))));
    }
    //ensure we get jobs in correct priority order
    int minPriority = Integer.MAX_VALUE;
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setExcludeRunning(true);
    for (int i = 0; i < JOB_LIMIT; i++) {
        JobHolder holder = jobQueue.nextJobAndIncRunCount(constraint);
        assertThat(holder.getPriority() <= minPriority, is(true));
    }
    assertThat(jobQueue.nextJobAndIncRunCount(constraint), nullValue());
}
Also used : TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobHolder(com.birbit.android.jobqueue.JobHolder) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) TagConstraint(com.birbit.android.jobqueue.TagConstraint) Constraint(com.birbit.android.jobqueue.Constraint) TestConstraint(com.birbit.android.jobqueue.TestConstraint) Test(org.junit.Test)

Example 62 with Params

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

the class JobQueueTestBase method testDeadlineDoesNotAffectTags.

@Test
public void testDeadlineDoesNotAffectTags() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder jobHolder = createNewJobHolder(new Params(0).overrideDeadlineToRunInMs(10));
    jobQueue.insert(jobHolder);
    mockTimer.incrementMs(100);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setTags(new String[] { "a" });
    constraint.setTagConstraint(TagConstraint.ANY);
    assertThat(jobQueue.findJobs(constraint), is(Collections.EMPTY_SET));
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(nullValue()));
}
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 63 with Params

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

the class JobQueueTestBase method testDueDelayUntilWithPriority.

@Test
public void testDueDelayUntilWithPriority() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    mockTimer.setNow(2000);
    long now = mockTimer.nanoTime();
    JobHolder lowPriorityHolder = createNewJobHolderWithDelayUntil(new Params(5), now - 1000 * JobManager.NS_PER_MS);
    JobHolder highPriorityHolder = createNewJobHolderWithDelayUntil(new Params(10), now - 10000 * JobManager.NS_PER_MS);
    jobQueue.insert(lowPriorityHolder);
    jobQueue.insert(highPriorityHolder);
    long soonJobDelay = 2000;
    JobHolder highestPriorityDelayedJob = createNewJobHolderWithDelayUntil(new Params(12), now + soonJobDelay * JobManager.NS_PER_MS);
    jobQueue.insert(highestPriorityDelayedJob);
    Constraint constraint = new Constraint();
    constraint.setNowInNs(mockTimer.nanoTime());
    assertThat("when asked, if job's due has passed, highest priority jobs's delay until should be " + "returned", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(highPriorityHolder.getDelayUntilNs()));
    //make sure soon job is valid now
    mockTimer.incrementMs(soonJobDelay + 1);
    assertThat("when a job's time come, it should be returned", jobQueue.nextJobAndIncRunCount(constraint).getId(), equalTo(highestPriorityDelayedJob.getId()));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) JobQueue(com.birbit.android.jobqueue.JobQueue) TagConstraint(com.birbit.android.jobqueue.TagConstraint) Constraint(com.birbit.android.jobqueue.Constraint) TestConstraint(com.birbit.android.jobqueue.TestConstraint) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Example 64 with Params

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

the class JobQueueTestBase method testDelayUntilWithExcludeGroups.

@Test
public void testDelayUntilWithExcludeGroups() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    long now = mockTimer.nanoTime();
    JobHolder networkJobHolder = createNewJobHolderWithDelayUntil(new Params(0).requireNetwork().groupBy("group1"), now + 200000 * JobManager.NS_PER_MS);
    JobHolder noNetworkJobHolder = createNewJobHolderWithDelayUntil(new Params(0).groupBy("group2"), now + 500000 * JobManager.NS_PER_MS);
    jobQueue.insert(networkJobHolder);
    jobQueue.insert(noNetworkJobHolder);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    constraint.setExcludeRunning(true);
    assertThat("if there is no network, delay until should be provided for no network job", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(noNetworkJobHolder.getDelayUntilNs()));
    assertThat("if there is no network, delay until should be provided for no network job", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(noNetworkJobHolder.getDelayUntilNs()));
    constraint.setExcludeGroups(Arrays.asList("group2"));
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    assertThat("if there is no network, but the group is disabled, delay until should be null", jobQueue.getNextJobDelayUntilNs(constraint), nullValue());
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    constraint.setExcludeGroups(Arrays.asList("group1", "group2"));
    assertThat("if there is network, but both groups are disabled, delay until should be null", jobQueue.getNextJobDelayUntilNs(constraint), nullValue());
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    constraint.setExcludeGroups(Arrays.asList("group1"));
    assertThat("if there is network, but group1 is disabled, delay should come from group2", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(noNetworkJobHolder.getDelayUntilNs()));
    constraint.setExcludeGroups(Arrays.asList("group2"));
    assertThat("if there is network, but group2 is disabled, delay should come from group1", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(networkJobHolder.getDelayUntilNs()));
    JobHolder noNetworkJobHolder2 = createNewJobHolderWithDelayUntil(new Params(0), now + 100000 * JobManager.NS_PER_MS);
    constraint.setExcludeGroups(Arrays.asList("group1", "group2"));
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    jobQueue.insert(noNetworkJobHolder2);
    assertThat("if there is a 3rd job and other gorups are disabled. 3rd job's delay should be " + "returned", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(noNetworkJobHolder2.getDelayUntilNs()));
}
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 65 with Params

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

the class InjectorTest method testInjector.

@Test
public void testInjector() throws Throwable {
    Configuration.Builder builder = new Configuration.Builder(RuntimeEnvironment.application);
    final JobManagerTestBase.ObjectReference injectedJobReference = new JobManagerTestBase.ObjectReference();
    final AtomicInteger injectionCallCount = new AtomicInteger(0);
    DependencyInjector dependencyInjector = new DependencyInjector() {

        @Override
        public void inject(Job job) {
            injectedJobReference.setObject(job);
            injectionCallCount.incrementAndGet();
        }
    };
    builder.injector(dependencyInjector);
    builder.timer(mockTimer);
    JobManager jobManager = createJobManager(builder);
    jobManager.stop();
    jobManager.addJob(new DummyJob(new Params(4)));
    MatcherAssert.assertThat("injection should be called after adding a non-persistent job", injectionCallCount.get(), equalTo(1));
    jobManager.addJob(new DummyJob(new Params(1).persist()));
    MatcherAssert.assertThat("injection should be called after adding a persistent job", injectionCallCount.get(), equalTo(2));
    JobHolder holder = nextJob(jobManager);
    MatcherAssert.assertThat("injection should NOT be called for non persistent job", holder.getJob(), not(injectedJobReference.getObject()));
    MatcherAssert.assertThat("injection should be called once for non persistent job", injectionCallCount.get(), equalTo(2));
    holder = nextJob(jobManager);
    MatcherAssert.assertThat("injection should be called for persistent job", holder.getJob(), equalTo(injectedJobReference.getObject()));
    MatcherAssert.assertThat("injection should be called two times for persistent job", injectionCallCount.get(), equalTo(3));
}
Also used : DependencyInjector(com.birbit.android.jobqueue.di.DependencyInjector) Configuration(com.birbit.android.jobqueue.config.Configuration) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) JobHolder(com.birbit.android.jobqueue.JobHolder) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) 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