use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.
the class CancelWithNetworkToggleTest method testCancelWithoutNetwork.
public void testCancelWithoutNetwork(boolean async, TagConstraint constraint) throws InterruptedException {
DummyNetworkUtilWithConnectivityEventSupport networkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).networkUtil(networkUtil).timer(mockTimer));
networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
jobManager.addJob(new DummyJob(new Params(1).requireNetwork().groupBy("group").addTags("tag")));
jobManager.addJob(new DummyJob(new Params(2).requireNetwork().groupBy("group").addTags("tag")));
jobManager.addJob(new DummyJob(new Params(3).requireNetwork().groupBy("group").addTags("tag")));
final CancelResult[] result = new CancelResult[1];
if (async) {
final CountDownLatch cancelLatch = new CountDownLatch(1);
jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {
@Override
public void onCancelled(CancelResult cancelResult) {
result[0] = cancelResult;
cancelLatch.countDown();
}
}, constraint, "tag");
cancelLatch.await(2, TimeUnit.SECONDS);
} else {
result[0] = jobManager.cancelJobs(TagConstraint.ANY, "tag");
}
assertThat("all jobs should be canceled", result[0].getCancelledJobs().size(), is(3));
assertThat("no jobs should fail to cancel", result[0].getFailedToCancel().size(), is(0));
final CountDownLatch runLatch = new CountDownLatch(1);
jobManager.addJob(new DummyJob(new Params(1).requireNetwork().groupBy("group").addTags("tag")) {
@Override
public void onRun() throws Throwable {
super.onRun();
runLatch.countDown();
}
});
networkUtil.setNetworkStatus(NetworkUtil.METERED, true);
assertThat("new job should run w/o any issues", runLatch.await(2, TimeUnit.SECONDS), is(true));
}
use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.
the class CancelWithNetworkToggleTest method testCancelWithoutNetworkPersistent.
public void testCancelWithoutNetworkPersistent(boolean async, TagConstraint constraint) throws InterruptedException {
DummyNetworkUtilWithConnectivityEventSupport networkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).networkUtil(networkUtil));
networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
jobManager.addJob(new DummyJob(new Params(1).persist().requireNetwork().groupBy("group").addTags("tag")));
jobManager.addJob(new DummyJob(new Params(2).persist().requireNetwork().groupBy("group").addTags("tag")));
jobManager.addJob(new DummyJob(new Params(3).persist().requireNetwork().groupBy("group").addTags("tag")));
final CancelResult[] result = new CancelResult[1];
if (async) {
final CountDownLatch cancelLatch = new CountDownLatch(1);
jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {
@Override
public void onCancelled(CancelResult cancelResult) {
result[0] = cancelResult;
cancelLatch.countDown();
}
}, constraint, "tag");
cancelLatch.await(2, TimeUnit.SECONDS);
} else {
result[0] = jobManager.cancelJobs(TagConstraint.ANY, "tag");
}
assertThat("all jobs should be canceled", result[0].getCancelledJobs().size(), is(3));
assertThat("no jobs should fail to cancel", result[0].getFailedToCancel().size(), is(0));
final CountDownLatch runLatch = persistentLatches[latchCounter++];
jobManager.addJob(new PersistentDummyJob(new Params(3).persist().requireNetwork().groupBy("group").addTags("tag"), latchCounter - 1));
networkUtil.setNetworkStatus(NetworkUtil.METERED, true);
assertThat("new job should run w/o any issues", runLatch.await(2, TimeUnit.SECONDS), is(true));
}
use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.
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));
}
use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.
the class ConsumerCountTest method testMaxConsumerCount.
@Test
public void testMaxConsumerCount() throws Exception {
int maxConsumerCount = 2;
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).maxConsumerCount(maxConsumerCount).loadFactor(maxConsumerCount).timer(mockTimer));
Semaphore semaphore = new Semaphore(maxConsumerCount);
int totalJobCount = maxConsumerCount * 3;
List<NeverEndingDummyJob> runningJobs = new ArrayList<>(totalJobCount);
for (int i = 0; i < totalJobCount; i++) {
NeverEndingDummyJob job = new NeverEndingDummyJob(new Params((int) (Math.random() * 3)), runLock, semaphore);
runningJobs.add(job);
jobManager.addJob(job);
}
Timer timer = new SystemTimer();
//wait till enough jobs start
long start = timer.nanoTime();
long tenSeconds = TimeUnit.SECONDS.toNanos(10);
while (timer.nanoTime() - start < tenSeconds && semaphore.tryAcquire()) {
semaphore.release();
//noinspection SLEEP_IN_CODE
Thread.sleep(100);
}
MatcherAssert.assertThat("all consumers should start in 10 seconds", timer.nanoTime() - start > tenSeconds, is(false));
//wait some more to ensure no more jobs are started
//noinspection SLEEP_IN_CODE
Thread.sleep(TimeUnit.SECONDS.toMillis(3));
int totalRunningCount = 0;
for (DummyJob job : runningJobs) {
totalRunningCount += job.getOnRunCnt();
}
MatcherAssert.assertThat("only maxConsumerCount jobs should start", totalRunningCount, equalTo(maxConsumerCount));
waitUntilJobsAreDone(jobManager, runningJobs, new Runnable() {
@Override
public void run() {
runLock.countDown();
}
});
MatcherAssert.assertThat("no jobs should remain", jobManager.count(), equalTo(0));
}
use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.
the class CountTest method testCount.
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Test
public void testCount() throws Exception {
enableDebug();
JobManager jobManager = createJobManager();
jobManager.stop();
for (int i = 0; i < 10; i++) {
jobManager.addJob(new DummyJob(new Params(0).persist()));
MatcherAssert.assertThat((int) jobManager.count(), equalTo(i * 2 + 1));
jobManager.addJob(new DummyJob(new Params(0).persist()));
MatcherAssert.assertThat((int) jobManager.count(), equalTo(i * 2 + 2));
}
final CountDownLatch jobsToRun = new CountDownLatch(20);
jobManager.addCallback(new JobManagerCallbackAdapter() {
@Override
public void onAfterJobRun(@NonNull Job job, int resultCode) {
if (resultCode == JobManagerCallback.RESULT_SUCCEED) {
jobsToRun.countDown();
}
}
});
jobManager.start();
MatcherAssert.assertThat("test sanity", jobsToRun.await(1, TimeUnit.MINUTES), is(true));
MatcherAssert.assertThat((int) jobManager.count(), equalTo(0));
}
Aggregations