Search in sources :

Example 1 with CustomLogger

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

the class TestBase method enableDebug.

private void enableDebug() {
    ShadowLog.stream = System.out;
    JqLog.setCustomLogger(new CustomLogger() {

        private String TAG = "test_logger";

        @Override
        public boolean isDebugEnabled() {
            return true;
        }

        @Override
        public void d(String text, Object... args) {
            Log.d(TAG, String.format(text, args));
        }

        @Override
        public void e(Throwable t, String text, Object... args) {
            Log.e(TAG, String.format(text, args), t);
        }

        @Override
        public void e(String text, Object... args) {
            Log.e(TAG, String.format(text, args));
        }
    });
}
Also used : CustomLogger(com.path.android.jobqueue.log.CustomLogger)

Example 2 with CustomLogger

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

Example 3 with CustomLogger

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

Example 4 with CustomLogger

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

the class TwitterApplication method configureJobManager.

private void configureJobManager() {
    Configuration configuration = new Configuration.Builder(this).customLogger(new CustomLogger() {

        private static final String TAG = "JOBS";

        @Override
        public boolean isDebugEnabled() {
            return true;
        }

        @Override
        public void d(String text, Object... args) {
            Log.d(TAG, String.format(text, args));
        }

        @Override
        public void e(Throwable t, String text, Object... args) {
            Log.e(TAG, String.format(text, args), t);
        }

        @Override
        public void e(String text, Object... args) {
            Log.e(TAG, String.format(text, args));
        }
    }).minConsumerCount(// always keep at least one consumer alive
    1).maxConsumerCount(// up to 3 consumers at a time
    3).loadFactor(// 3 jobs per consumer
    3).consumerKeepAlive(// wait 2 minute
    120).build();
    jobManager = new JobManager(this, configuration);
}
Also used : Configuration(com.path.android.jobqueue.config.Configuration) CustomLogger(com.path.android.jobqueue.log.CustomLogger) JobManager(com.path.android.jobqueue.JobManager)

Aggregations

CustomLogger (com.path.android.jobqueue.log.CustomLogger)4 Configuration (com.path.android.jobqueue.config.Configuration)3 JobManager (com.path.android.jobqueue.JobManager)2 Params (com.path.android.jobqueue.Params)2 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)2 Test (org.junit.Test)2 BaseJob (com.path.android.jobqueue.BaseJob)1 DependencyInjector (com.path.android.jobqueue.di.DependencyInjector)1 JobConsumerExecutor (com.path.android.jobqueue.executor.JobConsumerExecutor)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1