Search in sources :

Example 11 with MockTimer

use of com.birbit.android.jobqueue.test.timer.MockTimer in project android-priority-jobqueue by yigit.

the class MessageQueueTestBase method postDelayed.

@Test
public void postDelayed() throws InterruptedException {
    MockTimer timer = new MockTimer();
    final T mq = createMessageQueue(timer, new MessageFactory());
    final CountDownLatch idleLatch = new CountDownLatch(1);
    final Throwable[] exception = new Throwable[1];
    final MessageQueueConsumer mqConsumer = new MessageQueueConsumer() {

        @Override
        public void handleMessage(Message message) {
        }

        @Override
        public void onIdle() {
            try {
                assertThat(idleLatch.getCount(), CoreMatchers.is(1L));
            } catch (Throwable t) {
                exception[0] = t;
            } finally {
                idleLatch.countDown();
            }
        }
    };
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            mq.consume(mqConsumer);
        }
    });
    thread.start();
    assertThat(idleLatch.await(10, TimeUnit.SECONDS), CoreMatchers.is(true));
    timer.incrementMs(1000000);
    Thread.sleep(1000);
    mq.stop();
    thread.join(5000);
    assertThat(exception[0], CoreMatchers.nullValue());
    assertThat(thread.isAlive(), CoreMatchers.is(false));
}
Also used : MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 12 with MockTimer

use of com.birbit.android.jobqueue.test.timer.MockTimer in project android-priority-jobqueue by yigit.

the class MessageQueueTestBase method recycleOnCancel.

@Test
public void recycleOnCancel() {
    MessageFactory factory = spy(new MessageFactory());
    MockTimer mockTimer = new MockTimer();
    final T mq = createMessageQueue(mockTimer, factory);
    final CommandMessage cm = factory.obtain(CommandMessage.class);
    cm.set(CommandMessage.POKE);
    mq.post(cm);
    final CommandMessage cm2 = factory.obtain(CommandMessage.class);
    cm2.set(CommandMessage.POKE);
    mq.post(cm2);
    mq.cancelMessages(new MessagePredicate() {

        @Override
        public boolean onMessage(Message message) {
            return message == cm;
        }
    });
    verify(factory).release(cm);
    verify(factory, times(0)).release(cm2);
}
Also used : MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) Test(org.junit.Test)

Example 13 with MockTimer

use of com.birbit.android.jobqueue.test.timer.MockTimer in project android-priority-jobqueue by yigit.

the class MessageQueueTestBase method postWhileIdle.

@Test
public void postWhileIdle() throws InterruptedException {
    final MockTimer timer = new MockTimer();
    final MessageQueue mq = createMessageQueue(timer, new MessageFactory());
    final CountDownLatch idleEnterLatch = new CountDownLatch(1);
    final CountDownLatch idleExitLatch = new CountDownLatch(1);
    final CountDownLatch handleMessage = new CountDownLatch(1);
    final CommandMessage cm = new CommandMessage();
    cm.set(CommandMessage.POKE);
    final MessageQueueConsumer consumer = new MessageQueueConsumer() {

        @Override
        public void handleMessage(Message message) {
            if (message == cm) {
                handleMessage.countDown();
            }
        }

        @Override
        public void onIdle() {
            idleEnterLatch.countDown();
            try {
                idleExitLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            mq.consume(consumer);
        }
    });
    thread.start();
    MatcherAssert.assertThat(idleEnterLatch.await(30, TimeUnit.SECONDS), CoreMatchers.is(true));
    mq.post(cm);
    idleExitLatch.countDown();
    MatcherAssert.assertThat(handleMessage.await(30, TimeUnit.SECONDS), CoreMatchers.is(true));
    mq.stop();
    thread.join(5000);
    assertThat(thread.isAlive(), CoreMatchers.is(false));
}
Also used : MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) CountDownLatch(java.util.concurrent.CountDownLatch) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) Test(org.junit.Test)

Example 14 with MockTimer

use of com.birbit.android.jobqueue.test.timer.MockTimer in project android-priority-jobqueue by yigit.

the class MessageQueueTestBase method recycleOnConsume.

@Test
public void recycleOnConsume() {
    MessageFactory factory = spy(new MessageFactory());
    MockTimer mockTimer = new MockTimer();
    final T mq = createMessageQueue(mockTimer, factory);
    CommandMessage cm = factory.obtain(CommandMessage.class);
    cm.set(CommandMessage.POKE);
    mq.post(cm);
    mq.consume(new MessageQueueConsumer() {

        @Override
        public void handleMessage(Message message) {
            mq.stop();
        }

        @Override
        public void onIdle() {
        }
    });
    verify(factory).release(cm);
}
Also used : MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) Test(org.junit.Test)

Aggregations

MockTimer (com.birbit.android.jobqueue.test.timer.MockTimer)14 Test (org.junit.Test)12 CommandMessage (com.birbit.android.jobqueue.messaging.message.CommandMessage)7 CountDownLatch (java.util.concurrent.CountDownLatch)4 RunningJobSet (com.birbit.android.jobqueue.RunningJobSet)3 BatchingScheduler (com.birbit.android.jobqueue.BatchingScheduler)2 JobHolder (com.birbit.android.jobqueue.JobHolder)2 Scheduler (com.birbit.android.jobqueue.scheduling.Scheduler)2 Context (android.content.Context)1 Consumer (com.birbit.android.jobqueue.ConsumerManager.Consumer)1 Params (com.birbit.android.jobqueue.Params)1 MessageFactory (com.birbit.android.jobqueue.messaging.MessageFactory)1 MessageQueueConsumer (com.birbit.android.jobqueue.messaging.MessageQueueConsumer)1 PriorityMessageQueue (com.birbit.android.jobqueue.messaging.PriorityMessageQueue)1 SafeMessageQueue (com.birbit.android.jobqueue.messaging.SafeMessageQueue)1 RunJobMessage (com.birbit.android.jobqueue.messaging.message.RunJobMessage)1 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)1 AssertionFailedError (junit.framework.AssertionFailedError)1 Before (org.junit.Before)1