use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class KeepAliveTest method testKeepAlive.
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void testKeepAlive(final DummyNetworkUtil networkUtil) throws Exception {
int keepAlive = 3;
final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).consumerKeepAlive(keepAlive).networkUtil(networkUtil).timer(mockTimer));
//give it a little time to create first consumer
final CountDownLatch jobDone = new CountDownLatch(1);
jobManager.addCallback(new JobManagerCallbackAdapter() {
@Override
public void onDone(@NonNull Job job) {
jobDone.countDown();
}
});
jobManager.addJob(new DummyJob(new Params(0)));
// Sync on job manager to ensure it handled add requests
jobManager.count();
MatcherAssert.assertThat("there should be 1 thread actively waiting for jobs", jobManager.getActiveConsumerCount(), equalTo(1));
MatcherAssert.assertThat(jobDone.await(1, TimeUnit.MINUTES), CoreMatchers.is(true));
// Sync on job manager to ensure it handled add requests
jobManager.count();
mockTimer.incrementNs((long) (JobManager.NETWORK_CHECK_INTERVAL + TimeUnit.SECONDS.toNanos(keepAlive) + 1));
FutureTask<Void> waitForConsumersFuture = new FutureTask<>(new Callable<Void>() {
@Override
public Void call() throws Exception {
jobManager.waitUntilConsumersAreFinished();
return null;
}
});
new Thread(waitForConsumersFuture).start();
waitForConsumersFuture.get(keepAlive * 10, TimeUnit.SECONDS);
jobManager.waitUntilConsumersAreFinished();
MatcherAssert.assertThat("after keep alive timeout, there should NOT be any threads waiting", jobManager.getActiveConsumerCount(), equalTo(0));
//disable network and add a network bound job
networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
final DummyJob dj1 = new DummyJob(new Params(0).requireNetwork());
jobManager.addJob(dj1);
// sync add job request
jobManager.count();
mockTimer.incrementNs(JobManager.NETWORK_CHECK_INTERVAL + TimeUnit.SECONDS.toNanos(keepAlive) * 2);
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
networkUtil.setNetworkStatus(NetworkUtil.METERED);
}
@Override
public void assertJob(Job job) {
Assert.assertThat("it should be dj1", job, is((Job) dj1));
}
});
MatcherAssert.assertThat("when network is recovered, job should be handled", jobManager.count(), equalTo(0));
}
use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class LoadFactorTest method testGoIdleIfNextJobCannotBeRunNow.
@SuppressLint("SLEEP_IN_CODE")
@Test
public void testGoIdleIfNextJobCannotBeRunNow() throws InterruptedException {
// see: https://github.com/yigit/android-priority-jobqueue/issues/262
final AtomicInteger nextJobDelayCall = new AtomicInteger(1);
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).maxConsumerCount(3).minConsumerCount(1).loadFactor(3).queueFactory(new QueueFactory() {
@Override
public JobQueue createPersistentQueue(Configuration configuration, long sessionId) {
return new SqliteJobQueue(configuration, sessionId, new SqliteJobQueue.JavaSerializer());
}
@Override
public JobQueue createNonPersistent(Configuration configuration, long sessionId) {
return new SimpleInMemoryPriorityQueue(configuration, sessionId) {
@Override
public Long getNextJobDelayUntilNs(@NonNull Constraint constraint) {
nextJobDelayCall.incrementAndGet();
return super.getNextJobDelayUntilNs(constraint);
}
};
}
}).timer(mockTimer));
final DummyJobWithStartEndLatch job1 = new DummyJobWithStartEndLatch(new Params(1));
final DummyJobWithStartEndLatch job2 = new DummyJobWithStartEndLatch(new Params(1));
jobManager.addJob(job1);
jobManager.addJob(job2);
assertThat(startLatch.await(5, TimeUnit.MINUTES), is(true));
// give it some time to cool down, ugly but nothing to do
Thread.sleep(2000);
int startCount = nextJobDelayCall.get();
Thread.sleep(5000);
assertThat("JobManager should not query any more next jobs", nextJobDelayCall.get(), is(startCount));
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
canEndLatch.countDown();
}
@Override
public void assertJob(Job job) {
}
});
}
use of com.birbit.android.jobqueue.Params 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.Params 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.Params 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));
}
Aggregations