Search in sources :

Example 11 with JobManager

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

the class DelayTest method testDelay.

public void testDelay(boolean persist) throws Exception {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    DummyJob delayedJob = new DummyJob(new Params(10).delayInMs(1000).setPersistent(persist));
    DummyJob nonDelayedJob = new DummyJob(new Params(0).setPersistent(persist));
    long jobId = jobManager.addJob(delayedJob);
    long nonDelayedJobId = jobManager.addJob(nonDelayedJob);
    Invoker<JobHolder> nextJobMethod = getNextJobMethod(jobManager);
    Invoker<Void> removeJobMethod = getRemoveJobMethod(jobManager);
    JobHolder receivedJob = nextJobMethod.invoke();
    MatcherAssert.assertThat("non-delayed job should be served", receivedJob, notNullValue());
    MatcherAssert.assertThat("non-delayed job should id should match", receivedJob.getId(), equalTo(nonDelayedJobId));
    removeJobMethod.invoke(receivedJob);
    MatcherAssert.assertThat("delayed job should not be served", nextJobMethod.invoke(), nullValue());
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    Thread.sleep(500);
    MatcherAssert.assertThat("delayed job should not be served", nextJobMethod.invoke(), nullValue());
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    Thread.sleep(2000);
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    receivedJob = nextJobMethod.invoke();
    MatcherAssert.assertThat("now should be able to receive the delayed job.", receivedJob, notNullValue());
    if (receivedJob != null) {
        MatcherAssert.assertThat("received job should be the delayed job", receivedJob.getId(), equalTo(jobId));
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager)

Example 12 with JobManager

use of com.path.android.jobqueue.JobManager in project dev-summit-architecture-demo by yigit.

the class TestUtil method prepare.

public static TestComponent prepare(App app) {
    FlowManager.destroy();
    resetSingleton(FlowManager.class, "mDatabaseHolder");
    ApplicationModule appModule = new ApplicationModule(app) {

        @Override
        public EventBus eventBus() {
            return new LoggingBus();
        }

        @Provides
        @Singleton
        public JobManager jobManager() {
            JobManager mock = mock(JobManager.class);
            when(mock.addJob(any(Job.class))).thenReturn(1L);
            return mock;
        }
    };
    TestComponent testComponent = DaggerTestComponent.builder().testApplicationModule(new TestApplicationModule()).applicationModule(appModule).build();
    testComponent.appContext().deleteDatabase(DemoDatabase.NAME + ".db");
    FlowManager.init(app);
    testComponent.feedModel().clear();
    testComponent.loggingBus().clear();
    return testComponent;
}
Also used : DaggerTestComponent(com.android.example.devsummit.archdemo.di.component.DaggerTestComponent) TestComponent(com.android.example.devsummit.archdemo.di.component.TestComponent) TestApplicationModule(com.android.example.devsummit.archdemo.di.module.TestApplicationModule) JobManager(com.path.android.jobqueue.JobManager) Job(com.path.android.jobqueue.Job) LoggingBus(com.android.example.devsummit.archdemo.event.LoggingBus) TestApplicationModule(com.android.example.devsummit.archdemo.di.module.TestApplicationModule) ApplicationModule(com.android.example.devsummit.archdemo.di.module.ApplicationModule)

Example 13 with JobManager

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

the class DelayedRunTest method testDelayedRun.

public void testDelayedRun(boolean persist, boolean tryToStop) throws Exception {
    JobManager jobManager = createJobManager();
    DummyJob delayedJob = new DummyJob(new Params(10).delayInMs(2000).setPersistent(persist));
    DummyJob nonDelayedJob = new DummyJob(new Params(0).setPersistent(persist));
    jobManager.addJob(delayedJob);
    jobManager.addJob(nonDelayedJob);
    Thread.sleep(500);
    MatcherAssert.assertThat("there should be 1 delayed job waiting to be run", jobManager.count(), equalTo(1));
    if (tryToStop) {
        // see issue #11
        jobManager.stop();
        Thread.sleep(3000);
        MatcherAssert.assertThat("there should still be 1 delayed job waiting to be run when job manager is stopped", jobManager.count(), equalTo(1));
        jobManager.start();
    }
    Thread.sleep(3000);
    MatcherAssert.assertThat("all jobs should be completed", jobManager.count(), equalTo(0));
}
Also used : DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager)

Example 14 with JobManager

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

the class GroupingTest method testGroupingRaceCondition.

@Test
public void testGroupingRaceCondition() throws Exception {
    DummyNetworkUtilWithConnectivityEventSupport dummyNetworkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
    JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).minConsumerCount(5).maxConsumerCount(10).networkUtil(dummyNetworkUtil));
    dummyNetworkUtil.setHasNetwork(false, 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);
    jobManager.addJobInBackground(new DummyJob(new Params(10).requireNetwork().groupBy(GROUP_ID)) {

        @Override
        public void onAdded() {
            super.onAdded();
            onAddedCalled.countDown();
            try {
                // wait until all other jobs are added
                remainingJobsOnAddedCalled.await();
                // wait a bit after all are added,
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }

        @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.setHasNetwork(true, true);
    // wait until all jobs are completed
    while (firstRunJob.get() == NOT_SET_JOB_ID) {
        Thread.sleep(100);
    }
    MatcherAssert.assertThat("highest priority job should run if it is added before others", firstRunJob.get(), is(FIRST_JOB_ID));
}
Also used : DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 15 with JobManager

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

Aggregations

JobManager (com.path.android.jobqueue.JobManager)27 Test (org.junit.Test)21 Params (com.path.android.jobqueue.Params)20 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)18 CountDownLatch (java.util.concurrent.CountDownLatch)8 JobHolder (com.path.android.jobqueue.JobHolder)6 Configuration (com.path.android.jobqueue.config.Configuration)6 BaseJob (com.path.android.jobqueue.BaseJob)4 Job (com.path.android.jobqueue.Job)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 DependencyInjector (com.path.android.jobqueue.di.DependencyInjector)2 CustomLogger (com.path.android.jobqueue.log.CustomLogger)2 ArrayList (java.util.ArrayList)2 DaggerTestComponent (com.android.example.devsummit.archdemo.di.component.DaggerTestComponent)1 TestComponent (com.android.example.devsummit.archdemo.di.component.TestComponent)1 ApplicationModule (com.android.example.devsummit.archdemo.di.module.ApplicationModule)1 TestApplicationModule (com.android.example.devsummit.archdemo.di.module.TestApplicationModule)1 LoggingBus (com.android.example.devsummit.archdemo.event.LoggingBus)1 AsyncAddCallback (com.path.android.jobqueue.AsyncAddCallback)1 JobQueue (com.path.android.jobqueue.JobQueue)1