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));
}
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);
}
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));
}
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);
}
Aggregations