Search in sources :

Example 31 with Job

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

the class GroupingTest method testGroupingRaceCondition.

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Test
public void testGroupingRaceCondition() throws Exception {
    DummyNetworkUtilWithConnectivityEventSupport dummyNetworkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).maxConsumerCount(10).networkUtil(dummyNetworkUtil).timer(mockTimer));
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
    //add a bunch of network requring jobs
    final String GROUP_ID = "shared_group_id";
    final int AFTER_ADDED_JOBS_COUNT = 5;
    final int NOT_SET_JOB_ID = -1;
    final AtomicInteger firstRunJob = new AtomicInteger(NOT_SET_JOB_ID);
    final int FIRST_JOB_ID = -10;
    final CountDownLatch onAddedCalled = new CountDownLatch(1);
    final CountDownLatch remainingJobsOnAddedCalled = new CountDownLatch(AFTER_ADDED_JOBS_COUNT);
    final CountDownLatch aJobRun = new CountDownLatch(1);
    jobManager.addCallback(new JobManagerCallbackAdapter() {

        @Override
        public void onJobRun(@NonNull Job job, int resultCode) {
            aJobRun.countDown();
        }
    });
    jobManager.addJobInBackground(new DummyJob(new Params(10).requireNetwork().groupBy(GROUP_ID)) {

        @Override
        public void onAdded() {
            super.onAdded();
            onAddedCalled.countDown();
        }

        @Override
        public void onRun() throws Throwable {
            super.onRun();
            firstRunJob.compareAndSet(NOT_SET_JOB_ID, FIRST_JOB_ID);
        }
    });
    //ensure first jobs on added is called
    onAddedCalled.await();
    for (int i = 0; i < AFTER_ADDED_JOBS_COUNT; i++) {
        final int finalI = i;
        jobManager.addJob(new DummyJob(new Params(5).groupBy(GROUP_ID).requireNetwork()) {

            final int id = finalI + 1;

            @Override
            public void onAdded() {
                super.onAdded();
                remainingJobsOnAddedCalled.countDown();
            }

            @Override
            public void onRun() throws Throwable {
                super.onRun();
                firstRunJob.compareAndSet(NOT_SET_JOB_ID, id);
            }
        });
    }
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED, true);
    //wait until all jobs are completed
    aJobRun.await(1, TimeUnit.MINUTES);
    MatcherAssert.assertThat("highest priority job should run if it is added before others", firstRunJob.get(), is(FIRST_JOB_ID));
}
Also used : JobManagerCallbackAdapter(com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter) Configuration(com.birbit.android.jobqueue.config.Configuration) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Example 32 with Job

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

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

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

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

the class AddInBackgroundTest method addInBackground.

@Test
public void addInBackground() throws Throwable {
    long currentThreadId = Thread.currentThread().getId();
    final AtomicLong onAddedThreadId = new AtomicLong();
    final CountDownLatch addedLatch = new CountDownLatch(2);
    final Job dummyJob = new DummyJob(new Params(1).setDelayMs(delayed ? 1000 : 0)) {

        @Override
        public void onAdded() {
            super.onAdded();
            onAddedThreadId.set(Thread.currentThread().getId());
            addedLatch.countDown();
        }
    };
    JobManager jobManager = createJobManager();
    jobManager.stop();
    if (useCallback) {
        jobManager.addJobInBackground(dummyJob, new AsyncAddCallback() {

            @Override
            public void onAdded() {
                addedLatch.countDown();
            }
        });
    } else {
        addedLatch.countDown();
        jobManager.addJobInBackground(dummyJob);
    }
    addedLatch.await();
    MatcherAssert.assertThat("thread ids should be different. delayed:" + delayed, currentThreadId, CoreMatchers.not(onAddedThreadId.get()));
    if (useCallback) {
        JobHolder holder = new JobManagerThreadRunnable<JobHolder>(jobManager) {

            @Override
            public JobHolder onRun() {
                return findJobFromQueues(dummyJob.getId());
            }
        }.run();
        MatcherAssert.assertThat("there should be a job in the holder. id:" + dummyJob.getId() + ", delayed:" + delayed, holder, CoreMatchers.notNullValue());
        MatcherAssert.assertThat("id callback should have the proper id:", holder.getJob(), CoreMatchers.is(dummyJob));
    }
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) AtomicLong(java.util.concurrent.atomic.AtomicLong) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) AsyncAddCallback(com.birbit.android.jobqueue.AsyncAddCallback) CountDownLatch(java.util.concurrent.CountDownLatch) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test)

Aggregations

Job (com.birbit.android.jobqueue.Job)36 Params (com.birbit.android.jobqueue.Params)31 JobManager (com.birbit.android.jobqueue.JobManager)26 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)26 Test (org.junit.Test)21 CountDownLatch (java.util.concurrent.CountDownLatch)19 TargetApi (android.annotation.TargetApi)10 JobManagerCallbackAdapter (com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter)10 RetryConstraint (com.birbit.android.jobqueue.RetryConstraint)9 Configuration (com.birbit.android.jobqueue.config.Configuration)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 JobHolder (com.birbit.android.jobqueue.JobHolder)4 JobManagerCallback (com.birbit.android.jobqueue.callback.JobManagerCallback)4 ArrayList (java.util.ArrayList)4 CancelResult (com.birbit.android.jobqueue.CancelResult)3 SuppressLint (android.annotation.SuppressLint)2 NonNull (android.support.annotation.NonNull)2 SqliteJobQueue (com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue)2 Nullable (android.support.annotation.Nullable)1 Pair (android.util.Pair)1