Search in sources :

Example 1 with CommandMessage

use of com.birbit.android.jobqueue.messaging.message.CommandMessage 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 CommandMessage

use of com.birbit.android.jobqueue.messaging.message.CommandMessage 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 CommandMessage

use of com.birbit.android.jobqueue.messaging.message.CommandMessage 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 CommandMessage

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

the class CallbackManager method destroy.

public void destroy() {
    if (!started.get()) {
        return;
    }
    CommandMessage message = factory.obtain(CommandMessage.class);
    message.set(CommandMessage.QUIT);
    messageQueue.post(message);
}
Also used : CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage)

Example 5 with CommandMessage

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

the class CallbackManager method start.

private void start() {
    Thread callbackThread = new Thread(new Runnable() {

        @Override
        public void run() {
            messageQueue.consume(new MessageQueueConsumer() {

                long lastDelivery = Long.MIN_VALUE;

                @Override
                public void onStart() {
                }

                @Override
                public void handleMessage(Message message) {
                    if (message.type == Type.CALLBACK) {
                        CallbackMessage cm = (CallbackMessage) message;
                        deliverMessage(cm);
                        lastDelivery = timer.nanoTime();
                    } else if (message.type == Type.CANCEL_RESULT_CALLBACK) {
                        deliverCancelResult((CancelResultMessage) message);
                        lastDelivery = timer.nanoTime();
                    } else if (message.type == Type.COMMAND) {
                        CommandMessage command = (CommandMessage) message;
                        final int what = command.getWhat();
                        if (what == CommandMessage.QUIT) {
                            messageQueue.stop();
                            started.set(false);
                        } else if (what == CommandMessage.RUNNABLE) {
                            command.getRunnable().run();
                        }
                    } else if (message.type == Type.PUBLIC_QUERY) {
                        ((PublicQueryMessage) message).getCallback().onResult(0);
                    }
                }

                @Override
                public void onIdle() {
                }
            });
        }
    }, "job-manager-callbacks");
    try {
        callbackThread.start();
    } catch (InternalError error) {
        // process is already dying, no reason to crash for this (and hide the real crash)
        JqLog.e(error, "Cannot start a thread. Looks like app is shutting down." + "See issue #294 for details.");
    }
}
Also used : CancelResultMessage(com.birbit.android.jobqueue.messaging.message.CancelResultMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) PublicQueryMessage(com.birbit.android.jobqueue.messaging.message.PublicQueryMessage) CallbackMessage(com.birbit.android.jobqueue.messaging.message.CallbackMessage) Message(com.birbit.android.jobqueue.messaging.Message) CallbackMessage(com.birbit.android.jobqueue.messaging.message.CallbackMessage) PublicQueryMessage(com.birbit.android.jobqueue.messaging.message.PublicQueryMessage) CancelResultMessage(com.birbit.android.jobqueue.messaging.message.CancelResultMessage) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage)

Aggregations

CommandMessage (com.birbit.android.jobqueue.messaging.message.CommandMessage)24 Test (org.junit.Test)12 MockTimer (com.birbit.android.jobqueue.test.timer.MockTimer)6 MessageQueueConsumer (com.birbit.android.jobqueue.messaging.MessageQueueConsumer)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 AddJobMessage (com.birbit.android.jobqueue.messaging.message.AddJobMessage)3 Message (com.birbit.android.jobqueue.messaging.Message)2 PublicQueryMessage (com.birbit.android.jobqueue.messaging.message.PublicQueryMessage)2 RunJobMessage (com.birbit.android.jobqueue.messaging.message.RunJobMessage)2 VisibleForTesting (android.support.annotation.VisibleForTesting)1 SafeMessageQueue (com.birbit.android.jobqueue.messaging.SafeMessageQueue)1 CallbackMessage (com.birbit.android.jobqueue.messaging.message.CallbackMessage)1 CancelMessage (com.birbit.android.jobqueue.messaging.message.CancelMessage)1 CancelResultMessage (com.birbit.android.jobqueue.messaging.message.CancelResultMessage)1 ConstraintChangeMessage (com.birbit.android.jobqueue.messaging.message.ConstraintChangeMessage)1 JobConsumerIdleMessage (com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage)1 RunJobResultMessage (com.birbit.android.jobqueue.messaging.message.RunJobResultMessage)1 SchedulerMessage (com.birbit.android.jobqueue.messaging.message.SchedulerMessage)1 SchedulerConstraint (com.birbit.android.jobqueue.scheduling.SchedulerConstraint)1 AssertionFailedError (junit.framework.AssertionFailedError)1