use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class NetworkJobWithConnectivityListenerTest method testNetworkJobWithConnectivityListener.
@Test
public void testNetworkJobWithConnectivityListener() throws Exception {
final DummyNetworkUtilWithConnectivityEventSupport dummyNetworkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
final DummyJob dummyJob = new DummyJob(new Params(0).requireNetwork());
jobManager.addJob(dummyJob);
// no job to run so consumers should finish
jobManager.waitUntilConsumersAreFinished();
MatcherAssert.assertThat("count should be 1 as no jobs should be consumed w/o network", jobManager.count(), equalTo(1));
// JobManager may wake up as idle right here and see the new network value. sleep to avoid it
// count will trigger the queue and will result in another IDLE call. We need to wait until
// it is handled.
//noinspection SLEEP_IN_CODE
Thread.sleep(2000);
dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED, false);
//noinspection SLEEP_IN_CODE
//wait a little bit more to let consumer run
Thread.sleep(5000);
MatcherAssert.assertThat("even though network is recovered, job manager should not consume any job because it " + "does not know (we did not inform)", jobManager.count(), equalTo(1));
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED, true);
}
@Override
public void assertJob(Job job) {
MatcherAssert.assertThat("should be the added job", job, CoreMatchers.is((Job) dummyJob));
}
});
MatcherAssert.assertThat("job manager should consume network job after it is informed that network is recovered", jobManager.count(), equalTo(0));
}
use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class RunFailingJobTest method runFailingJob.
@Test
public void runFailingJob() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
JobManager jobManager = createJobManager();
jobManager.addJob(new Job(new Params(0).requireNetwork()) {
@Override
public void onAdded() {
}
@Override
public void onRun() throws Throwable {
throw new RuntimeException();
}
@Override
protected void onCancel(@CancelReason int cancelReason, @Nullable Throwable throwable) {
latch.countDown();
}
@Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
return RetryConstraint.CANCEL;
}
});
latch.await(10, TimeUnit.SECONDS);
MatcherAssert.assertThat((int) latch.getCount(), equalTo(0));
}
use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class RunManyNonPersistentTest method runManyNonPersistentJobs.
@Test
public void runManyNonPersistentJobs() throws Exception {
JobManager jobManager = createJobManager();
jobManager.stop();
int limit = 2;
final CountDownLatch latch = new CountDownLatch(limit);
for (int i = 0; i < limit; i++) {
jobManager.addJob(new DummyLatchJob(new Params(i), latch));
}
jobManager.start();
latch.await(10, TimeUnit.SECONDS);
MatcherAssert.assertThat((int) latch.getCount(), equalTo(0));
}
use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class SchedulerSimpleTestCase method testScheduleWhenJobAdded.
@Test
public void testScheduleWhenJobAdded() throws InterruptedException {
Scheduler scheduler = Mockito.mock(Scheduler.class);
ArgumentCaptor<SchedulerConstraint> captor = ArgumentCaptor.forClass(SchedulerConstraint.class);
DummyNetworkUtilWithConnectivityEventSupport networkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
Configuration.Builder builder = new Configuration.Builder(RuntimeEnvironment.application).timer(mockTimer).networkUtil(networkUtil).inTestMode().scheduler(scheduler, false);
if (requireUnmeteredNetwork) {
networkUtil.setNetworkStatus(NetworkUtil.UNMETERED);
} else if (requireNetwork) {
networkUtil.setNetworkStatus(NetworkUtil.METERED);
} else {
networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
}
final JobManager jobManager = createJobManager(builder);
Params params = new Params(1);
params.setPersistent(persistent);
params.setRequiresNetwork(requireNetwork);
params.setRequiresUnmeteredNetwork(requireUnmeteredNetwork);
params.setDelayMs(delayInMs);
if (deadline != null) {
params.overrideDeadlineToRunInMs(deadline);
}
final SchedulerJob job = new SchedulerJob(params);
final CountDownLatch cancelLatch = new CountDownLatch(1);
Mockito.doAnswer(new Answer() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
cancelLatch.countDown();
return null;
}
}).when(scheduler).cancelAll();
waitUntilJobsAreDone(jobManager, Collections.singletonList(job), new Runnable() {
@Override
public void run() {
jobManager.addJob(job);
mockTimer.incrementMs(delayInMs);
}
});
if (persistent && (requireNetwork || requireUnmeteredNetwork || delayInMs >= JobManager.MIN_DELAY_TO_USE_SCHEDULER_IN_MS || (deadline != null && deadline >= JobManager.MIN_DELAY_TO_USE_SCHEDULER_IN_MS))) {
Mockito.verify(scheduler).request(captor.capture());
SchedulerConstraint constraint = captor.getValue();
MatcherAssert.assertThat(constraint.getNetworkStatus(), CoreMatchers.is(requireUnmeteredNetwork ? NetworkUtil.UNMETERED : requireNetwork ? NetworkUtil.METERED : NetworkUtil.DISCONNECTED));
MatcherAssert.assertThat(constraint.getDelayInMs(), CoreMatchers.is(delayInMs));
// wait until cancel is called because it is called when JQ is idle.
// for more clear reporting, let mockito handle the check
MatcherAssert.assertThat(constraint.getOverrideDeadlineInMs(), CoreMatchers.is(deadline));
cancelLatch.await(30, TimeUnit.SECONDS);
Mockito.verify(scheduler).cancelAll();
} else {
Mockito.verify(scheduler, Mockito.never()).request(Mockito.any(SchedulerConstraint.class));
}
}
use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class SessionIdTest method testSessionId.
@Test
public void testSessionId() throws Throwable {
JobManager jobManager = createJobManager();
//we know job manager uses this value :/
Long sessionId = mockTimer.nanoTime();
jobManager.stop();
Job[] jobs = new Job[] { new DummyJob(new Params(0)), new DummyJob(new Params(0).persist()) };
for (Job job : jobs) {
jobManager.addJob(job);
}
for (int i = 0; i < jobs.length; i++) {
JobHolder jobHolder = nextJob(jobManager);
MatcherAssert.assertThat("session id should be correct for job " + i, jobHolder.getRunningSessionId(), equalTo(sessionId));
}
}
Aggregations