Search in sources :

Example 56 with JobHolder

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

the class SimpleInMemoryPriorityQueue method getNextJobDelayUntilNs.

@Override
public Long getNextJobDelayUntilNs(@NonNull Constraint constraint) {
    Long minDelay = null;
    for (JobHolder holder : jobs) {
        if (matches(holder, constraint, true)) {
            final boolean hasDelay = holder.hasDelay() && matches(holder, constraint, false);
            final boolean hasDeadline = holder.hasDeadline();
            final long delay;
            if (hasDeadline == hasDelay) {
                delay = Math.min(holder.getDeadlineNs(), holder.getDelayUntilNs());
            } else if (hasDeadline) {
                delay = holder.getDeadlineNs();
            } else {
                delay = holder.getDelayUntilNs();
            }
            if (minDelay == null || delay < minDelay) {
                minDelay = delay;
            }
        }
    }
    return minDelay;
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 57 with JobHolder

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

the class SimpleInMemoryPriorityQueue method countReadyJobs.

@Override
public int countReadyJobs(@NonNull Constraint constraint) {
    int count = 0;
    reusedList.clear();
    for (JobHolder holder : jobs) {
        String groupId = holder.getGroupId();
        if ((groupId == null || !reusedList.contains(groupId)) && matches(holder, constraint, false)) {
            count++;
            if (groupId != null) {
                reusedList.add(groupId);
            }
        }
    }
    reusedList.clear();
    return count;
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) Constraint(com.birbit.android.jobqueue.Constraint)

Example 58 with JobHolder

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

Example 59 with JobHolder

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

the class SessionIdTest method testSessionId.

@Test
public void testSessionId() throws Throwable {
    JobManager jobManager = createJobManager();
    //we know job manager uses this value :/
    Long sessionId = mockTimer.nanoTime();
    jobManager.stop();
    Job[] jobs = new Job[] { new DummyJob(new Params(0)), new DummyJob(new Params(0).persist()) };
    for (Job job : jobs) {
        jobManager.addJob(job);
    }
    for (int i = 0; i < jobs.length; i++) {
        JobHolder jobHolder = nextJob(jobManager);
        MatcherAssert.assertThat("session id should be correct for job " + i, jobHolder.getRunningSessionId(), equalTo(sessionId));
    }
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) Job(com.birbit.android.jobqueue.Job) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Test(org.junit.Test)

Example 60 with JobHolder

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

the class JobParamsTest method assertParamsUnderstood.

@Test
public void assertParamsUnderstood() {
    MockTimer mockTimer = new MockTimer();
    JobHolder j1 = JobQueueTestBase.createNewJobHolder(new Params(1).requireNetwork(), mockTimer);
    assertThat("require network param should be understood properly", j1.getRequiredNetworkType(), equalTo(NetworkUtil.METERED));
    JobHolder j2 = JobQueueTestBase.createNewJobHolder(new Params(1).groupBy("blah"), mockTimer);
    assertThat("group param should be understood properly", j2.getGroupId(), equalTo("blah"));
    assertThat("require network param should be understood properly", j2.getRequiredNetworkType(), equalTo(NetworkUtil.DISCONNECTED));
    JobHolder j3 = JobQueueTestBase.createNewJobHolder(new Params(1).persist(), mockTimer);
    assertThat("persist param should be understood properly", j3.persistent, equalTo(true));
    JobHolder j4 = JobQueueTestBase.createNewJobHolder(new Params(1).setPersistent(false).setRequiresNetwork(false).setGroupId(null).setSingleId(null), mockTimer);
    assertThat("persist param should be understood properly", j4.persistent, equalTo(false));
    assertThat("require network param should be understood properly", j4.getRequiredNetworkType(), equalTo(NetworkUtil.DISCONNECTED));
    assertThat("group param should be understood properly", j4.groupId, nullValue());
    assertThat("single param should be understood properly", j4.getSingleInstanceId(), nullValue());
    mockTimer.incrementMs(2);
    DummyJob j15 = new DummyJob(new Params(1).singleInstanceBy("bloop"));
    assertThat("single param should be understood properly", j15.getSingleInstanceId(), endsWith("bloop"));
    assertThat("group param should be automatically set if single instance", j15.getRunGroupId(), notNullValue());
    mockTimer.setNow(150);
    JobHolder j6 = JobQueueTestBase.createNewJobHolder(new Params(1), mockTimer);
    assertThat("no deadline", j6.getDeadlineNs(), is(Params.FOREVER));
    JobHolder j7 = JobQueueTestBase.createNewJobHolder(new Params(1).overrideDeadlineToCancelInMs(100), mockTimer);
    assertThat("100 ms deadline", j7.getDeadlineNs(), is(100000150L));
    assertThat("100 ms deadline", j7.shouldCancelOnDeadline(), is(true));
    JobHolder j13 = JobQueueTestBase.createNewJobHolder(new Params(1).overrideDeadlineToCancelInMs(200), mockTimer);
    assertThat("100 ms deadline", j13.getDeadlineNs(), is(200000150L));
    assertThat("100 ms deadline", j7.shouldCancelOnDeadline(), is(true));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) 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