use of com.path.android.jobqueue.JobHolder in project android-priority-jobqueue by path.
the class JobQueueTestBase method testSessionId.
@Test
public void testSessionId() throws Exception {
long sessionId = (long) (Math.random() * 100000);
JobQueue jobQueue = createNewJobQueueWithSessionId(sessionId);
JobHolder jobHolder = createNewJobHolder();
jobQueue.insert(jobHolder);
jobHolder = jobQueue.nextJobAndIncRunCount(true, null);
assertThat("session id should be attached to next job", jobHolder.getRunningSessionId(), equalTo(sessionId));
}
use of com.path.android.jobqueue.JobHolder 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.JobHolder 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.JobHolder 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.JobHolder in project android-priority-jobqueue by path.
the class DelayTest method testDelay.
public void testDelay(boolean persist) throws Exception {
JobManager jobManager = createJobManager();
jobManager.stop();
DummyJob delayedJob = new DummyJob(new Params(10).delayInMs(1000).setPersistent(persist));
DummyJob nonDelayedJob = new DummyJob(new Params(0).setPersistent(persist));
long jobId = jobManager.addJob(delayedJob);
long nonDelayedJobId = jobManager.addJob(nonDelayedJob);
Invoker<JobHolder> nextJobMethod = getNextJobMethod(jobManager);
Invoker<Void> removeJobMethod = getRemoveJobMethod(jobManager);
JobHolder receivedJob = nextJobMethod.invoke();
MatcherAssert.assertThat("non-delayed job should be served", receivedJob, notNullValue());
MatcherAssert.assertThat("non-delayed job should id should match", receivedJob.getId(), equalTo(nonDelayedJobId));
removeJobMethod.invoke(receivedJob);
MatcherAssert.assertThat("delayed job should not be served", nextJobMethod.invoke(), nullValue());
MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
Thread.sleep(500);
MatcherAssert.assertThat("delayed job should not be served", nextJobMethod.invoke(), nullValue());
MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
Thread.sleep(2000);
MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
receivedJob = nextJobMethod.invoke();
MatcherAssert.assertThat("now should be able to receive the delayed job.", receivedJob, notNullValue());
if (receivedJob != null) {
MatcherAssert.assertThat("received job should be the delayed job", receivedJob.getId(), equalTo(jobId));
}
}
Aggregations