use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueAdvancedTest method testShutdown.
@Test
public void testShutdown() throws InterruptedException {
HazelcastInstance[] instances = createHazelcastInstanceFactory(2).newInstances(getConfig());
HazelcastInstance h1 = instances[0];
HazelcastInstance h2 = instances[1];
warmUpPartitions(h2, h1);
IQueue<VersionedObject<String>> q1 = h1.getQueue("default");
IQueue<VersionedObject<String>> q2 = h2.getQueue("default");
for (int i = 0; i < 40; i++) {
assertTrue("Expected q1.offer() to succeed", q1.offer(new VersionedObject<>("item" + i, i), 100, SECONDS));
}
h1.getLifecycleService().shutdown();
for (int i = 40; i < 100; i++) {
assertTrue("Expected q2.offer() to succeed", q2.offer(new VersionedObject<>("item" + i, i), 100, SECONDS));
}
for (int i = 0; i < 100; i++) {
assertEquals(new VersionedObject<>("item" + i, i), q2.poll());
}
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueAdvancedTest method testOffer.
@Test
public void testOffer() 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");
for (int i = 0; i < 100; i++) {
assertTrue("Expected q1.offer() to succeed", q1.offer(new VersionedObject<>("item" + i, i), 100, SECONDS));
assertTrue("Expected q2.offer() to succeed", q2.offer(new VersionedObject<>("item" + i, i), 100, SECONDS));
}
assertEquals(new VersionedObject<>("item0", 0), q1.peek());
assertEquals(new VersionedObject<>("item0", 0), q2.peek());
for (int i = 0; i < 100; i++) {
assertEquals(new VersionedObject<>("item" + i, i), q1.poll());
assertEquals(new VersionedObject<>("item" + i, i), q2.poll());
}
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueAdvancedTest method testTake.
@Test
public void testTake() {
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);
new Thread(() -> {
try {
Thread.sleep(3000);
for (int i = 0; i < 100; i++) {
if (q1.offer(new VersionedObject<>("item"))) {
offerLatch.countDown();
}
if (q2.offer(new VersionedObject<>("item"))) {
offerLatch.countDown();
}
}
} catch (InterruptedException e) {
LOG.info(e);
}
}).start();
assertOpenEventually(offerLatch);
ExecutorService es = Executors.newFixedThreadPool(50);
CountDownLatch latch = new CountDownLatch(200);
for (int i = 0; i < 100; i++) {
es.execute(() -> {
try {
if (new VersionedObject<>("item").equals(q1.take())) {
latch.countDown();
}
} catch (InterruptedException e) {
LOG.info(e);
}
});
es.execute(() -> {
try {
if (new VersionedObject<>("item").equals(q2.take())) {
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 QueueEvictionTest method testQueueEviction_whenTtlIsZero_thenListenersAreNeverthelessExecuted.
@Test
public void testQueueEviction_whenTtlIsZero_thenListenersAreNeverthelessExecuted() throws Exception {
String queueName = randomString();
Config config = smallInstanceConfig();
config.getQueueConfig("default").setPriorityComparatorClassName(comparatorClassName);
config.getQueueConfig(queueName).setEmptyQueueTtl(0);
HazelcastInstance hz = createHazelcastInstance(config);
final CountDownLatch latch = new CountDownLatch(2);
hz.addDistributedObjectListener(new DistributedObjectListener() {
public void distributedObjectCreated(DistributedObjectEvent event) {
latch.countDown();
}
public void distributedObjectDestroyed(DistributedObjectEvent event) {
latch.countDown();
}
});
IQueue<VersionedObject<String>> queue = hz.getQueue(queueName);
assertTrue(queue.offer(new VersionedObject<>("item")));
assertEquals(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 QueueEvictionTest method testQueueEviction_whenTtlIsSet_thenTakeThrowsException.
@Test
public void testQueueEviction_whenTtlIsSet_thenTakeThrowsException() throws Exception {
String queueName = randomString();
Config config = smallInstanceConfig();
config.getQueueConfig(queueName).setPriorityComparatorClassName(comparatorClassName).setEmptyQueueTtl(2);
HazelcastInstance hz = createHazelcastInstance(config);
IQueue<VersionedObject<String>> queue = hz.getQueue(queueName);
try {
assertTrue(queue.offer(new VersionedObject<>("item")));
assertEquals(new VersionedObject<>("item"), queue.poll());
queue.take();
fail();
} catch (DistributedObjectDestroyedException expected) {
ignore(expected);
}
assertEquals(0, queue.size());
}
Aggregations