Search in sources :

Example 1 with SimpleInMemoryPriorityQueue

use of com.birbit.android.jobqueue.inMemoryQueue.SimpleInMemoryPriorityQueue in project android-priority-jobqueue by yigit.

the class LoadFactorTest method testGoIdleIfNextJobCannotBeRunNow.

@SuppressLint("SLEEP_IN_CODE")
@Test
public void testGoIdleIfNextJobCannotBeRunNow() throws InterruptedException {
    // see: https://github.com/yigit/android-priority-jobqueue/issues/262
    final AtomicInteger nextJobDelayCall = new AtomicInteger(1);
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).maxConsumerCount(3).minConsumerCount(1).loadFactor(3).queueFactory(new QueueFactory() {

        @Override
        public JobQueue createPersistentQueue(Configuration configuration, long sessionId) {
            return new SqliteJobQueue(configuration, sessionId, new SqliteJobQueue.JavaSerializer());
        }

        @Override
        public JobQueue createNonPersistent(Configuration configuration, long sessionId) {
            return new SimpleInMemoryPriorityQueue(configuration, sessionId) {

                @Override
                public Long getNextJobDelayUntilNs(@NonNull Constraint constraint) {
                    nextJobDelayCall.incrementAndGet();
                    return super.getNextJobDelayUntilNs(constraint);
                }
            };
        }
    }).timer(mockTimer));
    final DummyJobWithStartEndLatch job1 = new DummyJobWithStartEndLatch(new Params(1));
    final DummyJobWithStartEndLatch job2 = new DummyJobWithStartEndLatch(new Params(1));
    jobManager.addJob(job1);
    jobManager.addJob(job2);
    assertThat(startLatch.await(5, TimeUnit.MINUTES), is(true));
    // give it some time to cool down, ugly but nothing to do
    Thread.sleep(2000);
    int startCount = nextJobDelayCall.get();
    Thread.sleep(5000);
    assertThat("JobManager should not query any more next jobs", nextJobDelayCall.get(), is(startCount));
    waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {

        @Override
        public void run() {
            canEndLatch.countDown();
        }

        @Override
        public void assertJob(Job job) {
        }
    });
}
Also used : Configuration(com.birbit.android.jobqueue.config.Configuration) JobQueue(com.birbit.android.jobqueue.JobQueue) SqliteJobQueue(com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue) Constraint(com.birbit.android.jobqueue.Constraint) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) QueueFactory(com.birbit.android.jobqueue.QueueFactory) SuppressLint(android.annotation.SuppressLint) Constraint(com.birbit.android.jobqueue.Constraint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SqliteJobQueue(com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue) SimpleInMemoryPriorityQueue(com.birbit.android.jobqueue.inMemoryQueue.SimpleInMemoryPriorityQueue) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test) SuppressLint(android.annotation.SuppressLint)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Constraint (com.birbit.android.jobqueue.Constraint)1 Job (com.birbit.android.jobqueue.Job)1 JobManager (com.birbit.android.jobqueue.JobManager)1 JobQueue (com.birbit.android.jobqueue.JobQueue)1 Params (com.birbit.android.jobqueue.Params)1 QueueFactory (com.birbit.android.jobqueue.QueueFactory)1 Configuration (com.birbit.android.jobqueue.config.Configuration)1 SimpleInMemoryPriorityQueue (com.birbit.android.jobqueue.inMemoryQueue.SimpleInMemoryPriorityQueue)1 SqliteJobQueue (com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue)1 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1