use of com.path.android.jobqueue.BaseJob in project android-priority-jobqueue by path.
the class InjectorTest method testInjector.
@Test
public void testInjector() throws Exception {
Configuration.Builder builder = new Configuration.Builder(Robolectric.application);
final JobManagerTestBase.ObjectReference injectedJobReference = new JobManagerTestBase.ObjectReference();
final AtomicInteger injectionCallCount = new AtomicInteger(0);
DependencyInjector dependencyInjector = new DependencyInjector() {
@Override
public void inject(BaseJob job) {
injectedJobReference.setObject(job);
injectionCallCount.incrementAndGet();
}
};
builder.injector(dependencyInjector);
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 = getNextJobMethod(jobManager).invoke();
MatcherAssert.assertThat("injection should NOT be called for non persistent job", holder.getBaseJob(), not(injectedJobReference.getObject()));
MatcherAssert.assertThat("injection should be called once for non persistent job", injectionCallCount.get(), equalTo(2));
holder = getNextJobMethod(jobManager).invoke();
MatcherAssert.assertThat("injection should be called for persistent job", holder.getBaseJob(), equalTo(injectedJobReference.getObject()));
MatcherAssert.assertThat("injection should be called two times for persistent job", injectionCallCount.get(), equalTo(3));
}
use of com.path.android.jobqueue.BaseJob in project android-priority-jobqueue by path.
the class RunFailingJobTest method runFailingJob.
@Test
public void runFailingJob() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
JobManager jobManager = createJobManager();
jobManager.addJob(0, new BaseJob(true) {
@Override
public void onAdded() {
}
@Override
public void onRun() throws Throwable {
throw new RuntimeException();
}
@Override
protected void onCancel() {
latch.countDown();
}
@Override
protected boolean shouldReRunOnThrowable(Throwable throwable) {
return false;
}
});
latch.await(10, TimeUnit.SECONDS);
MatcherAssert.assertThat((int) latch.getCount(), equalTo(0));
}
use of com.path.android.jobqueue.BaseJob 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.BaseJob in project android-priority-jobqueue by path.
the class InjectorTest method testInjectorCrash.
@Test
public void testInjectorCrash() throws Exception {
final String EXCEPTION_MESSAGE = "could not inject for whatever reason :)";
DependencyInjector dummyDependencyInjector = new DependencyInjector() {
@Override
public void inject(BaseJob baseJob) {
throw new RuntimeException(EXCEPTION_MESSAGE);
}
};
final ObjectReference objectReference = new ObjectReference();
final CountDownLatch exceptionLatch = new CountDownLatch(1);
CustomLogger customLogger = new CustomLogger() {
@Override
public boolean isDebugEnabled() {
return false;
}
@Override
public void d(String s, Object... objects) {
}
@Override
public void e(Throwable throwable, String s, Object... objects) {
objectReference.setObject(throwable);
exceptionLatch.countDown();
}
@Override
public void e(String s, Object... objects) {
//
}
};
JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).injector(dummyDependencyInjector).customLogger(customLogger));
Throwable addException = null;
try {
jobManager.addJob(new DummyJob(new Params(0)));
} catch (Throwable t) {
addException = t;
}
MatcherAssert.assertThat("addJob should throw exception if dependency injector throws exception", addException, notNullValue());
jobManager.addJobInBackground(new DummyJob(new Params(0)));
exceptionLatch.await(2, TimeUnit.SECONDS);
MatcherAssert.assertThat("there should be a received exception", objectReference.getObject(), notNullValue());
MatcherAssert.assertThat("logged exception should be a runtime exception", objectReference.getObject(), instanceOf(RuntimeException.class));
MatcherAssert.assertThat("logged exception should have expected message", ((Throwable) objectReference.getObject()).getMessage(), is(EXCEPTION_MESSAGE));
}
use of com.path.android.jobqueue.BaseJob in project android-priority-jobqueue by path.
the class PriorityTest method testPriority.
public void testPriority(JobManager jobManager, boolean persist) throws Exception {
priorityRunLatch = new CountDownLatch(2);
DummyJobWithRunOrderAssert.globalRunCount = new AtomicInteger(0);
BaseJob job1 = new DummyJobWithRunOrderAssert(2, persist);
BaseJob job2 = new DummyJobWithRunOrderAssert(1, persist);
jobManager.stop();
jobManager.addJob(1, job1);
jobManager.addJob(2, job2);
jobManager.start();
priorityRunLatch.await(4, TimeUnit.SECONDS);
// ensure both jobs did run
MatcherAssert.assertThat((int) priorityRunLatch.getCount(), equalTo(0));
}
Aggregations