use of com.path.android.jobqueue.JobManager 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));
}
}
use of com.path.android.jobqueue.JobManager in project dev-summit-architecture-demo by yigit.
the class TestUtil method prepare.
public static TestComponent prepare(App app) {
FlowManager.destroy();
resetSingleton(FlowManager.class, "mDatabaseHolder");
ApplicationModule appModule = new ApplicationModule(app) {
@Override
public EventBus eventBus() {
return new LoggingBus();
}
@Provides
@Singleton
public JobManager jobManager() {
JobManager mock = mock(JobManager.class);
when(mock.addJob(any(Job.class))).thenReturn(1L);
return mock;
}
};
TestComponent testComponent = DaggerTestComponent.builder().testApplicationModule(new TestApplicationModule()).applicationModule(appModule).build();
testComponent.appContext().deleteDatabase(DemoDatabase.NAME + ".db");
FlowManager.init(app);
testComponent.feedModel().clear();
testComponent.loggingBus().clear();
return testComponent;
}
use of com.path.android.jobqueue.JobManager in project android-priority-jobqueue by path.
the class DelayedRunTest method testDelayedRun.
public void testDelayedRun(boolean persist, boolean tryToStop) throws Exception {
JobManager jobManager = createJobManager();
DummyJob delayedJob = new DummyJob(new Params(10).delayInMs(2000).setPersistent(persist));
DummyJob nonDelayedJob = new DummyJob(new Params(0).setPersistent(persist));
jobManager.addJob(delayedJob);
jobManager.addJob(nonDelayedJob);
Thread.sleep(500);
MatcherAssert.assertThat("there should be 1 delayed job waiting to be run", jobManager.count(), equalTo(1));
if (tryToStop) {
// see issue #11
jobManager.stop();
Thread.sleep(3000);
MatcherAssert.assertThat("there should still be 1 delayed job waiting to be run when job manager is stopped", jobManager.count(), equalTo(1));
jobManager.start();
}
Thread.sleep(3000);
MatcherAssert.assertThat("all jobs should be completed", jobManager.count(), equalTo(0));
}
use of com.path.android.jobqueue.JobManager in project android-priority-jobqueue by path.
the class GroupingTest method testGroupingRaceCondition.
@Test
public void testGroupingRaceCondition() throws Exception {
DummyNetworkUtilWithConnectivityEventSupport dummyNetworkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).minConsumerCount(5).maxConsumerCount(10).networkUtil(dummyNetworkUtil));
dummyNetworkUtil.setHasNetwork(false, true);
// add a bunch of network requring jobs
final String GROUP_ID = "shared_group_id";
final int AFTER_ADDED_JOBS_COUNT = 5;
final int NOT_SET_JOB_ID = -1;
final AtomicInteger firstRunJob = new AtomicInteger(NOT_SET_JOB_ID);
final int FIRST_JOB_ID = -10;
final CountDownLatch onAddedCalled = new CountDownLatch(1);
final CountDownLatch remainingJobsOnAddedCalled = new CountDownLatch(AFTER_ADDED_JOBS_COUNT);
jobManager.addJobInBackground(new DummyJob(new Params(10).requireNetwork().groupBy(GROUP_ID)) {
@Override
public void onAdded() {
super.onAdded();
onAddedCalled.countDown();
try {
// wait until all other jobs are added
remainingJobsOnAddedCalled.await();
// wait a bit after all are added,
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
@Override
public void onRun() throws Throwable {
super.onRun();
firstRunJob.compareAndSet(NOT_SET_JOB_ID, FIRST_JOB_ID);
}
});
// ensure first jobs on added is called
onAddedCalled.await();
for (int i = 0; i < AFTER_ADDED_JOBS_COUNT; i++) {
final int finalI = i;
jobManager.addJob(new DummyJob(new Params(5).groupBy(GROUP_ID).requireNetwork()) {
final int id = finalI + 1;
@Override
public void onAdded() {
super.onAdded();
remainingJobsOnAddedCalled.countDown();
}
@Override
public void onRun() throws Throwable {
super.onRun();
firstRunJob.compareAndSet(NOT_SET_JOB_ID, id);
}
});
}
dummyNetworkUtil.setHasNetwork(true, true);
// wait until all jobs are completed
while (firstRunJob.get() == NOT_SET_JOB_ID) {
Thread.sleep(100);
}
MatcherAssert.assertThat("highest priority job should run if it is added before others", firstRunJob.get(), is(FIRST_JOB_ID));
}
use of com.path.android.jobqueue.JobManager 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));
}
Aggregations