Search in sources :

Example 31 with JobHolder

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

the class JobQueueTestBase method assertTags.

private void assertTags(String msg, JobQueue jobQueue, JobHolder holder) {
    Set<JobHolder> result;
    String wrongTag;
    final String id = holder.getId();
    boolean found;
    Matcher allTagsMatcher = CoreMatchers.hasItems(holder.getTags().toArray(new String[holder.getTags().size()]));
    do {
        wrongTag = UUID.randomUUID().toString();
        found = false;
        if (holder.getTags() != null) {
            for (String tag : holder.getTags()) {
                if (tag.equals(wrongTag)) {
                    found = true;
                    break;
                }
            }
        }
    } while (found);
    result = jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), wrongTag));
    found = false;
    for (JobHolder received : result) {
        if (received.getId().equals(holder.getId())) {
            found = true;
        }
    }
    assertThat(msg + " when wrong tag is given, our job should not return", found, is(false));
    if (holder.getTags() == null) {
        // done
        return;
    }
    Collection<String> exclude = Arrays.asList(holder.getId());
    for (String[] tags : combinations(holder.getTags())) {
        result = jobQueue.findJobs(forTags(mockTimer, TagConstraint.ANY, Collections.<String>emptyList(), tags));
        if (tags.length == 0) {
            assertThat(msg + " empty tag list, should return 0 jobs", result.size(), is(0));
        } else {
            assertThat(msg + " any combinations: when correct tag is given, it should return one", result.size(), is(1));
            assertThat(msg + " any combinations: returned job should be the correct one", result.iterator().next().getId(), is(id));
            assertThat(msg + " returned holder should have all tags:", result.iterator().next().getTags(), allTagsMatcher);
        }
        result = jobQueue.findJobs(forTags(mockTimer, TagConstraint.ANY, exclude, tags));
        assertThat(msg + " when excluded, holder should not show up in results", result.size(), is(0));
    }
    for (String[] tags : combinations(holder.getTags())) {
        result = jobQueue.findJobs(forTags(mockTimer, ALL, Collections.<String>emptyList(), tags));
        if (tags.length == 0) {
            assertThat(msg + " empty tag list, should return 0 jobs", result.size(), is(0));
        } else {
            assertThat(msg + " all combinations: when correct tag is given, it should return one", result.size(), is(1));
            assertThat(msg + " all combinations: returned job should be the correct one", result.iterator().next().getId(), is(id));
            assertThat(msg + " returned holder should have all tags:", result.iterator().next().getTags(), allTagsMatcher);
        }
        result = jobQueue.findJobs(forTags(mockTimer, ALL, exclude, tags));
        assertThat(msg + " when excluded, holder should not show up in results", result.size(), is(0));
    }
    for (String[] tags : combinations(holder.getTags())) {
        String[] tagsWithAdditional = new String[tags.length + 1];
        System.arraycopy(tags, 0, tagsWithAdditional, 0, tags.length);
        tagsWithAdditional[tags.length] = wrongTag;
        result = jobQueue.findJobs(forTags(mockTimer, TagConstraint.ANY, Collections.<String>emptyList(), tagsWithAdditional));
        if (tags.length == 0) {
            assertThat(msg + " empty tag list, should return 0 jobs", result.size(), is(0));
        } else {
            assertThat(msg + " any combinations with wrong tag: when correct tag is given, it should return one", result.size(), is(1));
            assertThat(msg + " any combinations with wrong tag: returned job should be the correct one", result.iterator().next().getId(), is(id));
            assertThat(msg + " returned holder should have all tags:", result.iterator().next().getTags(), allTagsMatcher);
        }
        result = jobQueue.findJobs(forTags(mockTimer, ALL, Collections.<String>emptyList(), tagsWithAdditional));
        assertThat(msg + " all combinations with wrong tag: when an additional wrong tag is given, it should return 0", result.size(), is(0));
        result = jobQueue.findJobs(forTags(mockTimer, ALL, exclude, tagsWithAdditional));
        assertThat(msg + " when excluded, holder should not show up in results", result.size(), is(0));
    }
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) Matcher(org.hamcrest.Matcher)

Example 32 with JobHolder

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

the class JobQueueTestBase method testDelayUntilWithNetworkRequirement.

@Test
public void testDelayUntilWithNetworkRequirement() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).requireNetwork().overrideDeadlineToRunInMs(1000));
    jobQueue.insert(holder1);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(1000000000L));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Example 33 with JobHolder

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

the class JobQueueTestBase method testDeadlineDoesNotAffectExcludeRunning.

@Test
public void testDeadlineDoesNotAffectExcludeRunning() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder jobHolder = createNewJobHolder(new Params(0).overrideDeadlineToRunInMs(10));
    jobQueue.insert(jobHolder);
    TestConstraint testConstraint = new TestConstraint(mockTimer);
    assertThat(jobQueue.nextJobAndIncRunCount(testConstraint).getId(), is(jobHolder.getId()));
    mockTimer.incrementMs(100);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setExcludeRunning(true);
    assertThat(jobQueue.findJobs(constraint), is(Collections.EMPTY_SET));
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(nullValue()));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Example 34 with JobHolder

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

Example 35 with JobHolder

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

the class JobQueueTestBase method testDeadlineDoesNotAffectExcludeGroupQuery.

@Test
public void testDeadlineDoesNotAffectExcludeGroupQuery() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder jobHolder = createNewJobHolder(new Params(0).groupBy("g1").overrideDeadlineToRunInMs(10));
    jobQueue.insert(jobHolder);
    mockTimer.incrementMs(100);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setExcludeGroups(Arrays.asList("g1"));
    assertThat(jobQueue.findJobs(constraint), is(Collections.EMPTY_SET));
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(nullValue()));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Aggregations

JobHolder (com.birbit.android.jobqueue.JobHolder)62 Test (org.junit.Test)50 Params (com.birbit.android.jobqueue.Params)46 JobQueue (com.birbit.android.jobqueue.JobQueue)42 TestConstraint (com.birbit.android.jobqueue.TestConstraint)39 Constraint (com.birbit.android.jobqueue.Constraint)8 TagConstraint (com.birbit.android.jobqueue.TagConstraint)7 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)7 JobManager (com.birbit.android.jobqueue.JobManager)6 Job (com.birbit.android.jobqueue.Job)4 Cursor (android.database.Cursor)2 Consumer (com.birbit.android.jobqueue.ConsumerManager.Consumer)2 Configuration (com.birbit.android.jobqueue.config.Configuration)2 MessageFactory (com.birbit.android.jobqueue.messaging.MessageFactory)2 MessageQueueConsumer (com.birbit.android.jobqueue.messaging.MessageQueueConsumer)2 PriorityMessageQueue (com.birbit.android.jobqueue.messaging.PriorityMessageQueue)2 SafeMessageQueue (com.birbit.android.jobqueue.messaging.SafeMessageQueue)2 RunJobMessage (com.birbit.android.jobqueue.messaging.message.RunJobMessage)2 MockTimer (com.birbit.android.jobqueue.test.timer.MockTimer)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2