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));
}
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) {
}
});
}
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);
}
Aggregations