Search in sources :

Example 16 with DummyJob

use of com.path.android.jobqueue.test.jobs.DummyJob 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 17 with DummyJob

use of com.path.android.jobqueue.test.jobs.DummyJob 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 18 with DummyJob

use of com.path.android.jobqueue.test.jobs.DummyJob 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 19 with DummyJob

use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.

the class AddedCountTest method testAddedCount.

@Test
public void testAddedCount() throws Exception {
    testAddedCount(new DummyJob(new Params(0)));
    testAddedCount(new DummyJob(new Params(0).persist()));
}
Also used : DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Params(com.path.android.jobqueue.Params) Test(org.junit.Test)

Example 20 with DummyJob

use of com.path.android.jobqueue.test.jobs.DummyJob 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

Params (com.path.android.jobqueue.Params)22 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)22 Test (org.junit.Test)19 JobManager (com.path.android.jobqueue.JobManager)18 JobHolder (com.path.android.jobqueue.JobHolder)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Configuration (com.path.android.jobqueue.config.Configuration)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BaseJob (com.path.android.jobqueue.BaseJob)3 Job (com.path.android.jobqueue.Job)3 ArrayList (java.util.ArrayList)3 JobQueue (com.path.android.jobqueue.JobQueue)2 DependencyInjector (com.path.android.jobqueue.di.DependencyInjector)2 CustomLogger (com.path.android.jobqueue.log.CustomLogger)2 Semaphore (java.util.concurrent.Semaphore)2 AsyncAddCallback (com.path.android.jobqueue.AsyncAddCallback)1 JobStatus (com.path.android.jobqueue.JobStatus)1 JobConsumerExecutor (com.path.android.jobqueue.executor.JobConsumerExecutor)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1