use of net.dempsy.lifecycle.annotation.Output in project Dempsy by Dempsy.
the class TestContainer method addOutputCatchStage.
public NodeManager addOutputCatchStage() throws InterruptedException {
// =======================================================
// configure an output catcher tier
final Node out = new Node.Builder("test-app").defaultRoutingStrategyId("net.dempsy.router.simple").receiver(new BlockingQueueReceiver(new ArrayBlockingQueue<>(16))).nodeStatsCollector(new BasicNodeStatsCollector()).cluster("output-catch").mp(new MessageProcessor<OutputCatcher>(new OutputCatcher())).build();
out.validate();
final NodeManager nman = track(new NodeManager()).node(out).collaborator(track(sessionFactory.createSession())).start();
// wait until we can actually reach the output-catch cluster from the main node
assertTrue(poll(o -> {
try {
return canReach(getRouter(manager), "output-catch", new KeyExtractor().extract(new OutputMessage("foo", 1, 1)).iterator().next());
} catch (final Exception e) {
return false;
}
}));
// =======================================================
return nman;
}
use of net.dempsy.lifecycle.annotation.Output in project Dempsy by Dempsy.
the class TestContainer method testInvokeOutput.
@Test
public void testInvokeOutput() throws Exception {
outputMessages = Collections.newSetFromMap(new ConcurrentHashMap<>());
cache = new ConcurrentHashMap<>();
final TestAdaptor adaptor = context.getBean(TestAdaptor.class);
assertNotNull(adaptor.dispatcher);
adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("foo"));
adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("bar"));
assertTrue(poll(container, c -> (c.getProcessorCount() + ((ClusterMetricGetters) c.statCollector).getMessageDiscardedCount()) > 1));
Thread.sleep(100);
assertEquals("number of MP instances", 2, container.getProcessorCount());
try (NodeManager nman = addOutputCatchStage()) {
final TestProcessor mp = cache.get("foo");
assertTrue(poll(mp, m -> mp.invocationCount > 0));
Thread.sleep(100);
assertEquals("invocation count, 1st message", 1, mp.invocationCount);
// because the sessionFactory is shared and the appname is the same, we should be in the same app
container.outputPass();
assertTrue(poll(outputMessages, o -> o.size() > 1));
Thread.sleep(100);
assertEquals(2, outputMessages.size());
// no new mps created in the first one
assertEquals("did not create MP", 2, container.getProcessorCount());
// but the invocation count should have increased since the output cycles feeds messages back to this cluster
assertTrue(poll(mp, m -> mp.invocationCount > 1));
Thread.sleep(100);
assertEquals("invocation count, 1st message", 2, mp.invocationCount);
// // order of messages is not guaranteed, so we need to aggregate keys
final HashSet<String> messageKeys = new HashSet<String>();
final Iterator<OutputMessage> iter = outputMessages.iterator();
messageKeys.add(iter.next().getKey());
messageKeys.add(iter.next().getKey());
assertTrue("first MP sent output", messageKeys.contains("foo"));
assertTrue("second MP sent output", messageKeys.contains("bar"));
}
}
Aggregations