Search in sources :

Example 1 with TagConstraint

use of com.birbit.android.jobqueue.TagConstraint in project android-priority-jobqueue by yigit.

the class CancelFailingJobsTest method testCancelWithoutNetwork.

public void testCancelWithoutNetwork(boolean async, TagConstraint constraint) throws InterruptedException {
    final int jobCount = 30;
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).networkUtil(networkUtil).timer(mockTimer));
    networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
    for (int i = 0; i < jobCount; i++) {
        jobManager.addJob(new FailingJob(new Params(i).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");
        assertThat(cancelLatch.await(jobCount, TimeUnit.SECONDS), is(true));
    } else {
        result[0] = jobManager.cancelJobs(TagConstraint.ANY, "tag");
    }
    assertThat("all jobs should be cancelled", result[0].getCancelledJobs().size(), is(jobCount));
    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).groupBy("group").addTags("tag")) {

        @Override
        public void onRun() throws Throwable {
            super.onRun();
            runLatch.countDown();
        }
    });
    networkUtil.setNetworkStatus(NetworkUtil.UNMETERED, true);
    assertThat("new job should run w/o any issues", runLatch.await(2, TimeUnit.SECONDS), is(true));
}
Also used : CancelResult(com.birbit.android.jobqueue.CancelResult) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) TagConstraint(com.birbit.android.jobqueue.TagConstraint)

Aggregations

CancelResult (com.birbit.android.jobqueue.CancelResult)1 JobManager (com.birbit.android.jobqueue.JobManager)1 Params (com.birbit.android.jobqueue.Params)1 TagConstraint (com.birbit.android.jobqueue.TagConstraint)1 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)1 CountDownLatch (java.util.concurrent.CountDownLatch)1