Search in sources :

Example 1 with SqliteJobQueue

use of com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue in project android-priority-jobqueue by yigit.

the class SqliteJobQueueTest method testCustomSerializer.

@Test
public void testCustomSerializer() throws Exception {
    final CountDownLatch calledForSerialize = new CountDownLatch(1);
    final CountDownLatch calledForDeserialize = new CountDownLatch(1);
    SqliteJobQueue.JobSerializer jobSerializer = new SqliteJobQueue.JavaSerializer() {

        @Override
        public byte[] serialize(Object object) throws IOException {
            calledForSerialize.countDown();
            return super.serialize(object);
        }

        @Override
        public <T extends Job> T deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
            calledForDeserialize.countDown();
            return super.deserialize(bytes);
        }
    };
    SqliteJobQueue jobQueue = new SqliteJobQueue(new Configuration.Builder(RuntimeEnvironment.application).id("__" + mockTimer.nanoTime()).jobSerializer(jobSerializer).inTestMode().timer(mockTimer).build(), mockTimer.nanoTime(), jobSerializer);
    jobQueue.insert(createNewJobHolder(new Params(0)));
    calledForSerialize.await(1, TimeUnit.SECONDS);
    MatcherAssert.assertThat("custom serializer should be called for serialize", (int) calledForSerialize.getCount(), CoreMatchers.equalTo(0));
    MatcherAssert.assertThat("custom serializer should NOT be called for deserialize", (int) calledForDeserialize.getCount(), CoreMatchers.equalTo(1));
    jobQueue.nextJobAndIncRunCount(new TestConstraint(mockTimer));
    MatcherAssert.assertThat("custom serializer should be called for deserialize", (int) calledForDeserialize.getCount(), CoreMatchers.equalTo(0));
}
Also used : TestConstraint(com.birbit.android.jobqueue.TestConstraint) SqliteJobQueue(com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue) Params(com.birbit.android.jobqueue.Params) CountDownLatch(java.util.concurrent.CountDownLatch) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test)

Example 2 with SqliteJobQueue

use of com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue 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)

Example 3 with SqliteJobQueue

use of com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue in project android-priority-jobqueue by yigit.

the class SqliteJobQueueTest method testClearTags.

@Test
public void testClearTags() throws Throwable {
    SqliteJobQueue queue = (SqliteJobQueue) createNewJobQueue();
    JobHolder vh1 = createNewJobHolder(new Params(1).addTags("a", "b", "c"));
    JobHolder vh2 = createNewJobHolder(new Params(1).addTags("a", "b", "x"));
    queue.insert(vh1);
    queue.insert(vh2);
    assertTags(queue, new TagInfo(0, vh1.getId(), "a"), new TagInfo(0, vh1.getId(), "b"), new TagInfo(0, vh1.getId(), "c"), new TagInfo(0, vh2.getId(), "a"), new TagInfo(0, vh2.getId(), "b"), new TagInfo(0, vh2.getId(), "x"));
    queue.remove(vh2);
    assertTags(queue, new TagInfo(0, vh1.getId(), "a"), new TagInfo(0, vh1.getId(), "b"), new TagInfo(0, vh1.getId(), "c"));
    queue.clear();
    assertTags(queue);
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) SqliteJobQueue(com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Aggregations

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