assertEquals(null, InMemoryCommandQueue.getQueue("test-queue"));
}
public void test_queue_receive() throws CommandQueueException, InterruptedException {
ConfigTree config = new ConfigTree("config");
InMemoryCommandQueue commandQueue = new InMemoryCommandQueue();
// receive should fail if the queue hasn't been opened yet...
try {
commandQueue.receiveCommand(0);
fail("Expected CommandQueueException.");
} catch (CommandQueueException e) {
// OK
}
config.setAttribute(InMemoryCommandQueue.COMMAND_QUEUE_NAME, "test-queue");
commandQueue.open(config);
// Start the consumer thread - it will receive the commands from the queue.
CommandConsumerThread consumerThread = new CommandConsumerThread(commandQueue);
consumerThread.start();
// Make sure the thread is running.
assertTrue(consumerThread.isRunning);
commandQueue.addCommand("command1");
assertCommandReceived(consumerThread, "command1", 0);
commandQueue.addCommand("command2");
assertCommandReceived(consumerThread, "command2", 1);
commandQueue.addCommand("command3");
assertCommandReceived(consumerThread, "command3", 2);
// Stop the queue thread...
commandQueue.addCommand("stop");
Thread.sleep(50);
assertTrue(!consumerThread.isRunning); // this flag being reset proves the stop command was consumed and so the queue is really working
assertEquals(4, consumerThread.unblockCount); // Should have unblocked 4 times - once for each command.
// receive should fail if the queue has been closed...
commandQueue.close();
try {
commandQueue.receiveCommand(0);
fail("Expected CommandQueueException.");
} catch (CommandQueueException e) {
// OK
}
}