use of com.nokia.dempsy.container.mocks.ContainerTestMessage 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.container.mocks.ContainerTestMessage in project Dempsy by Dempsy.
the class TestMpContainer method testInvokeOutput.
@Test
public void testInvokeOutput() throws Exception {
inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
outputQueue.poll(1000, TimeUnit.MILLISECONDS);
inputQueue.add(serializer.serialize(new ContainerTestMessage("bar")));
outputQueue.poll(1000, TimeUnit.MILLISECONDS);
assertEquals("number of MP instances", 2, container.getProcessorCount());
assertTrue("queue is empty", outputQueue.isEmpty());
container.outputPass();
OutputMessage out1 = (OutputMessage) serializer.deserialize((byte[]) outputQueue.poll(1000, TimeUnit.MILLISECONDS));
OutputMessage out2 = (OutputMessage) serializer.deserialize((byte[]) outputQueue.poll(1000, TimeUnit.MILLISECONDS));
assertTrue("messages received", (out1 != null) && (out2 != null));
assertEquals("no more messages in queue", 0, outputQueue.size());
// order of messages is not guaranteed, so we need to aggregate keys
HashSet<String> messageKeys = new HashSet<String>();
messageKeys.add(out1.getKey());
messageKeys.add(out2.getKey());
assertTrue("first MP sent output", messageKeys.contains("foo"));
assertTrue("second MP sent output", messageKeys.contains("bar"));
}
use of com.nokia.dempsy.container.mocks.ContainerTestMessage in project Dempsy by Dempsy.
the class TestMpContainer method testEvictableWithBusyMp.
@Test
public void testEvictableWithBusyMp() throws Throwable {
// first set the receiver to failFast
// This forces the instantiation of an Mp
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 processing to be held up.
mp.latch = new CountDownLatch(1);
// allow eviction
mp.evict.set(true);
// sending it a message will now cause it to hang up while processing
inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
TestProcessor prototype = context.getBean(TestProcessor.class);
// keep track of the cloneCount for later checking
int tmpCloneCount = prototype.cloneCount.intValue();
// invocation count should go to 2
assertTrue(TestUtils.poll(baseTimeoutMillis, mp, new TestUtils.Condition<TestProcessor>() {
@Override
public boolean conditionMet(TestProcessor o) {
return o.invocationCount == 2;
}
}));
// this is a double check that no message got all the way
assertNull(outputQueue.peek());
// 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();
// now check to make sure eviction doesn't complete.
// just a little to give any mistakes a change to work themselves through
Thread.sleep(50);
// make sure eviction didn't finish
assertFalse(evictIsComplete.get());
// this lets it go
mp.latch.countDown();
// wait until the eviction completes
assertTrue(TestUtils.poll(baseTimeoutMillis, evictIsComplete, new TestUtils.Condition<AtomicBoolean>() {
@Override
public boolean conditionMet(AtomicBoolean o) {
return o.get();
}
}));
// now it comes through.
assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
assertEquals("activation count, 2nd message", 1, mp.activationCount);
assertEquals("invocation count, 2nd message", 2, mp.invocationCount);
inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
assertEquals("Clone count, 2nd message", tmpCloneCount + 1, prototype.cloneCount.intValue());
}
use of com.nokia.dempsy.container.mocks.ContainerTestMessage in project Dempsy by Dempsy.
the class TestMpContainer method testOutputInvoker.
@Test
public void testOutputInvoker() throws Exception {
inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
ContainerTestMessage out1 = (ContainerTestMessage) serializer.deserialize((byte[]) outputQueue.poll(1000, TimeUnit.MILLISECONDS));
assertTrue("messages received", (out1 != null));
assertEquals("number of MP instances", 1, container.getProcessorCount());
assertTrue("queue is empty", outputQueue.isEmpty());
}
use of com.nokia.dempsy.container.mocks.ContainerTestMessage 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