use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueAdvancedTest method testPollLong.
@Test
public void testPollLong() throws Exception {
HazelcastInstance[] instances = createHazelcastInstanceFactory(2).newInstances(getConfig());
HazelcastInstance h1 = instances[0];
HazelcastInstance h2 = instances[1];
IQueue<VersionedObject<String>> q1 = h1.getQueue("default");
IQueue<VersionedObject<String>> q2 = h2.getQueue("default");
CountDownLatch offerLatch = new CountDownLatch(2 * 100);
Thread.sleep(1000);
new Thread(() -> {
for (int i = 0; i < 100; i++) {
if (q1.offer(new VersionedObject<>("item"))) {
offerLatch.countDown();
}
if (q2.offer(new VersionedObject<>("item"))) {
offerLatch.countDown();
}
}
}).start();
assertOpenEventually(offerLatch);
ExecutorService es = Executors.newFixedThreadPool(50);
CountDownLatch latch = new CountDownLatch(200);
Thread.sleep(3000);
for (int i = 0; i < 100; i++) {
es.execute(() -> {
try {
if (new VersionedObject<>("item").equals(q1.poll(5, SECONDS))) {
latch.countDown();
}
} catch (InterruptedException e) {
LOG.info(e);
}
});
es.execute(() -> {
try {
if (new VersionedObject<>("item").equals(q2.poll(5, SECONDS))) {
latch.countDown();
}
} catch (InterruptedException e) {
LOG.info(e);
}
});
}
assertOpenEventually(latch);
es.shutdown();
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueListenerTest method testConfigListenerRegistration.
@Test
public void testConfigListenerRegistration() throws Exception {
Config config = getConfig();
String name = "queue";
QueueConfig queueConfig = config.getQueueConfig(name);
CountdownItemListener listener = new CountdownItemListener(1, 1);
ItemListenerConfig itemListenerConfig = new ItemListenerConfig(listener, true);
queueConfig.addItemListenerConfig(itemListenerConfig);
HazelcastInstance instance = createHazelcastInstance(config);
IQueue<VersionedObject<String>> queue = instance.getQueue(name);
queue.offer(new VersionedObject<>("item"));
queue.poll();
assertTrue(listener.added.await(10, TimeUnit.SECONDS));
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueListenerTest method testListener_withEvictionViaTTL.
@Test
public void testListener_withEvictionViaTTL() throws Exception {
Config config = getConfig();
config.getQueueConfig("queueWithTTL").setEmptyQueueTtl(0);
HazelcastInstance hz = createHazelcastInstance(config);
final CountDownLatch latch = new CountDownLatch(2);
hz.addDistributedObjectListener(new DistributedObjectListener() {
@Override
public void distributedObjectCreated(DistributedObjectEvent event) {
latch.countDown();
}
@Override
public void distributedObjectDestroyed(DistributedObjectEvent event) {
latch.countDown();
}
});
IQueue<VersionedObject<String>> queue = hz.getQueue("queueWithTTL");
queue.offer(new VersionedObject<>("item"));
queue.poll();
assertTrue(latch.await(10, TimeUnit.SECONDS));
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueListenerTest method testListeners.
@Test
public void testListeners() throws Exception {
int totalQueuePut = 2000;
String queueName = randomString();
HazelcastInstance instance = createHazelcastInstance();
IQueue<VersionedObject<String>> queue = instance.getQueue(queueName);
TestItemListener listener = new TestItemListener(totalQueuePut);
UUID listenerId = queue.addItemListener(listener, true);
for (int i = 0; i < totalQueuePut / 2; i++) {
queue.offer(new VersionedObject<>("item-" + i, i));
}
for (int i = 0; i < totalQueuePut / 2; i++) {
queue.poll();
}
assertTrue(listener.latch.await(5, TimeUnit.SECONDS));
queue.removeItemListener(listenerId);
queue.offer(new VersionedObject<>("item-a"));
queue.poll();
assertTrue(listener.notCalled.get());
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueAdvancedTest method testCompareAndRemoveBackup.
@Test
public void testCompareAndRemoveBackup() {
HazelcastInstance[] instances = createHazelcastInstances();
HazelcastInstance instance1 = instances[0];
HazelcastInstance instance2 = instances[1];
String name = generateKeyOwnedBy(instance1);
IQueue<VersionedObject<String>> queue1 = instance1.getQueue(name);
IQueue<VersionedObject<String>> queue2 = instance2.getQueue(name);
for (int i = 0; i < 4; i++) {
queue1.offer(new VersionedObject<>("item" + i, i));
}
assertSizeEventually(4, queue2);
assertIterableEquals(queue2, new VersionedObject<>("item0", 0), new VersionedObject<>("item1", 1), new VersionedObject<>("item2", 2), new VersionedObject<>("item3", 3));
List<VersionedObject<String>> list = new ArrayList<>();
list.add(new VersionedObject<>("item0", 0));
list.add(new VersionedObject<>("item1", 1));
list.add(new VersionedObject<>("item2", 2));
assertTrue("Expected queue1.removeAll() to succeed", queue1.removeAll(list));
instance1.shutdown();
assertSizeEventually(1, queue2);
assertIterableEquals(queue2, new VersionedObject<>("item3", 3));
}
Aggregations