use of com.birbit.android.jobqueue.timer.SystemTimer in project android-priority-jobqueue by yigit.
the class SystemTimerTest method testWaitOnObject.
@SuppressLint("DIRECT_TIME_ACCESS")
@Test
public void testWaitOnObject() throws Throwable {
SystemTimer timer = new SystemTimer();
//noinspection DIRECT_TIME_ACCESS
long startRealTime = System.nanoTime();
long startNs = timer.nanoTime();
long waitNs = JobManager.NS_PER_MS * 3000;
long waitUntil = startNs + waitNs;
final Object object = new Object();
synchronized (object) {
timer.waitOnObjectUntilNs(object, waitUntil);
}
assertBetween(System.nanoTime() - startRealTime, waitNs, waitNs + 2000 * JobManager.NS_PER_MS);
}
use of com.birbit.android.jobqueue.timer.SystemTimer in project android-priority-jobqueue by yigit.
the class ConsumerCountTest method testMaxConsumerCount.
@Test
public void testMaxConsumerCount() throws Exception {
int maxConsumerCount = 2;
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).maxConsumerCount(maxConsumerCount).loadFactor(maxConsumerCount).timer(mockTimer));
Semaphore semaphore = new Semaphore(maxConsumerCount);
int totalJobCount = maxConsumerCount * 3;
List<NeverEndingDummyJob> runningJobs = new ArrayList<>(totalJobCount);
for (int i = 0; i < totalJobCount; i++) {
NeverEndingDummyJob job = new NeverEndingDummyJob(new Params((int) (Math.random() * 3)), runLock, semaphore);
runningJobs.add(job);
jobManager.addJob(job);
}
Timer timer = new SystemTimer();
//wait till enough jobs start
long start = timer.nanoTime();
long tenSeconds = TimeUnit.SECONDS.toNanos(10);
while (timer.nanoTime() - start < tenSeconds && semaphore.tryAcquire()) {
semaphore.release();
//noinspection SLEEP_IN_CODE
Thread.sleep(100);
}
MatcherAssert.assertThat("all consumers should start in 10 seconds", timer.nanoTime() - start > tenSeconds, is(false));
//wait some more to ensure no more jobs are started
//noinspection SLEEP_IN_CODE
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));
waitUntilJobsAreDone(jobManager, runningJobs, new Runnable() {
@Override
public void run() {
runLock.countDown();
}
});
MatcherAssert.assertThat("no jobs should remain", jobManager.count(), equalTo(0));
}
use of com.birbit.android.jobqueue.timer.SystemTimer in project android-priority-jobqueue by yigit.
the class SystemTimerTest method testNow.
@SuppressLint({ "DIRECT_TIME_ACCESS", "SLEEP_IN_CODE" })
@Test
public void testNow() throws Throwable {
SystemTimer timer = new SystemTimer();
//noinspection DIRECT_TIME_ACCESS
long startNs = System.nanoTime();
//noinspection DIRECT_TIME_ACCESS
long start = TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
//noinspection SLEEP_IN_CODE
Thread.sleep(3000);
assertReasonable(timer.nanoTime(), System.nanoTime() - startNs + start);
SystemTimer timer2 = new SystemTimer();
assertReasonable(timer2.nanoTime(), System.nanoTime() - startNs + start);
}
Aggregations