use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.
the class SlowOnAddedTest method testPersistent.
@Test
public void testPersistent() throws InterruptedException {
JobManager jobManager = createJobManager();
MyDummyPersistentJob.persistentJobLatch = new CountDownLatch(1);
for (int i = 0; i < 50; i++) {
jobManager.addJob(new DummyJob(new Params(1).persist()));
}
jobManager.addJob(new MyDummyPersistentJob(2));
MyDummyPersistentJob.persistentJobLatch.await();
assertThat("even if job is persistent, onAdded should be called b4 onRun", MyDummyPersistentJob.onAddedCountWhenOnRun, equalTo(1));
}
use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.
the class InjectorTest method testInjector.
@Test
public void testInjector() throws Throwable {
Configuration.Builder builder = new Configuration.Builder(RuntimeEnvironment.application);
final JobManagerTestBase.ObjectReference injectedJobReference = new JobManagerTestBase.ObjectReference();
final AtomicInteger injectionCallCount = new AtomicInteger(0);
DependencyInjector dependencyInjector = new DependencyInjector() {
@Override
public void inject(Job job) {
injectedJobReference.setObject(job);
injectionCallCount.incrementAndGet();
}
};
builder.injector(dependencyInjector);
builder.timer(mockTimer);
JobManager jobManager = createJobManager(builder);
jobManager.stop();
jobManager.addJob(new DummyJob(new Params(4)));
MatcherAssert.assertThat("injection should be called after adding a non-persistent job", injectionCallCount.get(), equalTo(1));
jobManager.addJob(new DummyJob(new Params(1).persist()));
MatcherAssert.assertThat("injection should be called after adding a persistent job", injectionCallCount.get(), equalTo(2));
JobHolder holder = nextJob(jobManager);
MatcherAssert.assertThat("injection should NOT be called for non persistent job", holder.getJob(), not(injectedJobReference.getObject()));
MatcherAssert.assertThat("injection should be called once for non persistent job", injectionCallCount.get(), equalTo(2));
holder = nextJob(jobManager);
MatcherAssert.assertThat("injection should be called for persistent job", holder.getJob(), equalTo(injectedJobReference.getObject()));
MatcherAssert.assertThat("injection should be called two times for persistent job", injectionCallCount.get(), equalTo(3));
}
use of com.birbit.android.jobqueue.test.jobs.DummyJob 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.test.jobs.DummyJob 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.test.jobs.DummyJob 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