Search in sources :

Example 1 with SafeMessageQueue

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

the class ConsumerManager method handleStop.

void handleStop() {
    // poke everybody so we can kill them
    for (Consumer consumer : consumers) {
        SafeMessageQueue mq = consumer.messageQueue;
        CommandMessage command = factory.obtain(CommandMessage.class);
        command.set(CommandMessage.POKE);
        mq.post(command);
    }
    if (consumers.isEmpty()) {
        for (Runnable runnable : internalZeroConsumersListeners) {
            runnable.run();
        }
    }
}
Also used : MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) SafeMessageQueue(com.birbit.android.jobqueue.messaging.SafeMessageQueue) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage)

Example 2 with SafeMessageQueue

use of com.birbit.android.jobqueue.messaging.SafeMessageQueue 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 3 with SafeMessageQueue

use of com.birbit.android.jobqueue.messaging.SafeMessageQueue 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 4 with SafeMessageQueue

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

the class ConsumerTest method init.

@Test
public void init() {
    PriorityMessageQueue pmq = mock(PriorityMessageQueue.class);
    SafeMessageQueue mq = mock(SafeMessageQueue.class);
    Consumer consumer = new Consumer(pmq, mq, factory, timer);
    consumer.run();
    verify(mq).consume(any(MessageQueueConsumer.class));
}
Also used : PriorityMessageQueue(com.birbit.android.jobqueue.messaging.PriorityMessageQueue) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) Consumer(com.birbit.android.jobqueue.ConsumerManager.Consumer) SafeMessageQueue(com.birbit.android.jobqueue.messaging.SafeMessageQueue) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) Test(org.junit.Test)

Example 5 with SafeMessageQueue

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

the class ConsumerTest method idleMessage.

@Test
public void idleMessage() {
    MessageFactory factory = new MessageFactory();
    PriorityMessageQueue pmq = new PriorityMessageQueue(timer, factory);
    SafeMessageQueue mq = new SafeMessageQueue(timer, factory, "test");
    setRunning(pmq);
    setRunning(mq);
    timer.setNow(2001);
    Consumer consumer = new Consumer(pmq, mq, factory, timer);
    consumer.queueConsumer.onIdle();
    Message message = pmq.next(dummyConsumer);
    assertThat(message, CoreMatchers.instanceOf(JobConsumerIdleMessage.class));
    assertThat(((JobConsumerIdleMessage) message).getLastJobCompleted(), CoreMatchers.is(2001L));
}
Also used : PriorityMessageQueue(com.birbit.android.jobqueue.messaging.PriorityMessageQueue) 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) JobConsumerIdleMessage(com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage) Test(org.junit.Test)

Aggregations

MessageQueueConsumer (com.birbit.android.jobqueue.messaging.MessageQueueConsumer)6 SafeMessageQueue (com.birbit.android.jobqueue.messaging.SafeMessageQueue)6 Consumer (com.birbit.android.jobqueue.ConsumerManager.Consumer)4 PriorityMessageQueue (com.birbit.android.jobqueue.messaging.PriorityMessageQueue)4 Test (org.junit.Test)4 MessageFactory (com.birbit.android.jobqueue.messaging.MessageFactory)3 CommandMessage (com.birbit.android.jobqueue.messaging.message.CommandMessage)3 RunJobMessage (com.birbit.android.jobqueue.messaging.message.RunJobMessage)3 JobHolder (com.birbit.android.jobqueue.JobHolder)2 Message (com.birbit.android.jobqueue.messaging.Message)2 JobConsumerIdleMessage (com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage)2 RunJobResultMessage (com.birbit.android.jobqueue.messaging.message.RunJobResultMessage)2 MockTimer (com.birbit.android.jobqueue.test.timer.MockTimer)1