use of com.path.android.jobqueue.Params in project android-priority-jobqueue by path.
the class JobQueueTestBase method testPriorityWithDelayedJobs.
@Test
public void testPriorityWithDelayedJobs() throws Exception {
JobQueue jobQueue = createNewJobQueue();
JobHolder delayedPriority_5 = createNewJobHolder(new Params(5));
org.fest.reflect.field.Invoker<Long> delayUntilField = getDelayUntilNsField(delayedPriority_5);
delayUntilField.set(System.nanoTime() - 1000);
JobHolder delayedPriority_2 = createNewJobHolder(new Params(2));
delayUntilField = getDelayUntilNsField(delayedPriority_2);
delayUntilField.set(System.nanoTime() - 500);
JobHolder nonDelayedPriority_6 = createNewJobHolder(new Params(6));
JobHolder nonDelayedPriority_3 = createNewJobHolder(new Params(3));
JobHolder nonDelayedPriority_2 = createNewJobHolder(new Params(2));
jobQueue.insert(delayedPriority_5);
jobQueue.insert(delayedPriority_2);
jobQueue.insert(nonDelayedPriority_6);
jobQueue.insert(nonDelayedPriority_2);
jobQueue.insert(nonDelayedPriority_3);
int lastPriority = Integer.MAX_VALUE;
for (int i = 0; i < 5; i++) {
JobHolder next = jobQueue.nextJobAndIncRunCount(true, null);
assertThat("next job should not be null", next, notNullValue());
assertThat("next job's priority should be lower then previous for job " + i, next.getPriority() <= lastPriority, is(true));
lastPriority = next.getPriority();
}
}
use of com.path.android.jobqueue.Params in project android-priority-jobqueue by path.
the class JobQueueTestBase method testDelayUntil.
@Test
public void testDelayUntil() throws Exception {
JobQueue jobQueue = createNewJobQueue();
long now = System.nanoTime();
JobHolder networkJobHolder = createNewJobHolderWithDelayUntil(new Params(0).requireNetwork(), now + 200000 * JobManager.NS_PER_MS);
JobHolder noNetworkJobHolder = createNewJobHolderWithDelayUntil(new Params(0), now + 500000 * JobManager.NS_PER_MS);
jobQueue.insert(networkJobHolder);
jobQueue.insert(noNetworkJobHolder);
assertThat("if there is no network, delay until should be provided for no network job", jobQueue.getNextJobDelayUntilNs(false), equalTo(noNetworkJobHolder.getDelayUntilNs()));
assertThat("if there is network, delay until should be provided for network job because it is " + "sooner", jobQueue.getNextJobDelayUntilNs(true), equalTo(networkJobHolder.getDelayUntilNs()));
JobHolder noNetworkJobHolder2 = createNewJobHolderWithDelayUntil(new Params(0), now + 100000 * JobManager.NS_PER_MS);
jobQueue.insert(noNetworkJobHolder2);
assertThat("if there is network, any job's delay until should be returned", jobQueue.getNextJobDelayUntilNs(true), equalTo(noNetworkJobHolder2.getDelayUntilNs()));
}
use of com.path.android.jobqueue.Params in project android-priority-jobqueue by path.
the class JobQueueTestBase method testJobFields.
@Test
public void testJobFields() throws Exception {
long sessionId = (long) (Math.random() * 1000);
JobQueue jobQueue = createNewJobQueueWithSessionId(sessionId);
JobHolder jobHolder = createNewJobHolder();
int priority = (int) (Math.random() * 1000);
jobHolder.setPriority(priority);
DummyJob dummyJob = new DummyJob(new Params(0));
jobHolder.setBaseJob(dummyJob);
int runCount = (int) (Math.random() * 10);
jobHolder.setRunCount(runCount);
long id = jobQueue.insert(jobHolder);
for (int i = 0; i < 2; i++) {
JobHolder received = jobQueue.nextJobAndIncRunCount(true, null);
assertThat("job id should be preserved", received.getId(), equalTo(id));
assertThat("job priority should be preserved", received.getPriority(), equalTo(priority));
assertThat("job session id should be assigned", received.getRunningSessionId(), equalTo(sessionId));
assertThat("job run count should be incremented", received.getRunCount(), equalTo(runCount + i + 1));
jobQueue.insertOrReplace(received);
}
}
use of com.path.android.jobqueue.Params in project android-priority-jobqueue by path.
the class AddInBackgroundTest method addInBackground.
public void addInBackground(boolean delayed, boolean useCallback) throws InterruptedException {
long currentThreadId = Thread.currentThread().getId();
final AtomicLong onAddedThreadId = new AtomicLong();
final CountDownLatch addedLatch = new CountDownLatch(2);
Job dummyJob = new DummyJob(new Params(1).setDelayMs(delayed ? 1000 : 0)) {
@Override
public void onAdded() {
super.onAdded();
onAddedThreadId.set(Thread.currentThread().getId());
addedLatch.countDown();
}
};
JobManager jobManager = createJobManager();
jobManager.stop();
final AtomicLong jobId = new AtomicLong(0);
if (useCallback) {
jobManager.addJobInBackground(dummyJob, new AsyncAddCallback() {
@Override
public void onAdded(long id) {
jobId.set(id);
addedLatch.countDown();
}
});
} else {
addedLatch.countDown();
jobManager.addJobInBackground(dummyJob);
}
addedLatch.await();
MatcherAssert.assertThat("thread ids should be different. delayed:" + delayed, currentThreadId, CoreMatchers.not(onAddedThreadId.get()));
if (useCallback) {
JobQueue queue = getNonPersistentQueue(jobManager);
JobHolder holder = queue.findJobById(jobId.longValue());
MatcherAssert.assertThat("there should be a job in the holder. id:" + jobId.longValue() + ", delayed:" + delayed + ", use cb:" + useCallback, holder, CoreMatchers.notNullValue());
MatcherAssert.assertThat("id callback should have the proper id:", holder.getBaseJob(), CoreMatchers.is((BaseJob) dummyJob));
}
}
use of com.path.android.jobqueue.Params in project android-priority-jobqueue by path.
the class CountTest method testCount.
@Test
public void testCount() throws Exception {
JobManager jobManager = createJobManager();
jobManager.stop();
for (int i = 0; i < 10; i++) {
jobManager.addJob(new DummyJob(new Params(0).persist()));
MatcherAssert.assertThat((int) jobManager.count(), equalTo(i * 2 + 1));
jobManager.addJob(new DummyJob(new Params(0).persist()));
MatcherAssert.assertThat((int) jobManager.count(), equalTo(i * 2 + 2));
}
jobManager.start();
Thread.sleep(2000);
MatcherAssert.assertThat((int) jobManager.count(), equalTo(0));
}
Aggregations