use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.
the class KeepAliveTest method testKeepAlive.
@Test
public void testKeepAlive() throws Exception {
int keepAlive = 3 + (int) (Math.random() * 5);
DummyNetworkUtil networkUtilWithoutEventSupport = new DummyNetworkUtil();
DummyNetworkUtilWithConnectivityEventSupport networkUtilWithEventSupport = new DummyNetworkUtilWithConnectivityEventSupport();
JobManager jobManager1 = createJobManager(new Configuration.Builder(Robolectric.application).consumerKeepAlive(keepAlive).networkUtil(networkUtilWithoutEventSupport));
JobManager jobManager2 = createJobManager(new Configuration.Builder(Robolectric.application).consumerKeepAlive(keepAlive).networkUtil(networkUtilWithEventSupport));
// give it a little time to create first consumer
jobManager1.addJob(new DummyJob(new Params(0)));
jobManager2.addJob(new DummyJob(new Params(0)));
AtomicInteger activeThreadCount1 = getActiveConsumerCount(getConsumerExecutor(jobManager1)).get();
AtomicInteger activeThreadCount2 = getActiveConsumerCount(getConsumerExecutor(jobManager2)).get();
Thread.sleep(1000);
MatcherAssert.assertThat("there should be 1 thread actively waiting for jobs", activeThreadCount1.get(), equalTo(1));
MatcherAssert.assertThat("there should be one thread actively waiting for jobs", activeThreadCount2.get(), equalTo(1));
// sleep till it dies
Thread.sleep((long) (TimeUnit.SECONDS.toMillis(keepAlive) * 1.33));
MatcherAssert.assertThat("after keep alive timeout, there should NOT be any threads waiting", activeThreadCount1.get(), equalTo(0));
MatcherAssert.assertThat("after keep alive timeout, there should NOT be any threads waiting", activeThreadCount2.get(), equalTo(0));
// disable network and add a network bound job
networkUtilWithoutEventSupport.setHasNetwork(false);
networkUtilWithEventSupport.setHasNetwork(false, true);
jobManager1.addJob(new DummyJob(new Params(0).requireNetwork()));
jobManager2.addJob(new DummyJob(new Params(0).requireNetwork()));
Thread.sleep(1000 + (long) (TimeUnit.SECONDS.toMillis(keepAlive) * 2));
MatcherAssert.assertThat("when network changes cannot be detected, there should be a consumer waiting alive", activeThreadCount1.get(), equalTo(1));
MatcherAssert.assertThat("when network changes can be detected, there should not be a consumer waiting alive", activeThreadCount2.get(), equalTo(0));
networkUtilWithEventSupport.setHasNetwork(true, true);
networkUtilWithoutEventSupport.setHasNetwork(true);
Thread.sleep(500);
MatcherAssert.assertThat("when network is recovered, job should be handled", jobManager2.count(), equalTo(0));
Thread.sleep(1000);
MatcherAssert.assertThat("when network is recovered, job should be handled", jobManager1.count(), equalTo(0));
}
use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.
the class NetworkJobTest method testNetworkJob.
@Test
public void testNetworkJob() throws Exception {
JobManagerTestBase.DummyNetworkUtil dummyNetworkUtil = new JobManagerTestBase.DummyNetworkUtil();
JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).networkUtil(dummyNetworkUtil));
jobManager.stop();
DummyJob networkDummyJob = new DummyJob(new Params(5).requireNetwork());
jobManager.addJob(networkDummyJob);
DummyJob noNetworkDummyJob = new DummyJob(new Params(2));
jobManager.addJob(noNetworkDummyJob);
DummyJob networkPersistentJob = new DummyJob(new Params(6).persist().requireNetwork());
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.setHasNetwork(true);
MatcherAssert.assertThat("count should be correct if there is network and non-network jobs w/o network", jobManager.count(), equalTo(4));
dummyNetworkUtil.setHasNetwork(false);
jobManager.start();
// this should be enough to consume dummy jobs
Thread.sleep(1000);
MatcherAssert.assertThat("no network jobs should be executed even if there is no network", jobManager.count(), equalTo(2));
dummyNetworkUtil.setHasNetwork(true);
// this should be enough to consume dummy jobs
Thread.sleep(1000);
MatcherAssert.assertThat("when network is recovered, all network jobs should be automatically consumed", jobManager.count(), equalTo(0));
}
use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.
the class NetworkNextJobTest method testNetworkNextJob.
@Test
public void testNetworkNextJob() throws Exception {
DummyNetworkUtil dummyNetworkUtil = new DummyNetworkUtil();
JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).networkUtil(dummyNetworkUtil));
jobManager.stop();
DummyJob dummyJob = new DummyJob(new Params(0).requireNetwork());
long dummyJobId = jobManager.addJob(dummyJob);
dummyNetworkUtil.setHasNetwork(false);
Invoker<JobHolder> nextJobMethod = getNextJobMethod(jobManager);
MatcherAssert.assertThat("when there isn't any network, next job should return null", nextJobMethod.invoke(), nullValue());
MatcherAssert.assertThat("even if there is network, job manager should return correct count", jobManager.count(), equalTo(1));
dummyNetworkUtil.setHasNetwork(true);
JobHolder retrieved = nextJobMethod.invoke();
MatcherAssert.assertThat("when network is recovered, next job should be retrieved", retrieved, notNullValue());
}
use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.
the class AddedCountTest method testAddedCount.
@Test
public void testAddedCount() throws Exception {
testAddedCount(new DummyJob(new Params(0)));
testAddedCount(new DummyJob(new Params(0).persist()));
}
use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.
the class ClearTest method testClear.
@Test
public void testClear() throws Exception {
JobManager jobManager = createJobManager();
final int LIMIT = 20;
for (int i = 0; i < LIMIT; i++) {
jobManager.addJob(new DummyJob(new Params(0).setPersistent(i % 2 == 1)));
}
jobManager.clear();
MatcherAssert.assertThat("after clear, count should be 0", jobManager.count(), equalTo(0));
}
Aggregations