use of com.path.android.jobqueue.JobManager in project android-priority-jobqueue by path.
the class JobStatusTest method testJobStatus.
@Test
public void testJobStatus() throws InterruptedException {
DummyNetworkUtilWithConnectivityEventSupport networkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
networkUtil.setHasNetwork(false, true);
JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).networkUtil(networkUtil));
jobManager.stop();
List<Integer> networkRequiringJobIndices = new ArrayList<Integer>();
Job[] jobs = new Job[] { new DummyJob(new Params(0)), new DummyJob(new Params(0).persist()), new DummyJob(new Params(0).persist().requireNetwork()) };
long[] ids = new long[jobs.length];
for (int i = 0; i < jobs.length; i++) {
ids[i] = jobManager.addJob(jobs[i]);
if (jobs[i].requiresNetwork()) {
networkRequiringJobIndices.add(i);
}
JobStatus expectedStatus = (networkUtil.isConnected() || jobs[i].requiresNetwork() == false) ? JobStatus.WAITING_READY : JobStatus.WAITING_NOT_READY;
assertThat("job should have correct status after being added", jobManager.getJobStatus(ids[i], jobs[i].isPersistent()), is(expectedStatus));
}
// create an unknown id, ensure status for that
boolean exists;
long unknownId;
do {
unknownId = (long) (Math.random() * 10000 - 5000);
exists = false;
for (long id : ids) {
if (id == unknownId) {
exists = true;
continue;
}
}
} while (exists);
for (boolean persistent : new boolean[] { true, false }) {
assertThat("job with unknown id should return as expected", jobManager.getJobStatus(unknownId, persistent), is(JobStatus.UNKNOWN));
}
CountDownLatch startLatch = new CountDownLatch(1), endLatch = new CountDownLatch(1);
DummyTwoLatchJob twoLatchJob = new DummyTwoLatchJob(new Params(0), startLatch, endLatch);
jobManager.start();
long jobId = jobManager.addJob(twoLatchJob);
twoLatchJob.waitTillOnRun();
assertThat("job should be in running state", jobManager.getJobStatus(jobId, false), is(JobStatus.RUNNING));
// let it run
startLatch.countDown();
// wait till it finishes
endLatch.await();
// give some time to job manager to clear the job
Thread.sleep(500);
assertThat("finished job should go to unknown state", jobManager.getJobStatus(jobId, false), is(JobStatus.UNKNOWN));
// network requiring job should not be ready
for (Integer i : networkRequiringJobIndices) {
assertThat("network requiring job should still be not-ready", jobManager.getJobStatus(ids[i], jobs[i].isPersistent()), is(JobStatus.WAITING_NOT_READY));
}
jobManager.stop();
networkUtil.setHasNetwork(true, true);
for (Integer i : networkRequiringJobIndices) {
assertThat("network requiring job should still be ready after network is there", jobManager.getJobStatus(ids[i], jobs[i].isPersistent()), is(JobStatus.WAITING_READY));
}
jobManager.start();
int limit = 10;
while (jobManager.count() > 0 && limit-- > 0) {
Thread.sleep(1000);
}
assertThat("jobs should finish", jobManager.count(), is(0));
for (int i = 0; i < jobs.length; i++) {
// after all jobs finish, state should be unknown
assertThat("all jobs finished, states should be unknown", jobManager.getJobStatus(ids[i], jobs[i].isPersistent()), is(JobStatus.UNKNOWN));
}
final long SHORT_SLEEP = 1000;
Job[] delayedJobs = new Job[] { new DummyJob(new Params(0).delayInMs(SHORT_SLEEP)), new DummyJob(new Params(0).delayInMs(SHORT_SLEEP).persist()), new DummyJob(new Params(0).delayInMs(SHORT_SLEEP * 10)), new DummyJob(new Params(0).delayInMs(SHORT_SLEEP * 10).persist()) };
long[] delayedIds = new long[delayedJobs.length];
for (int i = 0; i < delayedJobs.length; i++) {
delayedIds[i] = jobManager.addJob(delayedJobs[i]);
}
for (int i = 0; i < delayedJobs.length; i++) {
assertThat("delayed job(" + i + ") should receive not ready status", jobManager.getJobStatus(delayedIds[i], delayedJobs[i].isPersistent()), is(JobStatus.WAITING_NOT_READY));
}
jobManager.stop();
// sleep
Thread.sleep(SHORT_SLEEP * 2);
for (int i = 0; i < delayedJobs.length; i++) {
if (delayedJobs[i].getDelayInMs() == SHORT_SLEEP) {
assertThat("when enough time passes, delayed jobs should move to ready state", jobManager.getJobStatus(delayedIds[i], delayedJobs[i].isPersistent()), is(JobStatus.WAITING_READY));
} else {
assertThat("delayed job should receive not ready status until their time comes", jobManager.getJobStatus(delayedIds[i], delayedJobs[i].isPersistent()), is(JobStatus.WAITING_NOT_READY));
}
}
}
use of com.path.android.jobqueue.JobManager 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.JobManager 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.JobManager 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));
}
use of com.path.android.jobqueue.JobManager 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