Search in sources :

Example 21 with JobManager

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

the class KeepAliveTest method testKeepAlive.

@Test
public void testKeepAlive() throws Exception {
    int keepAlive = 3 + (int) (Math.random() * 5);
    DummyNetworkUtil networkUtilWithoutEventSupport = new DummyNetworkUtil();
    DummyNetworkUtilWithConnectivityEventSupport networkUtilWithEventSupport = new DummyNetworkUtilWithConnectivityEventSupport();
    JobManager jobManager1 = createJobManager(new Configuration.Builder(Robolectric.application).consumerKeepAlive(keepAlive).networkUtil(networkUtilWithoutEventSupport));
    JobManager jobManager2 = createJobManager(new Configuration.Builder(Robolectric.application).consumerKeepAlive(keepAlive).networkUtil(networkUtilWithEventSupport));
    // give it a little time to create first consumer
    jobManager1.addJob(new DummyJob(new Params(0)));
    jobManager2.addJob(new DummyJob(new Params(0)));
    AtomicInteger activeThreadCount1 = getActiveConsumerCount(getConsumerExecutor(jobManager1)).get();
    AtomicInteger activeThreadCount2 = getActiveConsumerCount(getConsumerExecutor(jobManager2)).get();
    Thread.sleep(1000);
    MatcherAssert.assertThat("there should be 1 thread  actively waiting for jobs", activeThreadCount1.get(), equalTo(1));
    MatcherAssert.assertThat("there should be one thread actively waiting for jobs", activeThreadCount2.get(), equalTo(1));
    // sleep till it dies
    Thread.sleep((long) (TimeUnit.SECONDS.toMillis(keepAlive) * 1.33));
    MatcherAssert.assertThat("after keep alive timeout, there should NOT be any threads waiting", activeThreadCount1.get(), equalTo(0));
    MatcherAssert.assertThat("after keep alive timeout, there should NOT be any threads waiting", activeThreadCount2.get(), equalTo(0));
    // disable network and add a network bound job
    networkUtilWithoutEventSupport.setHasNetwork(false);
    networkUtilWithEventSupport.setHasNetwork(false, true);
    jobManager1.addJob(new DummyJob(new Params(0).requireNetwork()));
    jobManager2.addJob(new DummyJob(new Params(0).requireNetwork()));
    Thread.sleep(1000 + (long) (TimeUnit.SECONDS.toMillis(keepAlive) * 2));
    MatcherAssert.assertThat("when network changes cannot be detected, there should be a consumer waiting alive", activeThreadCount1.get(), equalTo(1));
    MatcherAssert.assertThat("when network changes can be detected, there should not be a consumer waiting alive", activeThreadCount2.get(), equalTo(0));
    networkUtilWithEventSupport.setHasNetwork(true, true);
    networkUtilWithoutEventSupport.setHasNetwork(true);
    Thread.sleep(500);
    MatcherAssert.assertThat("when network is recovered, job should be handled", jobManager2.count(), equalTo(0));
    Thread.sleep(1000);
    MatcherAssert.assertThat("when network is recovered, job should be handled", jobManager1.count(), equalTo(0));
}
Also used : Configuration(com.path.android.jobqueue.config.Configuration) 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) Test(org.junit.Test)

Example 22 with JobManager

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

the class NetworkJobTest method testNetworkJob.

@Test
public void testNetworkJob() throws Exception {
    JobManagerTestBase.DummyNetworkUtil dummyNetworkUtil = new JobManagerTestBase.DummyNetworkUtil();
    JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).networkUtil(dummyNetworkUtil));
    jobManager.stop();
    DummyJob networkDummyJob = new DummyJob(new Params(5).requireNetwork());
    jobManager.addJob(networkDummyJob);
    DummyJob noNetworkDummyJob = new DummyJob(new Params(2));
    jobManager.addJob(noNetworkDummyJob);
    DummyJob networkPersistentJob = new DummyJob(new Params(6).persist().requireNetwork());
    jobManager.addJob(networkPersistentJob);
    DummyJob noNetworkPersistentJob = new DummyJob(new Params(1).persist());
    jobManager.addJob(noNetworkPersistentJob);
    MatcherAssert.assertThat("count should be correct if there are network and non-network jobs w/o network", jobManager.count(), equalTo(4));
    dummyNetworkUtil.setHasNetwork(true);
    MatcherAssert.assertThat("count should be correct if there is network and non-network jobs w/o network", jobManager.count(), equalTo(4));
    dummyNetworkUtil.setHasNetwork(false);
    jobManager.start();
    // this should be enough to consume dummy jobs
    Thread.sleep(1000);
    MatcherAssert.assertThat("no network jobs should be executed even if there is no network", jobManager.count(), equalTo(2));
    dummyNetworkUtil.setHasNetwork(true);
    // this should be enough to consume dummy jobs
    Thread.sleep(1000);
    MatcherAssert.assertThat("when network is recovered, all network jobs should be automatically consumed", 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) Test(org.junit.Test)

Example 23 with JobManager

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

the class NetworkNextJobTest method testNetworkNextJob.

@Test
public void testNetworkNextJob() throws Exception {
    DummyNetworkUtil dummyNetworkUtil = new DummyNetworkUtil();
    JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).networkUtil(dummyNetworkUtil));
    jobManager.stop();
    DummyJob dummyJob = new DummyJob(new Params(0).requireNetwork());
    long dummyJobId = jobManager.addJob(dummyJob);
    dummyNetworkUtil.setHasNetwork(false);
    Invoker<JobHolder> nextJobMethod = getNextJobMethod(jobManager);
    MatcherAssert.assertThat("when there isn't any network, next job should return null", nextJobMethod.invoke(), nullValue());
    MatcherAssert.assertThat("even if there is network, job manager should return correct count", jobManager.count(), equalTo(1));
    dummyNetworkUtil.setHasNetwork(true);
    JobHolder retrieved = nextJobMethod.invoke();
    MatcherAssert.assertThat("when network is recovered, next job should be retrieved", retrieved, notNullValue());
}
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) Test(org.junit.Test)

Example 24 with JobManager

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

the class AddedCountTest method testAddedCount.

private void testAddedCount(DummyJob dummyJob) {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    jobManager.addJob(dummyJob);
    MatcherAssert.assertThat(1, equalTo(dummyJob.getOnAddedCnt()));
}
Also used : JobManager(com.path.android.jobqueue.JobManager)

Example 25 with JobManager

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

the class ClearTest method testClear.

@Test
public void testClear() throws Exception {
    JobManager jobManager = createJobManager();
    final int LIMIT = 20;
    for (int i = 0; i < LIMIT; i++) {
        jobManager.addJob(new DummyJob(new Params(0).setPersistent(i % 2 == 1)));
    }
    jobManager.clear();
    MatcherAssert.assertThat("after clear, count should be 0", 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) 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