Search in sources :

Example 1 with JobConsumerExecutor

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

the class LoadFactorTest method testLoadFactor.

@Test
public void testLoadFactor() throws Exception {
    // test adding zillions of jobs from the same group and ensure no more than 1 thread is created
    int maxConsumerCount = 5;
    int minConsumerCount = 2;
    int loadFactor = 5;
    com.path.android.jobqueue.JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).maxConsumerCount(maxConsumerCount).minConsumerCount(minConsumerCount).customLogger(new CustomLogger() {

        public boolean isDebugEnabled() {
            return true;
        }

        public void d(String text, Object... args) {
            System.out.println(String.format(text, args));
        }

        public void e(Throwable t, String text, Object... args) {
            t.printStackTrace();
            System.out.println(String.format(text, args));
        }

        public void e(String text, Object... args) {
            System.out.println(String.format(text, args));
        }
    }).loadFactor(loadFactor));
    JobConsumerExecutor consumerExecutor = getConsumerExecutor(jobManager);
    org.fest.reflect.field.Invoker<AtomicInteger> activeConsumerCnt = getActiveConsumerCount(consumerExecutor);
    Object runLock = new Object();
    Semaphore semaphore = new Semaphore(maxConsumerCount);
    int totalJobCount = loadFactor * maxConsumerCount * 5;
    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);
        int expectedConsumerCount = Math.min(maxConsumerCount, (int) Math.ceil((float) (i + 1) / loadFactor));
        if (i >= minConsumerCount) {
            expectedConsumerCount = Math.max(minConsumerCount, expectedConsumerCount);
        }
        // wait till enough jobs start
        long now = System.nanoTime();
        long waitTill = now + TimeUnit.SECONDS.toNanos(10);
        while (System.nanoTime() < waitTill) {
            if (semaphore.availablePermits() == maxConsumerCount - expectedConsumerCount) {
                // enough # of jobs started
                break;
            }
        }
        if (i < loadFactor) {
            // make sure there is only min job running
            MatcherAssert.assertThat("while below load factor, active consumer count should be = min", activeConsumerCnt.get().get(), equalTo(Math.min(i + 1, minConsumerCount)));
        }
        if (i > loadFactor) {
            // make sure there is only 1 job running
            MatcherAssert.assertThat("while above load factor. there should be more job consumers. i=" + i, activeConsumerCnt.get().get(), equalTo(expectedConsumerCount));
        }
    }
    // finish all jobs
    long now = System.nanoTime();
    long waitTill = now + TimeUnit.SECONDS.toNanos(10);
    while (System.nanoTime() < waitTill) {
        synchronized (runLock) {
            runLock.notifyAll();
        }
        long 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 : JobConsumerExecutor(com.path.android.jobqueue.executor.JobConsumerExecutor) Configuration(com.path.android.jobqueue.config.Configuration) ArrayList(java.util.ArrayList) Params(com.path.android.jobqueue.Params) CustomLogger(com.path.android.jobqueue.log.CustomLogger) Semaphore(java.util.concurrent.Semaphore) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

Params (com.path.android.jobqueue.Params)1 Configuration (com.path.android.jobqueue.config.Configuration)1 JobConsumerExecutor (com.path.android.jobqueue.executor.JobConsumerExecutor)1 CustomLogger (com.path.android.jobqueue.log.CustomLogger)1 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)1 ArrayList (java.util.ArrayList)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1