use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueTestsFrom2X method testQueueRemove.
@Test
public void testQueueRemove() {
HazelcastInstance instance = createHazelcastInstance();
IQueue<VersionedObject<String>> q = instance.getQueue("testQueueRemove");
for (int i = 0; i < 10; i++) {
q.offer(new VersionedObject<>("item" + i, i));
}
for (int i = 0; i < 5; i++) {
assertNotNull(q.poll());
}
assertEquals(new VersionedObject<>("item5", 5), q.peek());
boolean removed = q.remove(new VersionedObject<>("item5", 5));
assertTrue(removed);
Iterator<VersionedObject<String>> it = q.iterator();
int i = 6;
while (it.hasNext()) {
int itemId = i++;
assertEquals(new VersionedObject<>("item" + itemId, itemId), it.next());
}
assertEquals(4, q.size());
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueTestsFrom2X method issue391.
@Test
public void issue391() throws Exception {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
int total = 10;
Collection<VersionedObject<Integer>> results = new ArrayList<>(5);
HazelcastInstance hz1 = factory.newHazelcastInstance(getConfig());
CountDownLatch latchOffer = new CountDownLatch(1);
CountDownLatch latchTake = new CountDownLatch(1);
spawn(() -> {
try {
IQueue<VersionedObject<Integer>> q = hz1.getQueue("q");
for (int i = 0; i < total; i++) {
results.add(q.take());
}
latchTake.countDown();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
HazelcastInstance hz2 = factory.newHazelcastInstance(getConfig());
waitAllForSafeState(hz1, hz2);
spawn(() -> {
for (int i = 0; i < total; i++) {
hz2.getQueue("q").offer(new VersionedObject<>(i, i));
}
latchOffer.countDown();
});
assertTrue(latchOffer.await(100, TimeUnit.SECONDS));
assertTrue(latchTake.await(10, TimeUnit.SECONDS));
assertTrue(hz1.getQueue("q").isEmpty());
hz1.shutdown();
assertTrue(hz2.getQueue("q").isEmpty());
@SuppressWarnings("unchecked") VersionedObject<Integer>[] objects = new VersionedObject[total];
for (int i = 0; i < total; i++) {
objects[i] = new VersionedObject<>(i, i);
}
assertArrayEquals(objects, results.toArray());
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueTestsFrom2X method issue427QOfferIncorrectWithinTransaction.
@Test
public void issue427QOfferIncorrectWithinTransaction() {
Config config = getConfig();
config.getQueueConfig("default").setMaxSize(100);
HazelcastInstance instance = createHazelcastInstance(config);
TransactionContext transactionContext = instance.newTransactionContext();
transactionContext.beginTransaction();
TransactionalQueue<VersionedObject<Integer>> queue = transactionContext.getQueue("default");
for (int i = 0; i < 100; i++) {
queue.offer(new VersionedObject<>(i, i));
}
boolean result = queue.offer(new VersionedObject<>(100, 100));
assertEquals(100, queue.size());
transactionContext.commitTransaction();
assertEquals(100, instance.getQueue("default").size());
assertFalse(result);
instance.shutdown();
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueTestsFrom2X method testQueueContains.
@Test
public void testQueueContains() {
HazelcastInstance instance = createHazelcastInstance();
IQueue<VersionedObject<String>> queue = instance.getQueue("testQueueContains");
@SuppressWarnings("unchecked") VersionedObject<String>[] items = new VersionedObject[] { new VersionedObject<>("one"), new VersionedObject<>("two"), new VersionedObject<>("three"), new VersionedObject<>("four") };
queue.addAll(asList(items));
assertContains(queue, new VersionedObject<>("one"));
assertContains(queue, new VersionedObject<>("two"));
assertContains(queue, new VersionedObject<>("three"));
assertContains(queue, new VersionedObject<>("four"));
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class QueueTestsFrom2X method issue114TestQueueListenersUnderTransaction.
/**
* Github issue #114
*/
@Test
public void issue114TestQueueListenersUnderTransaction() throws Exception {
String name = "issue99TestQueueTakeAndDuringRollback";
HazelcastInstance hz = createHazelcastInstance();
IQueue<VersionedObject<String>> testQueue = hz.getQueue(name);
CountDownLatch offerLatch = new CountDownLatch(2);
CountDownLatch pollLatch = new CountDownLatch(2);
testQueue.addItemListener(new ItemListener<VersionedObject<String>>() {
public void itemAdded(ItemEvent<VersionedObject<String>> item) {
offerLatch.countDown();
}
public void itemRemoved(ItemEvent<VersionedObject<String>> item) {
pollLatch.countDown();
}
}, true);
TransactionContext context = hz.newTransactionContext();
context.beginTransaction();
TransactionalQueue<VersionedObject<String>> queue = context.getQueue(name);
queue.offer(new VersionedObject<>("tx Hello"));
queue.offer(new VersionedObject<>("tx World"));
context.commitTransaction();
TransactionContext context2 = hz.newTransactionContext();
context2.beginTransaction();
TransactionalQueue<VersionedObject<String>> queue2 = context2.getQueue(name);
assertEquals(new VersionedObject<>("tx Hello"), queue2.poll());
assertEquals(new VersionedObject<>("tx World"), queue2.poll());
context2.commitTransaction();
assertTrue("Remaining offer listener count: " + offerLatch.getCount(), offerLatch.await(2, TimeUnit.SECONDS));
assertTrue("Remaining poll listener count: " + pollLatch.getCount(), pollLatch.await(2, TimeUnit.SECONDS));
}
Aggregations