use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class NetworkJobTest method testNetworkJobWithTimeout.
@Test
public void testNetworkJobWithTimeout() throws InterruptedException {
JobManagerTestBase.DummyNetworkUtil dummyNetworkUtil = new JobManagerTestBase.DummyNetworkUtil();
dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
final CountDownLatch runLatch = new CountDownLatch(1);
DummyJob networkDummyJob = new DummyJob(addRequirement(new Params(1), 4)) {
@Override
public void onRun() throws Throwable {
runLatch.countDown();
super.onRun();
}
};
jobManager.addJob(networkDummyJob);
MatcherAssert.assertThat("job should not run", runLatch.await(3, TimeUnit.SECONDS), is(false));
mockTimer.incrementMs(4);
MatcherAssert.assertThat("job should run because network wait timed out", runLatch.await(3, TimeUnit.SECONDS), is(true));
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class NetworkJobTest method testPersistentNetworkJobWithTimeout.
@Test
public void testPersistentNetworkJobWithTimeout() throws InterruptedException {
JobManagerTestBase.DummyNetworkUtil dummyNetworkUtil = new JobManagerTestBase.DummyNetworkUtil();
dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
PersistentDummyJob networkDummyJob = new PersistentDummyJob(addRequirement(new Params(1), 4));
jobManager.addJob(networkDummyJob);
MatcherAssert.assertThat("job should not run", persistentDummyJobRunLatch.await(3, TimeUnit.SECONDS), is(false));
mockTimer.incrementMs(4);
MatcherAssert.assertThat("job should run because network wait timed out", persistentDummyJobRunLatch.await(3, TimeUnit.SECONDS), is(true));
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class NetworkJobTest method testNetworkJob.
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Test
public void testNetworkJob() throws Exception {
enableDebug();
JobManagerTestBase.DummyNetworkUtil dummyNetworkUtil = new JobManagerTestBase.DummyNetworkUtil();
final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
jobManager.stop();
DummyJob networkDummyJob = new DummyJob(addRequirement(new Params(5)));
jobManager.addJob(networkDummyJob);
DummyJob noNetworkDummyJob = new DummyJob(new Params(2));
jobManager.addJob(noNetworkDummyJob);
DummyJob networkPersistentJob = new DummyJob(addRequirement(new Params(6).persist()));
jobManager.addJob(networkPersistentJob);
DummyJob noNetworkPersistentJob = new DummyJob(new Params(1).persist());
jobManager.addJob(noNetworkPersistentJob);
MatcherAssert.assertThat("count should be correct if there are network and non-network jobs w/o network", jobManager.count(), equalTo(4));
dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED);
MatcherAssert.assertThat("count should be correct if there is network and non-network jobs w/o network", jobManager.count(), equalTo(4));
dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
final CountDownLatch noNetworkLatch = new CountDownLatch(2);
jobManager.addCallback(new JobManagerCallbackAdapter() {
@Override
public void onAfterJobRun(@NonNull Job job, int resultCode) {
if (resultCode == JobManagerCallback.RESULT_SUCCEED) {
MatcherAssert.assertThat("should be a no network job", job.requiresNetwork(), is(false));
noNetworkLatch.countDown();
if (noNetworkLatch.getCount() == 0) {
jobManager.removeCallback(this);
}
}
}
});
jobManager.start();
MatcherAssert.assertThat(noNetworkLatch.await(1, TimeUnit.MINUTES), is(true));
MatcherAssert.assertThat("no network jobs should be executed even if there is no network", jobManager.count(), equalTo(2));
final CountDownLatch networkLatch = new CountDownLatch(2);
jobManager.addCallback(new JobManagerCallbackAdapter() {
@Override
public void onAfterJobRun(@NonNull Job job, int resultCode) {
if (resultCode == JobManagerCallback.RESULT_SUCCEED) {
MatcherAssert.assertThat("should be a network job", job.requiresNetwork(), is(true));
networkLatch.countDown();
if (networkLatch.getCount() == 0) {
jobManager.removeCallback(this);
}
}
}
});
dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED);
// network check delay, make public?
mockTimer.incrementMs(10000);
if (unmetered) {
MatcherAssert.assertThat("if jobs require unmetered, they should not be run", networkLatch.await(10, TimeUnit.SECONDS), is(false));
MatcherAssert.assertThat(networkLatch.getCount(), is(2L));
dummyNetworkUtil.setNetworkStatus(NetworkUtil.UNMETERED);
// network check delay
mockTimer.incrementMs(10000);
}
MatcherAssert.assertThat(networkLatch.await(1, TimeUnit.MINUTES), is(true));
MatcherAssert.assertThat("when network is recovered, all network jobs should be automatically consumed", jobManager.count(), equalTo(0));
}
use of com.birbit.android.jobqueue.JobManager 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.JobManager 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));
}
Aggregations