use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueTestsFrom2X method testInterruption.
@Test
@Ignore
public void testInterruption() throws Exception {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1);
HazelcastInstance instance = factory.newHazelcastInstance(getConfig());
IQueue<VersionedObject<String>> queue = instance.getQueue("testInterruption");
CountDownLatch latch = new CountDownLatch(1);
Thread thread = new Thread(() -> {
try {
queue.poll(5, TimeUnit.SECONDS);
fail();
} catch (InterruptedException e) {
latch.countDown();
}
});
thread.start();
sleep(2000);
thread.interrupt();
assertTrue(latch.await(100, TimeUnit.SECONDS));
queue.offer(new VersionedObject<>("message"));
assertEquals(1, queue.size());
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueTestsFrom2X method testListenerLifecycle.
@Test
public void testListenerLifecycle() throws Exception {
long sleep = 2000;
String name = "listenerLifecycle";
HazelcastInstance instance = createHazelcastInstance();
IQueue<VersionedObject<Integer>> queue = instance.getQueue(name);
try {
CountDownLatch latch = new CountDownLatch(3);
ItemListener<VersionedObject<Integer>> listener = new ItemListener<VersionedObject<Integer>>() {
public void itemAdded(ItemEvent<VersionedObject<Integer>> itemEvent) {
latch.countDown();
}
public void itemRemoved(ItemEvent<VersionedObject<Integer>> itemEvent) {
}
};
queue.addItemListener(listener, false);
queue.offer(new VersionedObject<>(1));
sleep(sleep);
queue.destroy();
queue = instance.getQueue(name);
UUID id = queue.addItemListener(listener, false);
queue.offer(new VersionedObject<>(2));
sleep(sleep);
queue.removeItemListener(id);
queue.offer(new VersionedObject<>(3));
sleep(sleep);
assertEquals(1, latch.getCount());
latch.countDown();
assertTrue("Remaining: " + latch.getCount(), latch.await(3, TimeUnit.SECONDS));
} finally {
queue.destroy();
}
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueAdvancedTest method testOfferLong.
@Test
public void testOfferLong() throws InterruptedException {
Config config = getConfig();
config.getQueueConfig("default").setMaxSize(200);
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance[] instances = factory.newInstances(config);
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));
}
assertFalse(q1.offer(new VersionedObject<>("item")));
assertFalse(q2.offer(new VersionedObject<>("item")));
assertFalse(q1.offer(new VersionedObject<>("item"), 2, SECONDS));
assertFalse(q2.offer(new VersionedObject<>("item"), 2, SECONDS));
CountDownLatch pollLatch = new CountDownLatch(200);
new Thread(() -> {
try {
Thread.sleep(3000);
for (int i = 0; i < 100; i++) {
if (new VersionedObject<>("item" + i, i).equals(q1.poll(2, SECONDS))) {
pollLatch.countDown();
}
if (new VersionedObject<>("item" + i, i).equals(q2.poll(2, SECONDS))) {
pollLatch.countDown();
}
}
} catch (InterruptedException e) {
LOG.info(e);
}
}).start();
assertOpenEventually(pollLatch);
ExecutorService es = Executors.newFixedThreadPool(50);
CountDownLatch latch = new CountDownLatch(200);
for (int i = 0; i < 100; i++) {
es.execute(() -> {
try {
if (q1.offer(new VersionedObject<>("item"), 30, SECONDS)) {
latch.countDown();
}
} catch (InterruptedException e) {
LOG.info(e);
}
});
es.execute(() -> {
try {
if (q2.offer(new VersionedObject<>("item"), 30, 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 QueueAdvancedTest method testDrainBackup.
@Test
public void testDrainBackup() {
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<>();
queue1.drainTo(list, 2);
instance1.shutdown();
assertSizeEventually(2, queue2);
assertIterableEquals(queue2, new VersionedObject<>("item2", 2), new VersionedObject<>("item3", 3));
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueAdvancedTest method queueEntriesShouldBeConsistentAfterShutdown.
@Test
public void queueEntriesShouldBeConsistentAfterShutdown() {
HazelcastInstance[] instances = createHazelcastInstanceFactory(2).newInstances(getConfig());
HazelcastInstance h1 = instances[0];
HazelcastInstance h2 = instances[1];
Queue<VersionedObject<String>> q1 = h1.getQueue("q");
Queue<VersionedObject<String>> q2 = h2.getQueue("q");
for (int i = 0; i < 5; i++) {
q1.offer(new VersionedObject<>("item" + i, i));
}
assertEquals(5, q1.size());
assertEquals(5, q2.size());
assertEquals(new VersionedObject<>("item0", 0), q2.poll());
assertEquals(new VersionedObject<>("item1", 1), q2.poll());
assertEquals(new VersionedObject<>("item2", 2), q2.poll());
assertSizeEventually(2, q1);
assertSizeEventually(2, q2);
h1.getLifecycleService().shutdown();
assertSizeEventually(2, q2);
}
Aggregations