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));
}
});
}
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));
}
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));
}
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);
}
Aggregations