}
@Test
public void testSimple() throws Exception
{
Timing timing = new Timing();
DistributedQueue<String> queue = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try
{
client.start();
final List<String> messages = new CopyOnWriteArrayList<String>();
final CountDownLatch latch = new CountDownLatch(2);
final Semaphore semaphore = new Semaphore(0);
QueueConsumer<String> consumer = new QueueConsumer<String>()
{
@Override
public void consumeMessage(String message) throws Exception
{
messages.add(message);
semaphore.acquire();
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState)
{
}
};
queue = QueueBuilder.builder(client, consumer, serializer, "/queue").executor(Executors.newSingleThreadExecutor()).maxItems(1).buildQueue();
queue.start();
QueuePutListener<String> listener = new QueuePutListener<String>()
{
@Override
public void putCompleted(String item)
{
latch.countDown();
}
@Override
public void putMultiCompleted(MultiItem<String> items)
{
}
};
queue.getPutListenerContainer().addListener(listener);
Assert.assertTrue(queue.put("1", timing.milliseconds(), TimeUnit.MILLISECONDS)); // should end up in consumer
Assert.assertTrue(queue.put("2", timing.milliseconds(), TimeUnit.MILLISECONDS)); // should sit blocking in DistributedQueue
Assert.assertTrue(timing.awaitLatch(latch));
timing.sleepABit();
Assert.assertFalse(queue.put("3", timing.multiple(.5).milliseconds(), TimeUnit.MILLISECONDS));
semaphore.release(100);
Assert.assertTrue(queue.put("3", timing.milliseconds(), TimeUnit.MILLISECONDS));
Assert.assertTrue(queue.put("4", timing.milliseconds(), TimeUnit.MILLISECONDS));
Assert.assertTrue(queue.put("5", timing.milliseconds(), TimeUnit.MILLISECONDS));
for ( int i = 0; i < 5; ++i )
{
if ( messages.size() == 3 )
{
break;
}
timing.sleepABit();
}
timing.sleepABit();
Assert.assertEquals(messages, Arrays.asList("1", "2", "3", "4", "5"));
}
finally
{