use of com.nokia.dempsy.messagetransport.blockingqueue.BlockingQueueAdaptor in project Dempsy by Dempsy.
the class TestMpContainer method testEvictCollision.
@Test
public void testEvictCollision() throws Throwable {
// This forces the instantiation of an Mp
BlockingQueueAdaptor adaptor = context.getBean(BlockingQueueAdaptor.class);
assertNotNull(adaptor);
adaptor.setFailFast(true);
inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
// Once the poll finishes the Mp is instantiated and handling messages.
assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
assertEquals("did not create MP", 1, container.getProcessorCount());
TestProcessor mp = (TestProcessor) container.getMessageProcessor("foo");
assertNotNull("MP not associated with expected key", mp);
assertEquals("activation count, 1st message", 1, mp.activationCount);
assertEquals("invocation count, 1st message", 1, mp.invocationCount);
// now we're going to cause the passivate to be held up.
mp.blockPassivate = new CountDownLatch(1);
// allow eviction
mp.evict.set(true);
// now kick off the evict in a separate thread since we expect it to hang
// until the mp becomes unstuck.
// this will allow us to see the evict pass complete
final AtomicBoolean evictIsComplete = new AtomicBoolean(false);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
container.evict();
evictIsComplete.set(true);
}
});
thread.start();
//let it get going.
Thread.sleep(50);
// check to see we're hung.
assertFalse(evictIsComplete.get());
final MetricGetters sc = (MetricGetters) container.getStatsCollector();
assertEquals(0, sc.getMessageCollisionCount());
// sending it a message will now cause it to have the collision tick up
inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
assertTrue(TestUtils.poll(baseTimeoutMillis, sc, new TestUtils.Condition<MetricGetters>() {
@Override
public boolean conditionMet(MetricGetters o) {
return o.getMessageCollisionCount() == 1;
}
}));
// now let the evict finish
mp.blockPassivate.countDown();
// wait until the eviction completes
assertTrue(TestUtils.poll(baseTimeoutMillis, evictIsComplete, new TestUtils.Condition<AtomicBoolean>() {
@Override
public boolean conditionMet(AtomicBoolean o) {
return o.get();
}
}));
// invocationCount should still be 1 from the initial invocation that caused the clone
assertEquals(1, mp.invocationCount);
// send a message that should go through
inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
// Once the poll finishes a new Mp is instantiated and handling messages.
assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
TestProcessor mp2 = (TestProcessor) container.getMessageProcessor("foo");
// make sure this new Mp isn't the old one.
assertTrue(mp != mp2);
}
use of com.nokia.dempsy.messagetransport.blockingqueue.BlockingQueueAdaptor in project Dempsy by Dempsy.
the class TestMpContainer method testEvictCollisionWithBlocking.
@Test
public void testEvictCollisionWithBlocking() throws Throwable {
// for this test we need to undo the setup
tearDown();
// and re-setUp setting failFast to false.
setUp("false");
// This forces the instantiation of an Mp
BlockingQueueAdaptor adaptor = context.getBean(BlockingQueueAdaptor.class);
assertNotNull(adaptor);
inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
// Once the poll finishes the Mp is instantiated and handling messages.
assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
assertEquals("did not create MP", 1, container.getProcessorCount());
TestProcessor mp = (TestProcessor) container.getMessageProcessor("foo");
assertNotNull("MP not associated with expected key", mp);
assertEquals("activation count, 1st message", 1, mp.activationCount);
assertEquals("invocation count, 1st message", 1, mp.invocationCount);
// now we're going to cause the passivate to be held up.
mp.blockPassivate = new CountDownLatch(1);
// allow eviction
mp.evict.set(true);
// now kick off the evict in a separate thread since we expect it to hang
// until the mp becomes unstuck.
// this will allow us to see the evict pass complete
final AtomicBoolean evictIsComplete = new AtomicBoolean(false);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
container.evict();
evictIsComplete.set(true);
}
});
thread.start();
//let it get going.
Thread.sleep(500);
// check to see we're hung.
assertFalse(evictIsComplete.get());
final MetricGetters sc = (MetricGetters) container.getStatsCollector();
assertEquals(0, sc.getMessageCollisionCount());
// sending it a message will now cause it to have the collision tick up
inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
// give it some time.
Thread.sleep(100);
// make sure there's no collision
assertEquals(0, sc.getMessageCollisionCount());
// make sure the message didn't get through
assertNull(outputQueue.peek());
// make sure no message got handled
// 1 is the initial invocation that caused the instantiation.
assertEquals(1, mp.invocationCount);
// now let the evict finish
mp.blockPassivate.countDown();
// wait until the eviction completes
assertTrue(TestUtils.poll(baseTimeoutMillis, evictIsComplete, new TestUtils.Condition<AtomicBoolean>() {
@Override
public boolean conditionMet(AtomicBoolean o) {
return o.get();
}
}));
// Once the poll finishes a new Mp is instantiated and handling messages.
assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
// get the new Mp
TestProcessor mp2 = (TestProcessor) container.getMessageProcessor("foo");
// invocationCount should be 1 from the initial invocation that caused the clone, and no more
assertEquals(1, mp.invocationCount);
assertEquals(1, mp2.invocationCount);
assertTrue(mp != mp2);
// send a message that should go through
inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
// Once the poll finishes mp2 invocationCount should be incremented
assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
assertEquals(1, mp.invocationCount);
assertEquals(2, mp2.invocationCount);
}
Aggregations