use of com.birbit.android.jobqueue.Constraint in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testBasicAddRemoveCount.
@Test
public void testBasicAddRemoveCount() throws Exception {
final int ADD_COUNT = 6;
JobQueue jobQueue = createNewJobQueue();
assertThat((int) jobQueue.count(), equalTo(0));
TestConstraint constraint = new TestConstraint(mockTimer);
constraint.setExcludeRunning(true);
assertThat(jobQueue.nextJobAndIncRunCount(constraint), nullValue());
for (int i = 0; i < ADD_COUNT; i++) {
JobHolder holder = createNewJobHolder();
jobQueue.insert(holder);
assertThat((int) jobQueue.count(), equalTo(i + 1));
assertThat(holder.getInsertionOrder(), equalTo(i + 1L));
jobQueue.insertOrReplace(holder);
assertThat((int) jobQueue.count(), equalTo(i + 1));
}
JobHolder firstHolder = jobQueue.nextJobAndIncRunCount(constraint);
assertThat(firstHolder.getRunCount(), equalTo(1));
//size should be down 1
assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 1));
//should return another job
JobHolder secondHolder = jobQueue.nextJobAndIncRunCount(constraint);
assertThat(secondHolder.getRunCount(), equalTo(1));
//size should be down 2
assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
//second holder and first holder should have different ids
assertThat(firstHolder.getId(), not(secondHolder.getId()));
jobQueue.remove(secondHolder);
assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
jobQueue.remove(secondHolder);
//non existed job removed, count should be the same
assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
jobQueue.remove(firstHolder);
assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
}
use of com.birbit.android.jobqueue.Constraint in project android-priority-jobqueue by yigit.
the class WhereQueryCache method createWhere.
private Where createWhere(long cacheKey, Constraint constraint, StringBuilder reusedStringBuilder) {
reusedStringBuilder.setLength(0);
int argCount = 0;
reusedStringBuilder.append("( (").append(DbOpenHelper.DEADLINE_COLUMN.columnName).append(" != ").append(Where.FOREVER).append(" AND ").append(DbOpenHelper.DEADLINE_COLUMN.columnName).append(" <= ?) OR ");
argCount++;
reusedStringBuilder.append(DbOpenHelper.REQUIRED_NETWORK_TYPE_COLUMN.columnName).append(" <= ?)");
reusedStringBuilder.append(" AND (").append(DbOpenHelper.CANCELLED_COLUMN.columnName).append(" IS NULL OR ").append(DbOpenHelper.CANCELLED_COLUMN.columnName).append(" != 1)");
argCount++;
if (constraint.getTimeLimit() != null) {
reusedStringBuilder.append(" AND ").append(DbOpenHelper.DELAY_UNTIL_NS_COLUMN.columnName).append(" <= ?");
argCount++;
}
if (constraint.getTagConstraint() != null) {
if (constraint.getTags().isEmpty()) {
reusedStringBuilder.append(" AND 0 ");
} else {
reusedStringBuilder.append(" AND ").append(DbOpenHelper.ID_COLUMN.columnName).append(" IN ( SELECT ").append(DbOpenHelper.TAGS_JOB_ID_COLUMN.columnName).append(" FROM ").append(DbOpenHelper.JOB_TAGS_TABLE_NAME).append(" WHERE ").append(DbOpenHelper.TAGS_NAME_COLUMN.columnName).append(" IN (");
SqlHelper.addPlaceholdersInto(reusedStringBuilder, constraint.getTags().size());
reusedStringBuilder.append(")");
if (constraint.getTagConstraint() == TagConstraint.ANY) {
reusedStringBuilder.append(")");
} else if (constraint.getTagConstraint() == TagConstraint.ALL) {
reusedStringBuilder.append(" GROUP BY (`").append(DbOpenHelper.TAGS_JOB_ID_COLUMN.columnName).append("`)").append(" HAVING count(*) = ").append(constraint.getTags().size()).append(")");
} else {
// have this in place in case we change number of constraints
throw new IllegalArgumentException("unknown constraint " + constraint);
}
argCount += constraint.getTags().size();
}
}
if (!constraint.getExcludeGroups().isEmpty()) {
reusedStringBuilder.append(" AND (").append(DbOpenHelper.GROUP_ID_COLUMN.columnName).append(" IS NULL OR ").append(DbOpenHelper.GROUP_ID_COLUMN.columnName).append(" NOT IN(");
SqlHelper.addPlaceholdersInto(reusedStringBuilder, constraint.getExcludeGroups().size());
reusedStringBuilder.append("))");
argCount += constraint.getExcludeGroups().size();
}
if (!constraint.getExcludeJobIds().isEmpty()) {
reusedStringBuilder.append(" AND ").append(DbOpenHelper.ID_COLUMN.columnName).append(" NOT IN(");
SqlHelper.addPlaceholdersInto(reusedStringBuilder, constraint.getExcludeJobIds().size());
reusedStringBuilder.append(")");
argCount += constraint.getExcludeJobIds().size();
}
if (constraint.excludeRunning()) {
reusedStringBuilder.append(" AND ").append(DbOpenHelper.RUNNING_SESSION_ID_COLUMN.columnName).append(" != ?");
argCount++;
}
String[] args = new String[argCount];
//noinspection UnnecessaryLocalVariable
Where where = new Where(cacheKey, reusedStringBuilder.toString(), args);
return where;
}
use of com.birbit.android.jobqueue.Constraint in project android-priority-jobqueue by yigit.
the class WhereQueryCache method fillWhere.
private void fillWhere(Constraint constraint, Where where) {
int count = 0;
where.args[count++] = Long.toString(constraint.getNowInNs());
where.args[count++] = Integer.toString(constraint.getMaxNetworkType());
if (constraint.getTimeLimit() != null) {
where.args[count++] = Long.toString(constraint.getTimeLimit());
}
if (constraint.getTagConstraint() != null) {
for (String tag : constraint.getTags()) {
where.args[count++] = tag;
}
}
for (String group : constraint.getExcludeGroups()) {
where.args[count++] = group;
}
for (String jobId : constraint.getExcludeJobIds()) {
where.args[count++] = jobId;
}
if (constraint.excludeRunning()) {
where.args[count++] = sessionId;
}
if (count != where.args.length) {
throw new IllegalStateException("something is wrong with where query cache for " + where.query);
}
}
use of com.birbit.android.jobqueue.Constraint in project android-priority-jobqueue by yigit.
the class SimpleInMemoryPriorityQueue method countReadyJobs.
@Override
public int countReadyJobs(@NonNull Constraint constraint) {
int count = 0;
reusedList.clear();
for (JobHolder holder : jobs) {
String groupId = holder.getGroupId();
if ((groupId == null || !reusedList.contains(groupId)) && matches(holder, constraint, false)) {
count++;
if (groupId != null) {
reusedList.add(groupId);
}
}
}
reusedList.clear();
return count;
}
use of com.birbit.android.jobqueue.Constraint 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) {
}
});
}
Aggregations