public void testNoDuplicateProcessing() throws Exception
{
final int itemQty = 1000;
final int consumerQty = 4;
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
client.start();
try
{
DistributedQueue<TestQueueItem> producerQueue = QueueBuilder.builder(client, null, serializer, QUEUE_PATH).buildQueue();
try
{
producerQueue.start();
for ( int i = 0; i < itemQty; ++i )
{
TestQueueItem item = new TestQueueItem(Integer.toString(i));
producerQueue.put(item);
}
producerQueue.flushPuts(timing.multiple(2).seconds(), TimeUnit.SECONDS);
}
finally
{
producerQueue.close();
}
}
finally
{
client.close();
}
final Set<String> consumedMessages = Sets.newHashSet();
final Set<String> duplicateMessages = Sets.newHashSet();
final CountDownLatch latch = new CountDownLatch(itemQty);
List<DistributedQueue<TestQueueItem>> consumers = Lists.newArrayList();
List<CuratorFramework> consumerClients = Lists.newArrayList();
try
{
final QueueConsumer<TestQueueItem> ourQueue = new QueueConsumer<TestQueueItem>()
{
@Override
public void consumeMessage(TestQueueItem message)
{
synchronized(consumedMessages)
{
if ( consumedMessages.contains(message.str) )
{
duplicateMessages.add(message.str);
}
consumedMessages.add(message.str);
}
latch.countDown();
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState)
{
}
};
for ( int i = 0; i < consumerQty; ++i )
{
CuratorFramework thisClient = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
consumerClients.add(thisClient);
thisClient.start();
DistributedQueue<TestQueueItem> thisConsumer = QueueBuilder.builder(thisClient, ourQueue, serializer, QUEUE_PATH).
lockPath("/a/locks").
buildQueue();
consumers.add(thisConsumer);
}
for ( DistributedQueue<TestQueueItem> consumer : consumers )
{
consumer.start();
}
timing.awaitLatch(latch);
Assert.assertTrue(duplicateMessages.size() == 0, duplicateMessages.toString());
}
finally
{
for ( DistributedQueue<TestQueueItem> consumer : consumers )