use of com.path.android.jobqueue.Params 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.Params in project android-priority-jobqueue by path.
the class MultiThreadTest method testMultiThreaded.
@Test
public void testMultiThreaded() throws Exception {
multiThreadedJobCounter = new AtomicInteger(0);
final JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).loadFactor(3).maxConsumerCount(10));
int limit = 200;
ExecutorService executor = new ThreadPoolExecutor(20, 20, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(limit));
Collection<Future<?>> futures = new LinkedList<Future<?>>();
for (int i = 0; i < limit; i++) {
final int id = i;
futures.add(executor.submit(new Runnable() {
@Override
public void run() {
final boolean persistent = Math.round(Math.random()) % 2 == 0;
boolean requiresNetwork = Math.round(Math.random()) % 2 == 0;
int priority = (int) (Math.round(Math.random()) % 10);
multiThreadedJobCounter.incrementAndGet();
jobManager.addJob(new DummyJobForMultiThread(id, new Params(priority).setRequiresNetwork(requiresNetwork).setPersistent(persistent)));
}
}));
}
for (Future<?> future : futures) {
future.get();
}
Log.d("TAG", "added all jobs");
// wait until all jobs are added
long start = System.nanoTime();
// 20 seconds
long timeLimit = JobManager.NS_PER_MS * 20000;
while (System.nanoTime() - start < timeLimit && multiThreadedJobCounter.get() != 0) {
Thread.sleep(1000);
}
Log.d("TAG", "did we reach timeout? " + (System.nanoTime() - start >= timeLimit));
MatcherAssert.assertThat("jobmanager count should be 0", jobManager.count(), equalTo(0));
MatcherAssert.assertThat("multiThreadedJobCounter should be 0", multiThreadedJobCounter.get(), equalTo(0));
}
use of com.path.android.jobqueue.Params in project android-priority-jobqueue by path.
the class NetworkJobWithConnectivityListenerTest method testNetworkJobWithConnectivityListener.
@Test
public void testNetworkJobWithConnectivityListener() throws Exception {
DummyNetworkUtilWithConnectivityEventSupport dummyNetworkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).networkUtil(dummyNetworkUtil));
dummyNetworkUtil.setHasNetwork(false, true);
DummyJob dummyJob = new DummyJob(new Params(0).requireNetwork());
long dummyJobId = jobManager.addJob(dummyJob);
// sleep a while so that consumers die. they should die since we are using a network util
Thread.sleep(2000);
// with event support
MatcherAssert.assertThat("count should be 1 as no jobs should be consumed w/o network", jobManager.count(), equalTo(1));
dummyNetworkUtil.setHasNetwork(true, false);
// wait a little bit more to consumer will run
Thread.sleep(1000);
MatcherAssert.assertThat("even though network is recovered, job manager should not consume any job because it " + "does not know (we did not inform)", jobManager.count(), equalTo(1));
dummyNetworkUtil.setHasNetwork(true, true);
// wait a little bit more to consumer will run
Thread.sleep(1000);
MatcherAssert.assertThat("job manager should consume network job after it is informed that network is recovered", jobManager.count(), equalTo(0));
}
use of com.path.android.jobqueue.Params in project android-priority-jobqueue by path.
the class JobQueueTestBase method testGroupId.
@Test
public void testGroupId() throws Exception {
JobQueue jobQueue = createNewJobQueue();
long jobId1 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group1")));
long jobId2 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group1")));
long jobId3 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group2")));
long jobId4 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group2")));
long jobId5 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group1")));
JobHolder holder1 = jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group2" }));
assertThat("first jobs should be from group group2 if group1 is excluded", holder1.getBaseJob().getRunGroupId(), equalTo("group1"));
assertThat("correct job should be returned if groupId is provided", holder1.getId(), equalTo(jobId1));
assertThat("no jobs should be returned if all groups are excluded", jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1", "group2" })), is(nullValue()));
long jobId6 = jobQueue.insert(createNewJobHolder(new Params(0)));
assertThat("both groups are disabled, null group job should be returned", jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1", "group2" })).getId(), is(jobId6));
assertThat("if group1 is excluded, next job should be from group2", jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1" })).getBaseJob().getRunGroupId(), equalTo("group2"));
// to test re-run case, add the job back in
jobQueue.insertOrReplace(holder1);
// ask for it again, should return the same holder because it is grouped
JobHolder holder2 = jobQueue.nextJobAndIncRunCount(true, null);
assertThat("for grouped jobs, re-fetching job should work fine", holder2.getId(), equalTo(holder1.getId()));
JobHolder holder3 = jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1" }));
assertThat("if a group it excluded, next available from another group should be returned", holder3.getId(), equalTo(jobId4));
// add two more non-grouped jobs
long jobId7 = jobQueue.insert(createNewJobHolder(new Params(0)));
long jobId8 = jobQueue.insert(createNewJobHolder(new Params(0)));
JobHolder holder4 = jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1", "group2" }));
assertThat("if all grouped jobs are excluded, non-grouped jobs should be returned", holder4.getId(), equalTo(jobId7));
jobQueue.insertOrReplace(holder4);
// for non-grouped jobs, run counts should be respected
assertThat("if all grouped jobs are excluded, re-inserted highest priority job should still be returned", jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1", "group2" })).getId(), equalTo(jobId7));
}
use of com.path.android.jobqueue.Params in project android-priority-jobqueue by path.
the class JobQueueTestBase method testNetwork.
@Test
public void testNetwork() throws Exception {
JobQueue jobQueue = createNewJobQueue();
JobHolder jobHolder = createNewJobHolder(new Params(0));
jobQueue.insert(jobHolder);
assertThat("no network job should be returned even if there is no netowrk", jobQueue.nextJobAndIncRunCount(false, null), notNullValue());
jobQueue.remove(jobHolder);
jobHolder = createNewJobHolder(new Params(0).requireNetwork());
assertThat("if there isn't any network, job with network requirement should not return", jobQueue.nextJobAndIncRunCount(false, null), nullValue());
assertThat("if there is network, job with network requirement should be returned", jobQueue.nextJobAndIncRunCount(true, null), nullValue());
jobQueue.remove(jobHolder);
jobHolder = createNewJobHolder(new Params(1));
JobHolder jobHolder2 = createNewJobHolder(new Params(5).requireNetwork());
long firstJobId = jobQueue.insert(jobHolder);
long secondJobId = jobQueue.insert(jobHolder2);
JobHolder retrieved = jobQueue.nextJobAndIncRunCount(false, null);
assertThat("one job should be returned w/o network", retrieved, notNullValue());
if (retrieved != null) {
assertThat("no network job should be returned although it has lower priority", retrieved.getId(), equalTo(firstJobId));
}
assertThat("no other job should be returned w/o network", jobQueue.nextJobAndIncRunCount(false, null), nullValue());
retrieved = jobQueue.nextJobAndIncRunCount(true, null);
assertThat("if network is back, network requiring job should be returned", retrieved, notNullValue());
if (retrieved != null) {
assertThat("when there is network, network job should be returned", retrieved.getId(), equalTo(secondJobId));
}
// add first job back
jobQueue.insertOrReplace(jobHolder);
// add second job back
jobQueue.insertOrReplace(jobHolder2);
retrieved = jobQueue.nextJobAndIncRunCount(true, null);
assertThat("if network is back, job w/ higher priority should be returned", retrieved, notNullValue());
if (retrieved != null) {
assertThat("if network is back, job w/ higher priority should be returned", retrieved.getId(), equalTo(secondJobId));
}
jobQueue.insertOrReplace(jobHolder2);
JobHolder highestPriorityJob = createNewJobHolder(new Params(10));
long highestPriorityJobId = jobQueue.insert(highestPriorityJob);
retrieved = jobQueue.nextJobAndIncRunCount(true, null);
assertThat("w/ or w/o network, highest priority should be returned", retrieved, notNullValue());
if (retrieved != null) {
assertThat("w/ or w/o network, highest priority should be returned", retrieved.getId(), equalTo(highestPriorityJobId));
}
// TODO test delay until
}
Aggregations