Search in sources :

Example 6 with CancelResult

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

the class CancelBeforeRunningTest method testCancelBeforeRunningWithGroups.

@Test
public void testCancelBeforeRunningWithGroups() throws InterruptedException {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    DummyJob nonPersistentJob = new DummyJob(new Params(0).addTags("dummyTag").groupBy("group1"));
    DummyJob persistentJob = new DummyJob(new Params(0).addTags("dummyTag").persist().groupBy("group2"));
    jobManager.addJob(nonPersistentJob);
    jobManager.addJob(persistentJob);
    CancelResult result = jobManager.cancelJobs(TagConstraint.ANY, "dummyTag");
    assertThat("both jobs should be cancelled", result.getCancelledJobs().size(), is(2));
    assertThat("both jobs should be cancelled", result.getFailedToCancel().size(), is(0));
    for (Job j : result.getCancelledJobs()) {
        DummyJob job = (DummyJob) j;
        if (!job.isPersistent()) {
            assertThat("job is still added", job.getOnAddedCnt(), is(1));
        }
        assertThat("job is cancelled", job.getOnCancelCnt(), is(1));
        assertThat("job is NOT run", job.getOnRunCnt(), is(0));
    }
    assertThat("there should not be any jobs in the queue", jobManager.count(), is(0));
    jobManager.start();
    DummyJob nonPersistentJob2 = new DummyJob(new Params(0).addTags("dummyTag").groupBy("group1")) {

        @Override
        public void onRun() throws Throwable {
            super.onRun();
            nonPersistentJobLatch.countDown();
        }
    };
    DummyJob persistentJob2 = new PersistentDummyJob(new Params(0).addTags("dummyTag").groupBy("group2"));
    jobManager.addJob(persistentJob2);
    jobManager.addJob(nonPersistentJob2);
    assertThat("a new job in the same group with canceled job should run", nonPersistentJobLatch.await(1, TimeUnit.SECONDS), is(true));
    assertThat("a new persistent job in the same group with canceled job should run", persistentJobLatch.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) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test)

Example 7 with CancelResult

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

the class CancelFailingJobsTest method testCancelWithoutNetworkPersistent.

public void testCancelWithoutNetworkPersistent(boolean async, TagConstraint constraint) throws InterruptedException {
    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().groupBy("group").addTags("tag")));
    jobManager.addJob(new DummyJob(new Params(2).persist().groupBy("group").addTags("tag")));
    jobManager.addJob(new DummyJob(new Params(3).persist().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().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));
}
Also used : CancelResult(com.birbit.android.jobqueue.CancelResult) Configuration(com.birbit.android.jobqueue.config.Configuration) 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)

Example 8 with CancelResult

use of com.birbit.android.jobqueue.CancelResult 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)

Example 9 with CancelResult

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

the class CancelWhileRunningWithGroupsTest method testCancelBeforeRunning.

@Test
public void testCancelBeforeRunning() throws InterruptedException {
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).timer(mockTimer));
    DummyJobWithLatches nonPersistentJob = new DummyJobWithLatches(0, new Params(1).addTags("dummyTag").groupBy("group1"));
    jobManager.addJob(nonPersistentJob);
    DummyJobWithLatches persistentJob = new DummyJobWithLatches(0, new Params(1).addTags("dummyTag").groupBy("group2").persist());
    jobManager.addJob(persistentJob);
    assertThat("both jobs should start", startLatches[0].await(2, TimeUnit.SECONDS), is(true));
    final CancelResult[] cancelResults = new CancelResult[1];
    final CountDownLatch resultLatch = new CountDownLatch(1);
    startLatches[0].await(2, TimeUnit.SECONDS);
    jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {

        @Override
        public void onCancelled(CancelResult cancelResult) {
            cancelResults[0] = cancelResult;
            resultLatch.countDown();
        }
    }, TagConstraint.ANY, "dummyTag");
    while (!nonPersistentJob.isCancelled()) {
    // wait
    }
    endLatches[0].countDown();
    endLatches[0].countDown();
    assertThat("result should come after jobs end", resultLatch.await(2, TimeUnit.SECONDS), is(true));
    assertThat("no jobs should be canceled", cancelResults[0].getCancelledJobs().size(), is(0));
    assertThat("both jobs should fail to cancel", cancelResults[0].getFailedToCancel().size(), is(2));
    jobManager.addJob(new DummyJobWithLatches(1, new Params(1).addTags("dummyTag").groupBy("group1")));
    jobManager.addJob(new DummyJobWithLatches(1, new Params(1).addTags("dummyTag").groupBy("group2").persist()));
    assertThat("new jobs with canceled groups should start", startLatches[1].await(10, TimeUnit.SECONDS), is(true));
    endLatches[1].countDown();
    endLatches[1].countDown();
}
Also used : CancelResult(com.birbit.android.jobqueue.CancelResult) Configuration(com.birbit.android.jobqueue.config.Configuration) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

CancelResult (com.birbit.android.jobqueue.CancelResult)9 JobManager (com.birbit.android.jobqueue.JobManager)9 Params (com.birbit.android.jobqueue.Params)9 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Configuration (com.birbit.android.jobqueue.config.Configuration)5 Test (org.junit.Test)5 Job (com.birbit.android.jobqueue.Job)3 TagConstraint (com.birbit.android.jobqueue.TagConstraint)2 RetryConstraint (com.birbit.android.jobqueue.RetryConstraint)1 LinkedList (java.util.LinkedList)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1