Search in sources :

Example 41 with JobManager

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

the class ThreadFactoryTest method testThreadFactory.

@Test
public void testThreadFactory() throws Throwable {
    final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).timer(mockTimer).threadFactory(new ThreadFactory() {

        @Override
        public Thread newThread(@NonNull Runnable r) {
            return new DummyThread(r);
        }
    }));
    final Job job = new CheckWorkerJob();
    waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {

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

        @Override
        public void assertJob(Job job) {
        }
    });
    if (error != null) {
        throw error;
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) Configuration(com.birbit.android.jobqueue.config.Configuration) NonNull(android.support.annotation.NonNull) JobManager(com.birbit.android.jobqueue.JobManager) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test)

Example 42 with JobManager

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

the class PersistentJobTest method testPersistentJob.

@Test
public void testPersistentJob() throws Exception {
    JobManager jobManager = createJobManager();
    jobManager.addJob(new DummyPersistentLatchJob());
    persistentRunLatch.await(5, TimeUnit.SECONDS);
    MatcherAssert.assertThat((int) persistentRunLatch.getCount(), equalTo(0));
}
Also used : JobManager(com.birbit.android.jobqueue.JobManager) Test(org.junit.Test)

Example 43 with JobManager

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

the class ReRunWithLimitTest method testReRunWithLimitPersist.

@Test
public void testReRunWithLimitPersist() throws Exception {
    JobManager jobManager = createJobManager();
    testReRun(jobManager, true);
}
Also used : JobManager(com.birbit.android.jobqueue.JobManager) Test(org.junit.Test)

Example 44 with JobManager

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

the class ReRunWithLimitTest method testReRunWithLimit.

@Test
public void testReRunWithLimit() throws Exception {
    JobManager jobManager = createJobManager();
    testReRun(jobManager, false);
}
Also used : JobManager(com.birbit.android.jobqueue.JobManager) Test(org.junit.Test)

Example 45 with JobManager

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

the class RetryLogicTest method testChangeDelay.

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void testChangeDelay(boolean persistent) throws InterruptedException {
    canRun = true;
    RetryJob job = new RetryJob(new Params(1).setPersistent(persistent));
    job.retryLimit = 2;
    retryProvider = new RetryProvider() {

        @Override
        public RetryConstraint build(Job job, Throwable throwable, int runCount, int maxRunCount) {
            RetryConstraint constraint = new RetryConstraint(true);
            constraint.setNewDelayInMs(2000L);
            return constraint;
        }
    };
    final List<Long> runTimes = new ArrayList<>();
    onRunCallback = new Callback() {

        @Override
        public void on(Job job) {
            runTimes.add(mockTimer.nanoTime());
        }
    };
    final Throwable[] callbackError = new Throwable[1];
    final CountDownLatch runLatch = new CountDownLatch(2);
    final JobManager jobManager = createJobManager();
    jobManager.addCallback(new JobManagerCallbackAdapter() {

        @Override
        public void onAfterJobRun(@NonNull Job job, int resultCode) {
            try {
                mockTimer.incrementMs(1999);
                assertThat("no jobs should be ready", jobManager.countReadyJobs(), is(0));
                mockTimer.incrementMs(2);
            } catch (Throwable t) {
                callbackError[0] = t;
            } finally {
                runLatch.countDown();
            }
        }
    });
    jobManager.addJob(job);
    assertThat("on run callbacks should arrive", runLatch.await(100, TimeUnit.MINUTES), is(true));
    assertThat("run callback should not have any errors", callbackError[0], nullValue());
    assertThat("job should be canceled", cancelLatch.await(1, TimeUnit.SECONDS), is(true));
    assertThat("should run 2 times", runCount, is(2));
    long timeInBetween = TimeUnit.NANOSECONDS.toSeconds(runTimes.get(1) - runTimes.get(0));
    assertThat("time between two runs should be at least 2 seconds. " + timeInBetween, 2 <= timeInBetween, is(true));
}
Also used : JobManagerCallbackAdapter(com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter) ArrayList(java.util.ArrayList) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) TargetApi(android.annotation.TargetApi)

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