Search in sources :

Example 1 with SystemTimer

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);
}
Also used : SystemTimer(com.birbit.android.jobqueue.timer.SystemTimer) Test(org.junit.Test) SuppressLint(android.annotation.SuppressLint)

Example 2 with SystemTimer

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));
}
Also used : ArrayList(java.util.ArrayList) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) Semaphore(java.util.concurrent.Semaphore) SystemTimer(com.birbit.android.jobqueue.timer.SystemTimer) Timer(com.birbit.android.jobqueue.timer.Timer) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) SystemTimer(com.birbit.android.jobqueue.timer.SystemTimer) Test(org.junit.Test)

Example 3 with SystemTimer

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);
}
Also used : SystemTimer(com.birbit.android.jobqueue.timer.SystemTimer) Test(org.junit.Test) SuppressLint(android.annotation.SuppressLint)

Aggregations

SystemTimer (com.birbit.android.jobqueue.timer.SystemTimer)3 Test (org.junit.Test)3 SuppressLint (android.annotation.SuppressLint)2 JobManager (com.birbit.android.jobqueue.JobManager)1 Params (com.birbit.android.jobqueue.Params)1 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)1 Timer (com.birbit.android.jobqueue.timer.Timer)1 ArrayList (java.util.ArrayList)1 Semaphore (java.util.concurrent.Semaphore)1