Search in sources :

Example 6 with JobHolder

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

the class ConsumerTest method runJobMessage.

@Test
public void runJobMessage() {
    MessageFactory factory = new MessageFactory();
    PriorityMessageQueue pmq = new PriorityMessageQueue(timer, factory);
    setRunning(pmq);
    SafeMessageQueue mq = new SafeMessageQueue(timer, factory, "test");
    setRunning(mq);
    timer.setNow(2001);
    Consumer consumer = new Consumer(pmq, mq, factory, timer);
    RunJobMessage rjm = factory.obtain(RunJobMessage.class);
    JobHolder mockHolder = mock(JobHolder.class);
    when(mockHolder.safeRun(0, timer)).thenReturn(JobHolder.RUN_RESULT_SUCCESS);
    rjm.setJobHolder(mockHolder);
    timer.setNow(3001);
    consumer.queueConsumer.handleMessage(rjm);
    Message message = pmq.next(dummyConsumer);
    assertThat(message, CoreMatchers.instanceOf(RunJobResultMessage.class));
    RunJobResultMessage result = (RunJobResultMessage) message;
    assertThat(result.getResult(), CoreMatchers.is(JobHolder.RUN_RESULT_SUCCESS));
    assertThat(result.getJobHolder(), CoreMatchers.is(mockHolder));
    assertThat(consumer.lastJobCompleted, CoreMatchers.is(3001L));
}
Also used : PriorityMessageQueue(com.birbit.android.jobqueue.messaging.PriorityMessageQueue) JobHolder(com.birbit.android.jobqueue.JobHolder) MessageFactory(com.birbit.android.jobqueue.messaging.MessageFactory) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) Consumer(com.birbit.android.jobqueue.ConsumerManager.Consumer) Message(com.birbit.android.jobqueue.messaging.Message) RunJobResultMessage(com.birbit.android.jobqueue.messaging.message.RunJobResultMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) JobConsumerIdleMessage(com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage) RunJobMessage(com.birbit.android.jobqueue.messaging.message.RunJobMessage) SafeMessageQueue(com.birbit.android.jobqueue.messaging.SafeMessageQueue) RunJobMessage(com.birbit.android.jobqueue.messaging.message.RunJobMessage) RunJobResultMessage(com.birbit.android.jobqueue.messaging.message.RunJobResultMessage) Test(org.junit.Test)

Example 7 with JobHolder

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

the class ConsumerTest method removePokesAfterJobTest.

@Test
public void removePokesAfterJobTest() {
    MessageFactory factory = new MessageFactory();
    PriorityMessageQueue pmq = new PriorityMessageQueue(timer, factory);
    setRunning(pmq);
    SafeMessageQueue mq = spy(new SafeMessageQueue(timer, factory, "test"));
    setRunning(mq);
    timer.setNow(2001);
    Consumer consumer = new Consumer(pmq, mq, factory, timer);
    RunJobMessage rjm = factory.obtain(RunJobMessage.class);
    JobHolder mockHolder = mock(JobHolder.class);
    when(mockHolder.safeRun(0, new MockTimer())).thenReturn(JobHolder.RUN_RESULT_SUCCESS);
    rjm.setJobHolder(mockHolder);
    timer.setNow(3001);
    verify(mq, times(0)).cancelMessages(Consumer.pokeMessagePredicate);
    consumer.queueConsumer.handleMessage(rjm);
    verify(mq, times(1)).cancelMessages(Consumer.pokeMessagePredicate);
}
Also used : PriorityMessageQueue(com.birbit.android.jobqueue.messaging.PriorityMessageQueue) JobHolder(com.birbit.android.jobqueue.JobHolder) MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) MessageFactory(com.birbit.android.jobqueue.messaging.MessageFactory) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) Consumer(com.birbit.android.jobqueue.ConsumerManager.Consumer) SafeMessageQueue(com.birbit.android.jobqueue.messaging.SafeMessageQueue) RunJobMessage(com.birbit.android.jobqueue.messaging.message.RunJobMessage) Test(org.junit.Test)

Example 8 with JobHolder

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

the class DelayTest method testDelay.

public void testDelay(boolean persist) throws Throwable {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    DummyJob delayedJob = new DummyJob(new Params(10).delayInMs(1000).setPersistent(persist));
    DummyJob nonDelayedJob = new DummyJob(new Params(0).setPersistent(persist));
    jobManager.addJob(delayedJob);
    jobManager.addJob(nonDelayedJob);
    JobHolder receivedJob = nextJob(jobManager);
    MatcherAssert.assertThat("non-delayed job should be served", receivedJob, notNullValue());
    MatcherAssert.assertThat("non-delayed job should id should match", receivedJob.getId(), equalTo(nonDelayedJob.getId()));
    removeJob(jobManager, receivedJob);
    MatcherAssert.assertThat("delayed job should not be served", nextJob(jobManager), nullValue());
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    mockTimer.incrementMs(500);
    MatcherAssert.assertThat("delayed job should not be served", nextJob(jobManager), nullValue());
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    mockTimer.incrementMs(2000);
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    receivedJob = nextJob(jobManager);
    MatcherAssert.assertThat("now should be able to receive the delayed job.", receivedJob, notNullValue());
    if (receivedJob != null) {
        MatcherAssert.assertThat("received job should be the delayed job", receivedJob.getId(), equalTo(delayedJob.getId()));
    }
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager)

Example 9 with JobHolder

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

the class GroupingTest method testGrouping.

@Test
public void testGrouping() throws Throwable {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    String jobId1 = addJob(jobManager, new DummyJob(new Params(0).groupBy("group1")));
    String jobId2 = addJob(jobManager, new DummyJob(new Params(0).groupBy("group1")));
    String jobId3 = addJob(jobManager, new DummyJob(new Params(0).persist().groupBy("group2")));
    String jobId4 = addJob(jobManager, new DummyJob(new Params(0).persist().groupBy("group1")));
    JobHolder nextJob = nextJob(jobManager);
    MatcherAssert.assertThat("next job should be the first job from group1", nextJob.getId(), equalTo(jobId1));
    JobHolder group2Job = nextJob(jobManager, Collections.singletonList("group1"));
    MatcherAssert.assertThat("since group 1 is running now, next job should be from group 2", group2Job.getId(), equalTo(jobId3));
    removeJob(jobManager, nextJob);
    JobHolder group1NextJob = nextJob(jobManager, Arrays.asList("group2"));
    MatcherAssert.assertThat("after removing job from group 1, another job from group1 should be returned", group1NextJob.getId(), equalTo(jobId2));
    MatcherAssert.assertThat("when jobs from both groups are running, no job should be returned from next job", nextJob(jobManager, Arrays.asList("group1", "group2")), is(nullValue()));
    removeJob(jobManager, group2Job);
    MatcherAssert.assertThat("even after group2 job is complete, no jobs should be returned" + "since we only have group1 jobs left", nextJob(jobManager, Arrays.asList("group1")), is(nullValue()));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) Test(org.junit.Test)

Example 10 with JobHolder

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

the class NetworkNextJobTest method testNetworkNextJob.

@Test
public void testNetworkNextJob() throws Throwable {
    DummyNetworkUtil dummyNetworkUtil = new DummyNetworkUtil();
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
    jobManager.stop();
    DummyJob dummyJob = new DummyJob(new Params(0).requireNetwork());
    jobManager.addJob(dummyJob);
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
    MatcherAssert.assertThat("when there isn't any network, next job should return null", nextJob(jobManager), nullValue());
    MatcherAssert.assertThat("even if there is network, job manager should return correct count", jobManager.count(), equalTo(1));
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED);
    JobHolder retrieved = nextJob(jobManager);
    MatcherAssert.assertThat("when network is recovered, next job should be retrieved", retrieved, notNullValue());
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) Configuration(com.birbit.android.jobqueue.config.Configuration) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) 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