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;
}
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;
}
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));
}
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));
}
}
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));
}
Aggregations