Search in sources :

Example 51 with JobManager

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

the class HitDeadlineAfterException method failAndHitDeadline.

@Test
public void failAndHitDeadline() throws InterruptedException {
    final JobManager jobManager = createJobManager();
    final AtomicBoolean calledShouldReRun = new AtomicBoolean(false);
    final AtomicInteger reason = new AtomicInteger();
    final DummyJob job = new DummyJob(new Params(0).overrideDeadlineToCancelInMs(100)) {

        @Override
        public void onRun() throws Throwable {
            super.onRun();
            mockTimer.incrementMs(150);
            throw new RuntimeException("why not fail");
        }

        @Override
        protected void onCancel(@CancelReason int cancelReason, @Nullable Throwable throwable) {
            reason.set(cancelReason);
            super.onCancel(cancelReason, throwable);
        }

        @Override
        protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
            calledShouldReRun.set(true);
            return RetryConstraint.RETRY;
        }
    };
    waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {

        @Override
        public void run() {
            jobManager.addJob(job);
        }

        @Override
        public void assertJob(Job job) {
        }
    });
    MatcherAssert.assertThat(reason.get(), CoreMatchers.is(CancelReason.REACHED_DEADLINE));
    MatcherAssert.assertThat(calledShouldReRun.get(), CoreMatchers.is(false));
}
Also used : Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CancelReason(com.birbit.android.jobqueue.CancelReason) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NonNull(android.support.annotation.NonNull) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Nullable(android.support.annotation.Nullable) Test(org.junit.Test)

Example 52 with JobManager

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

the class CallbackTest method successPersistent.

@Test
public void successPersistent() throws Throwable {
    JobManagerCallback callback = mock(JobManagerCallback.class);
    final Job job = new PersistentDummyJob();
    final JobManager jobManager = createJobManager();
    jobManager.addCallback(callback);
    waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {

        @Override
        public void run() {
            jobManager.addJob(job);
        }

        @Override
        public void assertJob(Job job) {
        }
    });
    verify(callback).onJobAdded(any(PersistentDummyJob.class));
    verify(callback).onJobRun(any(PersistentDummyJob.class), eq(JobManagerCallback.RESULT_SUCCEED));
}
Also used : JobManager(com.birbit.android.jobqueue.JobManager) Job(com.birbit.android.jobqueue.Job) JobManagerCallback(com.birbit.android.jobqueue.callback.JobManagerCallback) Test(org.junit.Test)

Example 53 with JobManager

use of com.birbit.android.jobqueue.JobManager 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 54 with JobManager

use of com.birbit.android.jobqueue.JobManager 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 55 with JobManager

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

JobManager (com.birbit.android.jobqueue.JobManager)58 Params (com.birbit.android.jobqueue.Params)47 Test (org.junit.Test)40 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)35 Job (com.birbit.android.jobqueue.Job)26 CountDownLatch (java.util.concurrent.CountDownLatch)26 Configuration (com.birbit.android.jobqueue.config.Configuration)22 CancelResult (com.birbit.android.jobqueue.CancelResult)9 TargetApi (android.annotation.TargetApi)8 RetryConstraint (com.birbit.android.jobqueue.RetryConstraint)8 JobManagerCallbackAdapter (com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter)8 JobHolder (com.birbit.android.jobqueue.JobHolder)6 ArrayList (java.util.ArrayList)6 JobManagerCallback (com.birbit.android.jobqueue.callback.JobManagerCallback)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Semaphore (java.util.concurrent.Semaphore)3 SuppressLint (android.annotation.SuppressLint)2 NonNull (android.support.annotation.NonNull)2 Constraint (com.birbit.android.jobqueue.Constraint)2 TagConstraint (com.birbit.android.jobqueue.TagConstraint)2