Search in sources :

Example 1 with MockTimer

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

the class MessageQueueTestBase method addMessageOnIdle.

private void addMessageOnIdle(final boolean delayed) throws InterruptedException {
    final MockTimer timer = new MockTimer();
    final MessageQueue mq = createMessageQueue(timer, new MessageFactory());
    final CountDownLatch idleLatch = new CountDownLatch(1);
    final CountDownLatch runLatch = new CountDownLatch(1);
    final MessageQueueConsumer mqConsumer = new MessageQueueConsumer() {

        @Override
        public void handleMessage(Message message) {
            if (message.type == Type.COMMAND && ((CommandMessage) message).getWhat() == CommandMessage.POKE) {
                runLatch.countDown();
            }
        }

        @Override
        public void onIdle() {
            if (idleLatch.getCount() == 1) {
                CommandMessage cm = new CommandMessage();
                cm.set(CommandMessage.POKE);
                if (delayed) {
                    mq.postAt(cm, timer.nanoTime() + 100000);
                } else {
                    mq.post(cm);
                }
                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);
    assertThat(runLatch.await(10, TimeUnit.SECONDS), CoreMatchers.is(true));
    mq.stop();
    thread.join(5000);
    if (thread.isAlive()) {
        threadDump.failed(new AssertionFailedError("thread did not die"), null);
    }
    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) AssertionFailedError(junit.framework.AssertionFailedError) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage)

Example 2 with MockTimer

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

the class MessageQueueTestBase method recycleOnClear.

@Test
public void recycleOnClear() {
    MessageFactory factory = spy(new MessageFactory());
    MockTimer mockTimer = new MockTimer();
    T mq = createMessageQueue(mockTimer, factory);
    CommandMessage cm = factory.obtain(CommandMessage.class);
    cm.set(CommandMessage.POKE);
    mq.post(cm);
    mq.clear();
    verify(factory).release(cm);
}
Also used : MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) Test(org.junit.Test)

Example 3 with MockTimer

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

the class MessageQueueTestBase method postAtNoIdleCall.

@Test
public void postAtNoIdleCall() throws InterruptedException {
    final MockTimer timer = new MockTimer();
    final MessageQueue mq = createMessageQueue(timer, new MessageFactory());
    final CountDownLatch idleLatch = new CountDownLatch(1);
    final CountDownLatch firstIdleLatch = new CountDownLatch(1);
    final CountDownLatch runLatch = new CountDownLatch(1);
    final MessageQueueConsumer mqConsumer = new MessageQueueConsumer() {

        @Override
        public void handleMessage(Message message) {
            if (message.type == Type.COMMAND && ((CommandMessage) message).getWhat() == CommandMessage.POKE) {
                runLatch.countDown();
            }
        }

        @Override
        public void onIdle() {
            if (firstIdleLatch.getCount() == 1) {
                firstIdleLatch.countDown();
            } else {
                idleLatch.countDown();
            }
        }
    };
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            mq.consume(mqConsumer);
        }
    });
    thread.start();
    firstIdleLatch.await();
    CommandMessage cm = new CommandMessage();
    cm.set(CommandMessage.POKE);
    mq.postAt(cm, 100);
    assertThat(idleLatch.await(3, TimeUnit.SECONDS), CoreMatchers.is(false));
    timer.incrementNs(100);
    assertThat(idleLatch.await(3, TimeUnit.SECONDS), CoreMatchers.is(true));
    mq.stop();
    thread.join();
}
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 4 with MockTimer

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

the class RunningJobSetTest method testAddSameGroupTwiceWithTimeout.

@Test
public void testAddSameGroupTwiceWithTimeout() {
    MockTimer timer = new MockTimer();
    set = new RunningJobSet(timer);
    set.addGroupUntil("g1", 10L);
    set.addGroupUntil("g1", 12L);
    timer.setNow(5);
    assertList("g1");
    timer.setNow(11);
    assertList("g1");
    timer.setNow(13);
    assertList();
}
Also used : MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) RunningJobSet(com.birbit.android.jobqueue.RunningJobSet) Test(org.junit.Test)

Example 5 with MockTimer

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

the class RunningJobSetTest method testAddWithTimeout.

@Test
public void testAddWithTimeout() {
    MockTimer timer = new MockTimer();
    set = new RunningJobSet(timer);
    set.addGroupUntil("g1", 10L);
    timer.setNow(5);
    assertList("g1");
    timer.setNow(11);
    assertList();
    timer.setNow(3);
    // should've pruned the list
    assertList();
}
Also used : MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) RunningJobSet(com.birbit.android.jobqueue.RunningJobSet) 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