use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.
the class QueueAdvancedTest method testDeadTaker.
/**
* Test for issue 730 (Google).
*/
@Test
public void testDeadTaker() throws Exception {
Config config = new Config();
final CountDownLatch shutdownLatch = new CountDownLatch(1);
config.addListenerConfig(new ListenerConfig().setImplementation(new MembershipListener() {
@Override
public void memberAdded(MembershipEvent membershipEvent) {
}
@Override
public void memberRemoved(MembershipEvent membershipEvent) {
shutdownLatch.countDown();
}
@Override
public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
}
}));
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance[] instances = factory.newInstances(config);
final HazelcastInstance h1 = instances[0];
final HazelcastInstance h2 = instances[1];
warmUpPartitions(h1, h2);
final IQueue<String> q1 = h1.getQueue("default");
final IQueue<String> q2 = h2.getQueue("default");
final CountDownLatch startLatch = new CountDownLatch(1);
new Thread(new Runnable() {
@Override
public void run() {
try {
assertTrue("Expected startLatch.await() to succeed within 10 seconds", startLatch.await(10, SECONDS));
Thread.sleep(5000);
h2.getLifecycleService().terminate();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
startLatch.countDown();
String value = q2.take();
fail("Should not be able to take value from queue, but got: " + value);
} catch (HazelcastInstanceNotActiveException e) {
EmptyStatement.ignore(e);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
assertTrue("Expected shutdownLatch.await() to succeed within 1 minute", shutdownLatch.await(1, MINUTES));
q1.offer("item");
assertEquals(1, q1.size());
assertEquals("item", q1.poll());
}
use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.
the class QueueAdvancedTest method testOffer.
@Test
public void testOffer() throws Exception {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance[] instances = factory.newInstances();
HazelcastInstance h1 = instances[0];
HazelcastInstance h2 = instances[1];
IQueue<String> q1 = h1.getQueue("default");
IQueue<String> q2 = h2.getQueue("default");
for (int i = 0; i < 100; i++) {
assertTrue("Expected q1.offer() to succeed", q1.offer("item" + i, 100, SECONDS));
assertTrue("Expected q2.offer() to succeed", q2.offer("item" + i, 100, SECONDS));
}
assertEquals("item0", q1.peek());
assertEquals("item0", q2.peek());
for (int i = 0; i < 100; i++) {
assertEquals("item" + i, q1.poll());
assertEquals("item" + i, q2.poll());
}
}
use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.
the class QueueListenerTest method testItemListener_addedToQueueConfig_Issue366.
@Test
public void testItemListener_addedToQueueConfig_Issue366() throws Exception {
itemListenerConfig.setImplementation(countdownItemListener);
itemListenerConfig.setIncludeValue(true);
queueConfig.setName(QUEUE_NAME);
queueConfig.addItemListenerConfig(itemListenerConfig);
config.addQueueConfig(queueConfig);
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance instance = factory.newHazelcastInstance(config);
IQueue<Integer> queue = instance.getQueue(QUEUE_NAME);
for (int i = 0; i < TOTAL_QUEUE_PUT / 2; i++) {
queue.put(i);
}
factory.newHazelcastInstance(config);
for (int i = 0; i < TOTAL_QUEUE_PUT / 4; i++) {
queue.put(i);
}
assertTrue(countdownItemListener.added.await(3, TimeUnit.SECONDS));
}
use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.
the class ListListenerTest method testListenerRemove.
@Test
public void testListenerRemove() throws Exception {
final String name = randomString();
final int count = 10;
final int insCount = 4;
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(insCount);
final HazelcastInstance[] instances = factory.newInstances();
ListenerTest listener = new ListenerTest(count);
IList list = getList(instances, name);
list.addItemListener(listener, true);
for (int i = 0; i < count; i++) {
list.add("item" + i);
}
for (int i = count - 1; i >= 0; i--) {
list.remove(i);
}
assertTrue(listener.latchAdd.await(5, TimeUnit.SECONDS));
assertTrue(listener.latchRemove.await(5, TimeUnit.SECONDS));
}
use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.
the class QueueAdvancedTest method testQueueAfterShutdown.
/**
* Test case for issue 289.
* <p/>
* 1. Create HazelcastInstance h1 and h2, then get a queue from each (same queue name)
* 2. Put a message on queue from h2
* 3. Take a message off on queue from h1
* 4. Shutdown h1, then check if queue is still empty on h2
*/
@Test
public void testQueueAfterShutdown() throws Exception {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance[] instances = factory.newInstances();
HazelcastInstance h1 = instances[0];
HazelcastInstance h2 = instances[1];
IQueue<String> q1 = h1.getQueue("default");
IQueue<String> q2 = h2.getQueue("default");
q2.offer("item");
assertEquals(1, q1.size());
assertEquals(1, q2.size());
assertEquals("item", q1.take());
assertEquals(0, q1.size());
assertEquals(0, q2.size());
h1.getLifecycleService().shutdown();
assertEquals(0, q2.size());
}
Aggregations