Search in sources :

Example 1 with Constraint

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));
}
Also used : TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobHolder(com.birbit.android.jobqueue.JobHolder) JobQueue(com.birbit.android.jobqueue.JobQueue) TagConstraint(com.birbit.android.jobqueue.TagConstraint) Constraint(com.birbit.android.jobqueue.Constraint) TestConstraint(com.birbit.android.jobqueue.TestConstraint) Test(org.junit.Test)

Example 2 with Constraint

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;
}
Also used : Constraint(com.birbit.android.jobqueue.Constraint) TagConstraint(com.birbit.android.jobqueue.TagConstraint)

Example 3 with Constraint

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);
    }
}
Also used : Constraint(com.birbit.android.jobqueue.Constraint) TagConstraint(com.birbit.android.jobqueue.TagConstraint)

Example 4 with Constraint

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;
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) Constraint(com.birbit.android.jobqueue.Constraint)

Example 5 with Constraint

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

Constraint (com.birbit.android.jobqueue.Constraint)8 TagConstraint (com.birbit.android.jobqueue.TagConstraint)6 JobHolder (com.birbit.android.jobqueue.JobHolder)5 JobQueue (com.birbit.android.jobqueue.JobQueue)5 Test (org.junit.Test)5 Params (com.birbit.android.jobqueue.Params)4 TestConstraint (com.birbit.android.jobqueue.TestConstraint)4 SuppressLint (android.annotation.SuppressLint)1 Job (com.birbit.android.jobqueue.Job)1 JobManager (com.birbit.android.jobqueue.JobManager)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