Search in sources :

Example 21 with Params

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

the class JobQueueTestBase method testFindJobHolderById.

@Test
public void testFindJobHolderById() {
    JobQueue jobQueue = createNewJobQueue();
    assertJob(jobQueue, "non existing job (negative id)", -4, null);
    assertJob(jobQueue, "non existing job (positive id)", +4, null);
    final int LIMIT = 100;
    JobHolder[] holders = new JobHolder[LIMIT];
    long[] ids = new long[LIMIT];
    for (int i = 0; i < LIMIT; i++) {
        holders[i] = createNewJobHolder(new Params((int) (Math.random() * 50)).setPersistent(Math.random() < .5).setRequiresNetwork(Math.random() < .5));
        ids[i] = jobQueue.insert(holders[i]);
        assertJob(jobQueue, "job by id should work for inserted job", ids[i], holders[i]);
    }
    final int REMOVE_CNT = LIMIT / 2;
    for (int i = 0; i < REMOVE_CNT; i++) {
        int ind = (int) (Math.random() * LIMIT);
        if (holders[ind] == null) {
            continue;
        }
        // remove some randomly, up to half
        jobQueue.remove(holders[ind]);
        holders[ind] = null;
    }
    // re-query all, ensure we can still find non-removed jobs and not find removed jobs
    for (int i = 0; i < LIMIT; i++) {
        if (holders[i] != null) {
            assertJob(jobQueue, "if job is still in the Q, it should be returned", ids[i], holders[i]);
            // re add job
            jobQueue.insertOrReplace(holders[i]);
            // re-test after re-add
            assertJob(jobQueue, "after re-insert, if job is still in the Q, it should be returned", ids[i], holders[i]);
        } else {
            assertJob(jobQueue, "removed job should not be returned in id query", ids[i], null);
        }
    }
    jobQueue.clear();
    for (int i = 0; i < LIMIT; i++) {
        assertJob(jobQueue, "after clear, find by id should return null", ids[i], null);
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) JobQueue(com.path.android.jobqueue.JobQueue) Params(com.path.android.jobqueue.Params) Test(org.junit.Test)

Example 22 with Params

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

the class NonPersistentJobQueueTest method testTooManyQueueChanges.

/**
 * issue #21 https://github.com/path/android-priority-jobqueue/issues/21
 */
@Test
public void testTooManyQueueChanges() throws InterruptedException {
    JobQueue jobQueue = createNewJobQueue();
    int limit = 10000;
    long delayMs = 2000;
    long then = System.nanoTime() + delayMs * JobManager.NS_PER_MS;
    for (int i = 0; i < limit; i++) {
        jobQueue.insert(createNewJobHolder(new Params(0).requireNetwork().delayInMs(delayMs)));
    }
    MatcherAssert.assertThat("all jobs require network, should return null", jobQueue.nextJobAndIncRunCount(false, null), nullValue());
    long sleep = then - System.nanoTime();
    sleep += JobManager.NS_PER_MS * 1000;
    if (sleep > 0) {
        Thread.sleep(sleep / JobManager.NS_PER_MS);
    }
    // should be able to get it w/o an overflow
    for (int i = 0; i < limit; i++) {
        JobHolder holder = jobQueue.nextJobAndIncRunCount(true, null);
        MatcherAssert.assertThat("should get a next job", holder, notNullValue());
        jobQueue.remove(holder);
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) JobQueue(com.path.android.jobqueue.JobQueue) Params(com.path.android.jobqueue.Params) Test(org.junit.Test)

Example 23 with Params

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

the class ConsumerCountTest method testMaxConsumerCount.

@Test
public void testMaxConsumerCount() throws Exception {
    int maxConsumerCount = 2;
    JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).maxConsumerCount(maxConsumerCount).loadFactor(maxConsumerCount));
    Object runLock = new Object();
    Semaphore semaphore = new Semaphore(maxConsumerCount);
    int totalJobCount = maxConsumerCount * 3;
    List<DummyJob> runningJobs = new ArrayList<DummyJob>(totalJobCount);
    for (int i = 0; i < totalJobCount; i++) {
        DummyJob job = new NeverEndingDummyJob(new Params((int) (Math.random() * 3)), runLock, semaphore);
        runningJobs.add(job);
        jobManager.addJob(job);
    }
    // wait till enough jobs start
    long now = System.nanoTime();
    long waitTill = now + TimeUnit.SECONDS.toNanos(10);
    while (System.nanoTime() < waitTill) {
        if (semaphore.availablePermits() == 0) {
            // enough # of jobs started
            break;
        }
    }
    // wait some more to ensure no more jobs are started
    Thread.sleep(TimeUnit.SECONDS.toMillis(3));
    int totalRunningCount = 0;
    for (DummyJob job : runningJobs) {
        totalRunningCount += job.getOnRunCnt();
    }
    MatcherAssert.assertThat("only maxConsumerCount jobs should start", totalRunningCount, equalTo(maxConsumerCount));
    // try to finish all jobs
    // wait till enough jobs start
    now = System.nanoTime();
    waitTill = now + TimeUnit.SECONDS.toNanos(10);
    while (System.nanoTime() < waitTill) {
        synchronized (runLock) {
            runLock.notifyAll();
        }
        totalRunningCount = 0;
        for (DummyJob job : runningJobs) {
            totalRunningCount += job.getOnRunCnt();
        }
        if (totalJobCount == totalRunningCount) {
            // cool!
            break;
        }
    }
    MatcherAssert.assertThat("no jobs should remain", jobManager.count(), equalTo(0));
}
Also used : Configuration(com.path.android.jobqueue.config.Configuration) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) ArrayList(java.util.ArrayList) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 24 with Params

use of com.path.android.jobqueue.Params 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 25 with Params

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

Aggregations

Params (com.path.android.jobqueue.Params)36 Test (org.junit.Test)33 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)22 JobManager (com.path.android.jobqueue.JobManager)20 JobHolder (com.path.android.jobqueue.JobHolder)17 JobQueue (com.path.android.jobqueue.JobQueue)13 CountDownLatch (java.util.concurrent.CountDownLatch)8 Configuration (com.path.android.jobqueue.config.Configuration)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 BaseJob (com.path.android.jobqueue.BaseJob)4 Job (com.path.android.jobqueue.Job)3 ArrayList (java.util.ArrayList)3 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 SqliteJobQueue (com.path.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue)1 LinkedList (java.util.LinkedList)1