use of com.path.android.jobqueue.JobHolder in project android-priority-jobqueue by path.
the class NonPersistentPriorityQueue method nextJobAndIncRunCount.
/**
* {@inheritDoc}
*/
@Override
public JobHolder nextJobAndIncRunCount(boolean hasNetwork, Collection<String> excludeGroups) {
JobHolder jobHolder = jobs.peek(hasNetwork, excludeGroups);
if (jobHolder != null) {
// check if job can run
if (jobHolder.getDelayUntilNs() > System.nanoTime()) {
jobHolder = null;
} else {
jobHolder.setRunningSessionId(sessionId);
jobHolder.setRunCount(jobHolder.getRunCount() + 1);
jobs.remove(jobHolder);
}
}
return jobHolder;
}
use of com.path.android.jobqueue.JobHolder in project android-priority-jobqueue by path.
the class JobQueueTestBase method testDueDelayUntilWithPriority.
@Test
public void testDueDelayUntilWithPriority() throws Exception {
JobQueue jobQueue = createNewJobQueue();
long now = System.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);
long highestPriorityDelayedJobId = jobQueue.insert(highestPriorityDelayedJob);
assertThat("when asked, if job's due has passed, highest priority jobs's delay until should be " + "returned", jobQueue.getNextJobDelayUntilNs(true), equalTo(highPriorityHolder.getDelayUntilNs()));
// make sure soon job is valid now
Thread.sleep(soonJobDelay);
assertThat("when a job's time come, it should be returned", jobQueue.nextJobAndIncRunCount(true, null).getId(), equalTo(highestPriorityDelayedJobId));
}
use of com.path.android.jobqueue.JobHolder in project android-priority-jobqueue by path.
the class JobQueueTestBase method testPriorityWithReAdd.
@Test
public void testPriorityWithReAdd() 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;
for (int i = 0; i < JOB_LIMIT; i++) {
JobHolder holder = jobQueue.nextJobAndIncRunCount(true, null);
assertThat(holder.getPriority() <= minPriority, is(true));
jobQueue.insertOrReplace(holder);
}
assertThat(jobQueue.nextJobAndIncRunCount(true, null), notNullValue());
}
use of com.path.android.jobqueue.JobHolder in project android-priority-jobqueue by path.
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;
for (int i = 0; i < JOB_LIMIT; i++) {
JobHolder holder = jobQueue.nextJobAndIncRunCount(true, null);
assertThat(holder.getPriority() <= minPriority, is(true));
}
assertThat(jobQueue.nextJobAndIncRunCount(true, null), nullValue());
}
use of com.path.android.jobqueue.JobHolder in project android-priority-jobqueue by path.
the class NetworkNextJobTest method testNetworkNextJob.
@Test
public void testNetworkNextJob() throws Exception {
DummyNetworkUtil dummyNetworkUtil = new DummyNetworkUtil();
JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).networkUtil(dummyNetworkUtil));
jobManager.stop();
DummyJob dummyJob = new DummyJob(new Params(0).requireNetwork());
long dummyJobId = jobManager.addJob(dummyJob);
dummyNetworkUtil.setHasNetwork(false);
Invoker<JobHolder> nextJobMethod = getNextJobMethod(jobManager);
MatcherAssert.assertThat("when there isn't any network, next job should return null", nextJobMethod.invoke(), nullValue());
MatcherAssert.assertThat("even if there is network, job manager should return correct count", jobManager.count(), equalTo(1));
dummyNetworkUtil.setHasNetwork(true);
JobHolder retrieved = nextJobMethod.invoke();
MatcherAssert.assertThat("when network is recovered, next job should be retrieved", retrieved, notNullValue());
}
Aggregations