Search in sources :

Example 1 with BaseJob

use of com.path.android.jobqueue.BaseJob in project android-priority-jobqueue by path.

the class InjectorTest method testInjector.

@Test
public void testInjector() throws Exception {
    Configuration.Builder builder = new Configuration.Builder(Robolectric.application);
    final JobManagerTestBase.ObjectReference injectedJobReference = new JobManagerTestBase.ObjectReference();
    final AtomicInteger injectionCallCount = new AtomicInteger(0);
    DependencyInjector dependencyInjector = new DependencyInjector() {

        @Override
        public void inject(BaseJob job) {
            injectedJobReference.setObject(job);
            injectionCallCount.incrementAndGet();
        }
    };
    builder.injector(dependencyInjector);
    JobManager jobManager = createJobManager(builder);
    jobManager.stop();
    jobManager.addJob(new DummyJob(new Params(4)));
    MatcherAssert.assertThat("injection should be called after adding a non-persistent job", injectionCallCount.get(), equalTo(1));
    jobManager.addJob(new DummyJob(new Params(1).persist()));
    MatcherAssert.assertThat("injection should be called after adding a persistent job", injectionCallCount.get(), equalTo(2));
    JobHolder holder = getNextJobMethod(jobManager).invoke();
    MatcherAssert.assertThat("injection should NOT be called for non persistent job", holder.getBaseJob(), not(injectedJobReference.getObject()));
    MatcherAssert.assertThat("injection should be called once for non persistent job", injectionCallCount.get(), equalTo(2));
    holder = getNextJobMethod(jobManager).invoke();
    MatcherAssert.assertThat("injection should be called for persistent job", holder.getBaseJob(), equalTo(injectedJobReference.getObject()));
    MatcherAssert.assertThat("injection should be called two times for persistent job", injectionCallCount.get(), equalTo(3));
}
Also used : DependencyInjector(com.path.android.jobqueue.di.DependencyInjector) Configuration(com.path.android.jobqueue.config.Configuration) BaseJob(com.path.android.jobqueue.BaseJob) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) JobHolder(com.path.android.jobqueue.JobHolder) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 2 with BaseJob

use of com.path.android.jobqueue.BaseJob in project android-priority-jobqueue by path.

the class RunFailingJobTest method runFailingJob.

@Test
public void runFailingJob() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    JobManager jobManager = createJobManager();
    jobManager.addJob(0, new BaseJob(true) {

        @Override
        public void onAdded() {
        }

        @Override
        public void onRun() throws Throwable {
            throw new RuntimeException();
        }

        @Override
        protected void onCancel() {
            latch.countDown();
        }

        @Override
        protected boolean shouldReRunOnThrowable(Throwable throwable) {
            return false;
        }
    });
    latch.await(10, TimeUnit.SECONDS);
    MatcherAssert.assertThat((int) latch.getCount(), equalTo(0));
}
Also used : BaseJob(com.path.android.jobqueue.BaseJob) JobManager(com.path.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with BaseJob

use of com.path.android.jobqueue.BaseJob in project android-priority-jobqueue by path.

the class AddInBackgroundTest method addInBackground.

public void addInBackground(boolean delayed, boolean useCallback) throws InterruptedException {
    long currentThreadId = Thread.currentThread().getId();
    final AtomicLong onAddedThreadId = new AtomicLong();
    final CountDownLatch addedLatch = new CountDownLatch(2);
    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();
    final AtomicLong jobId = new AtomicLong(0);
    if (useCallback) {
        jobManager.addJobInBackground(dummyJob, new AsyncAddCallback() {

            @Override
            public void onAdded(long id) {
                jobId.set(id);
                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) {
        JobQueue queue = getNonPersistentQueue(jobManager);
        JobHolder holder = queue.findJobById(jobId.longValue());
        MatcherAssert.assertThat("there should be a job in the holder. id:" + jobId.longValue() + ", delayed:" + delayed + ", use cb:" + useCallback, holder, CoreMatchers.notNullValue());
        MatcherAssert.assertThat("id callback should have the proper id:", holder.getBaseJob(), CoreMatchers.is((BaseJob) dummyJob));
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) AtomicLong(java.util.concurrent.atomic.AtomicLong) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) JobQueue(com.path.android.jobqueue.JobQueue) BaseJob(com.path.android.jobqueue.BaseJob) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) AsyncAddCallback(com.path.android.jobqueue.AsyncAddCallback) CountDownLatch(java.util.concurrent.CountDownLatch) BaseJob(com.path.android.jobqueue.BaseJob) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Job(com.path.android.jobqueue.Job)

Example 4 with BaseJob

use of com.path.android.jobqueue.BaseJob in project android-priority-jobqueue by path.

the class InjectorTest method testInjectorCrash.

@Test
public void testInjectorCrash() throws Exception {
    final String EXCEPTION_MESSAGE = "could not inject for whatever reason :)";
    DependencyInjector dummyDependencyInjector = new DependencyInjector() {

        @Override
        public void inject(BaseJob baseJob) {
            throw new RuntimeException(EXCEPTION_MESSAGE);
        }
    };
    final ObjectReference objectReference = new ObjectReference();
    final CountDownLatch exceptionLatch = new CountDownLatch(1);
    CustomLogger customLogger = new CustomLogger() {

        @Override
        public boolean isDebugEnabled() {
            return false;
        }

        @Override
        public void d(String s, Object... objects) {
        }

        @Override
        public void e(Throwable throwable, String s, Object... objects) {
            objectReference.setObject(throwable);
            exceptionLatch.countDown();
        }

        @Override
        public void e(String s, Object... objects) {
        // 
        }
    };
    JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).injector(dummyDependencyInjector).customLogger(customLogger));
    Throwable addException = null;
    try {
        jobManager.addJob(new DummyJob(new Params(0)));
    } catch (Throwable t) {
        addException = t;
    }
    MatcherAssert.assertThat("addJob should throw exception if dependency injector throws exception", addException, notNullValue());
    jobManager.addJobInBackground(new DummyJob(new Params(0)));
    exceptionLatch.await(2, TimeUnit.SECONDS);
    MatcherAssert.assertThat("there should be a received exception", objectReference.getObject(), notNullValue());
    MatcherAssert.assertThat("logged exception should be a runtime exception", objectReference.getObject(), instanceOf(RuntimeException.class));
    MatcherAssert.assertThat("logged exception should have expected message", ((Throwable) objectReference.getObject()).getMessage(), is(EXCEPTION_MESSAGE));
}
Also used : DependencyInjector(com.path.android.jobqueue.di.DependencyInjector) Configuration(com.path.android.jobqueue.config.Configuration) BaseJob(com.path.android.jobqueue.BaseJob) Params(com.path.android.jobqueue.Params) CustomLogger(com.path.android.jobqueue.log.CustomLogger) JobManager(com.path.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Test(org.junit.Test)

Example 5 with BaseJob

use of com.path.android.jobqueue.BaseJob in project android-priority-jobqueue by path.

the class PriorityTest method testPriority.

public void testPriority(JobManager jobManager, boolean persist) throws Exception {
    priorityRunLatch = new CountDownLatch(2);
    DummyJobWithRunOrderAssert.globalRunCount = new AtomicInteger(0);
    BaseJob job1 = new DummyJobWithRunOrderAssert(2, persist);
    BaseJob job2 = new DummyJobWithRunOrderAssert(1, persist);
    jobManager.stop();
    jobManager.addJob(1, job1);
    jobManager.addJob(2, job2);
    jobManager.start();
    priorityRunLatch.await(4, TimeUnit.SECONDS);
    // ensure both jobs did run
    MatcherAssert.assertThat((int) priorityRunLatch.getCount(), equalTo(0));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BaseJob(com.path.android.jobqueue.BaseJob) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

BaseJob (com.path.android.jobqueue.BaseJob)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 JobManager (com.path.android.jobqueue.JobManager)4 Params (com.path.android.jobqueue.Params)4 Test (org.junit.Test)4 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)3 JobHolder (com.path.android.jobqueue.JobHolder)2 Configuration (com.path.android.jobqueue.config.Configuration)2 DependencyInjector (com.path.android.jobqueue.di.DependencyInjector)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AsyncAddCallback (com.path.android.jobqueue.AsyncAddCallback)1 Job (com.path.android.jobqueue.Job)1 JobQueue (com.path.android.jobqueue.JobQueue)1 CustomLogger (com.path.android.jobqueue.log.CustomLogger)1 SqliteJobQueue (com.path.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1