use of com.path.android.jobqueue.Params in project android-priority-jobqueue by path.
the class JobQueueTestBase method testFindJobHolderById.
@Test
public void testFindJobHolderById() {
JobQueue jobQueue = createNewJobQueue();
assertJob(jobQueue, "non existing job (negative id)", -4, null);
assertJob(jobQueue, "non existing job (positive id)", +4, null);
final int LIMIT = 100;
JobHolder[] holders = new JobHolder[LIMIT];
long[] ids = new long[LIMIT];
for (int i = 0; i < LIMIT; i++) {
holders[i] = createNewJobHolder(new Params((int) (Math.random() * 50)).setPersistent(Math.random() < .5).setRequiresNetwork(Math.random() < .5));
ids[i] = jobQueue.insert(holders[i]);
assertJob(jobQueue, "job by id should work for inserted job", ids[i], holders[i]);
}
final int REMOVE_CNT = LIMIT / 2;
for (int i = 0; i < REMOVE_CNT; i++) {
int ind = (int) (Math.random() * LIMIT);
if (holders[ind] == null) {
continue;
}
// remove some randomly, up to half
jobQueue.remove(holders[ind]);
holders[ind] = null;
}
// re-query all, ensure we can still find non-removed jobs and not find removed jobs
for (int i = 0; i < LIMIT; i++) {
if (holders[i] != null) {
assertJob(jobQueue, "if job is still in the Q, it should be returned", ids[i], holders[i]);
// re add job
jobQueue.insertOrReplace(holders[i]);
// re-test after re-add
assertJob(jobQueue, "after re-insert, if job is still in the Q, it should be returned", ids[i], holders[i]);
} else {
assertJob(jobQueue, "removed job should not be returned in id query", ids[i], null);
}
}
jobQueue.clear();
for (int i = 0; i < LIMIT; i++) {
assertJob(jobQueue, "after clear, find by id should return null", ids[i], null);
}
}
use of com.path.android.jobqueue.Params in project android-priority-jobqueue by path.
the class NonPersistentJobQueueTest method testTooManyQueueChanges.
/**
* issue #21 https://github.com/path/android-priority-jobqueue/issues/21
*/
@Test
public void testTooManyQueueChanges() throws InterruptedException {
JobQueue jobQueue = createNewJobQueue();
int limit = 10000;
long delayMs = 2000;
long then = System.nanoTime() + delayMs * JobManager.NS_PER_MS;
for (int i = 0; i < limit; i++) {
jobQueue.insert(createNewJobHolder(new Params(0).requireNetwork().delayInMs(delayMs)));
}
MatcherAssert.assertThat("all jobs require network, should return null", jobQueue.nextJobAndIncRunCount(false, null), nullValue());
long sleep = then - System.nanoTime();
sleep += JobManager.NS_PER_MS * 1000;
if (sleep > 0) {
Thread.sleep(sleep / JobManager.NS_PER_MS);
}
// should be able to get it w/o an overflow
for (int i = 0; i < limit; i++) {
JobHolder holder = jobQueue.nextJobAndIncRunCount(true, null);
MatcherAssert.assertThat("should get a next job", holder, notNullValue());
jobQueue.remove(holder);
}
}
use of com.path.android.jobqueue.Params 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.Params in project android-priority-jobqueue by path.
the class KeepAliveTest method testKeepAlive.
@Test
public void testKeepAlive() throws Exception {
int keepAlive = 3 + (int) (Math.random() * 5);
DummyNetworkUtil networkUtilWithoutEventSupport = new DummyNetworkUtil();
DummyNetworkUtilWithConnectivityEventSupport networkUtilWithEventSupport = new DummyNetworkUtilWithConnectivityEventSupport();
JobManager jobManager1 = createJobManager(new Configuration.Builder(Robolectric.application).consumerKeepAlive(keepAlive).networkUtil(networkUtilWithoutEventSupport));
JobManager jobManager2 = createJobManager(new Configuration.Builder(Robolectric.application).consumerKeepAlive(keepAlive).networkUtil(networkUtilWithEventSupport));
// give it a little time to create first consumer
jobManager1.addJob(new DummyJob(new Params(0)));
jobManager2.addJob(new DummyJob(new Params(0)));
AtomicInteger activeThreadCount1 = getActiveConsumerCount(getConsumerExecutor(jobManager1)).get();
AtomicInteger activeThreadCount2 = getActiveConsumerCount(getConsumerExecutor(jobManager2)).get();
Thread.sleep(1000);
MatcherAssert.assertThat("there should be 1 thread actively waiting for jobs", activeThreadCount1.get(), equalTo(1));
MatcherAssert.assertThat("there should be one thread actively waiting for jobs", activeThreadCount2.get(), equalTo(1));
// sleep till it dies
Thread.sleep((long) (TimeUnit.SECONDS.toMillis(keepAlive) * 1.33));
MatcherAssert.assertThat("after keep alive timeout, there should NOT be any threads waiting", activeThreadCount1.get(), equalTo(0));
MatcherAssert.assertThat("after keep alive timeout, there should NOT be any threads waiting", activeThreadCount2.get(), equalTo(0));
// disable network and add a network bound job
networkUtilWithoutEventSupport.setHasNetwork(false);
networkUtilWithEventSupport.setHasNetwork(false, true);
jobManager1.addJob(new DummyJob(new Params(0).requireNetwork()));
jobManager2.addJob(new DummyJob(new Params(0).requireNetwork()));
Thread.sleep(1000 + (long) (TimeUnit.SECONDS.toMillis(keepAlive) * 2));
MatcherAssert.assertThat("when network changes cannot be detected, there should be a consumer waiting alive", activeThreadCount1.get(), equalTo(1));
MatcherAssert.assertThat("when network changes can be detected, there should not be a consumer waiting alive", activeThreadCount2.get(), equalTo(0));
networkUtilWithEventSupport.setHasNetwork(true, true);
networkUtilWithoutEventSupport.setHasNetwork(true);
Thread.sleep(500);
MatcherAssert.assertThat("when network is recovered, job should be handled", jobManager2.count(), equalTo(0));
Thread.sleep(1000);
MatcherAssert.assertThat("when network is recovered, job should be handled", jobManager1.count(), equalTo(0));
}
use of com.path.android.jobqueue.Params in project android-priority-jobqueue by path.
the class NetworkJobTest method testNetworkJob.
@Test
public void testNetworkJob() throws Exception {
JobManagerTestBase.DummyNetworkUtil dummyNetworkUtil = new JobManagerTestBase.DummyNetworkUtil();
JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).networkUtil(dummyNetworkUtil));
jobManager.stop();
DummyJob networkDummyJob = new DummyJob(new Params(5).requireNetwork());
jobManager.addJob(networkDummyJob);
DummyJob noNetworkDummyJob = new DummyJob(new Params(2));
jobManager.addJob(noNetworkDummyJob);
DummyJob networkPersistentJob = new DummyJob(new Params(6).persist().requireNetwork());
jobManager.addJob(networkPersistentJob);
DummyJob noNetworkPersistentJob = new DummyJob(new Params(1).persist());
jobManager.addJob(noNetworkPersistentJob);
MatcherAssert.assertThat("count should be correct if there are network and non-network jobs w/o network", jobManager.count(), equalTo(4));
dummyNetworkUtil.setHasNetwork(true);
MatcherAssert.assertThat("count should be correct if there is network and non-network jobs w/o network", jobManager.count(), equalTo(4));
dummyNetworkUtil.setHasNetwork(false);
jobManager.start();
// this should be enough to consume dummy jobs
Thread.sleep(1000);
MatcherAssert.assertThat("no network jobs should be executed even if there is no network", jobManager.count(), equalTo(2));
dummyNetworkUtil.setHasNetwork(true);
// this should be enough to consume dummy jobs
Thread.sleep(1000);
MatcherAssert.assertThat("when network is recovered, all network jobs should be automatically consumed", jobManager.count(), equalTo(0));
}
Aggregations