use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class CancelWhileRunningTest method testCancelBeforeRunning.
@Test
public void testCancelBeforeRunning() throws InterruptedException {
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).timer(mockTimer));
JobWithEndLatch nonPersistent1 = new JobWithEndLatch(new Params(0).addTags("dummyTag"), true);
JobWithEndLatch nonPersistent2 = new JobWithEndLatch(new Params(0).addTags("dummyTag"), false);
DummyJob persistentJob1 = new PersistentJobWithEndLatch(new Params(0).addTags("dummyTag"), false);
DummyJob persistentJob2 = new PersistentJobWithEndLatch(new Params(0).addTags("dummyTag"), true);
jobManager.addJob(nonPersistent1);
jobManager.addJob(nonPersistent2);
jobManager.addJob(persistentJob1);
jobManager.addJob(persistentJob2);
onStartLatch.await();
nonPersistent1.onStartLatch.await();
nonPersistent2.onStartLatch.await();
final CancelResult[] resultHolder = new CancelResult[2];
final CountDownLatch cancelLatch = new CountDownLatch(1);
jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {
@Override
public void onCancelled(CancelResult cancelResult) {
resultHolder[0] = cancelResult;
cancelLatch.countDown();
}
}, TagConstraint.ANY, "dummyTag");
jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {
@Override
public void onCancelled(CancelResult cancelResult) {
resultHolder[1] = cancelResult;
}
}, TagConstraint.ANY, "dummyTag");
assertThat("result should not arrive until existing jobs finish", cancelLatch.await(4, TimeUnit.SECONDS), is(false));
onEndLatch.countDown();
nonPersistent1.onEndLatch.countDown();
nonPersistent2.onEndLatch.countDown();
assertThat("when jobs in question are finished, cancel callback should be triggered", cancelLatch.await(10, TimeUnit.SECONDS), is(true));
final CancelResult result = resultHolder[0];
JqLog.d("cancelled jobs %s", result.getCancelledJobs());
JqLog.d("failed to cancel %s", result.getFailedToCancel());
assertThat("two jobs should be cancelled", result.getCancelledJobs().size(), is(2));
assertThat("two jobs should fail to cancel", result.getFailedToCancel().size(), is(2));
for (Job j : result.getCancelledJobs()) {
FailingJob job = (FailingJob) j;
if (!job.isPersistent()) {
assertThat("job is still added", job.getOnAddedCnt(), is(1));
}
if (job.fail) {
assertThat("job is cancelled", job.getOnCancelCnt(), is(1));
} else {
assertThat("job could not be cancelled", job.getOnCancelCnt(), is(0));
}
}
for (Job j : result.getFailedToCancel()) {
FailingJob job = (FailingJob) j;
if (!job.isPersistent()) {
assertThat("job is still added", job.getOnAddedCnt(), is(1));
}
if (job.fail) {
assertThat("job is cancelled", job.getOnCancelCnt(), is(1));
} else {
assertThat("job could not be cancelled", job.getOnCancelCnt(), is(0));
}
}
assertThat("second cancel should not cancel anything", resultHolder[1].getCancelledJobs().size(), is(0));
assertThat("second cancel should not cancel anything", resultHolder[1].getFailedToCancel().size(), is(0));
}
use of com.birbit.android.jobqueue.JobManager 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.JobManager 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.JobManager 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.JobManager 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));
}
Aggregations