use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.
the class TestSimple method testSimple.
@Test
public void testSimple() throws Exception {
final AtomicLong count = new AtomicLong(0L);
try (final NodeManager nm = new NodeManager();
final DefaultThreadingModel tm = new DefaultThreadingModel("TB", -1, 1)) {
final Node n = new Node.Builder("test-app").defaultRoutingStrategyId("net.dempsy.router.simple").receiver(new Dummy()).cluster("start").adaptor(new Adaptor() {
private Dispatcher disp;
boolean done = false;
@Override
public void stop() {
done = true;
}
@Override
public void start() {
try {
while (!done) {
disp.dispatch(new KeyedMessageWithType(Integer.valueOf(1), "Hello", "string"));
// This is here for when the Container has a max pending and it gets starved for CPU cycles
// in this particular test.
Thread.yield();
}
} catch (final InterruptedException ie) {
if (!done)
LOGGER.error("Interrupted but not stopping.");
}
}
@Override
public void setDispatcher(final Dispatcher dispatcher) {
this.disp = dispatcher;
}
}).cluster("mp").mp(new MessageProcessor(MpFactory.make(() -> new Mp() {
@Override
public KeyedMessageWithType[] handle(final KeyedMessage message) {
count.incrementAndGet();
return null;
}
}, "string"))).build();
nm.node(n).collaborator(new LocalClusterSessionFactory().createSession()).threadingModel(tm.start("nodeid"));
nm.start();
assertTrue(ConditionPoll.poll(o -> count.get() > 100000));
}
}
use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.
the class TestSimpleRoutingStrategy method testInboundWithOutbound.
@Test
public void testInboundWithOutbound() throws Exception {
final Manager<RoutingStrategy.Inbound> manager = new Manager<>(RoutingStrategy.Inbound.class);
try (final RoutingStrategy.Inbound ib = manager.getAssociatedInstance(SimpleRoutingStrategy.class.getPackage().getName())) {
assertNotNull(ib);
assertTrue(SimpleInboundSide.class.isAssignableFrom(ib.getClass()));
final ClusterId cid = new ClusterId("test", "test");
ib.setContainerDetails(cid, new ContainerAddress(new DummyNodeAddress("here"), 0), (l, m) -> {
});
ib.start(infra);
assertTrue(waitForReg(session));
try (final ClusterInfoSession ses2 = sessFact.createSession()) {
try (final RoutingStrategyManager obman = chain(new RoutingStrategyManager(), o -> o.start(makeInfra(ses2, sched)));
final RoutingStrategy.Factory obf = obman.getAssociatedInstance(SimpleRoutingStrategy.class.getPackage().getName())) {
obf.start(makeInfra(ses2, sched));
final RoutingStrategy.Router ob = obf.getStrategy(cid);
final KeyedMessageWithType km = new KeyedMessageWithType(null, null, "");
assertTrue(poll(o -> ob.selectDestinationForMessage(km) != null));
final ContainerAddress ca = ob.selectDestinationForMessage(km);
assertNotNull(ca);
assertEquals("here", ((DummyNodeAddress) ca.node).name);
// now distupt the session
session.close();
// the destination should clear until a new in runs
assertTrue(poll(o -> ob.selectDestinationForMessage(km) == null));
try (ClusterInfoSession ses3 = sessFact.createSession();
RoutingStrategy.Inbound ib2 = manager.getAssociatedInstance(SimpleRoutingStrategy.class.getPackage().getName())) {
ib2.setContainerDetails(cid, ca, (l, m) -> {
});
ib2.start(makeInfra(ses3, sched));
assertTrue(poll(o -> ob.selectDestinationForMessage(km) != null));
}
}
}
}
}
use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.
the class TestInstanceManager method testSingleInstanceTwoMessagesDifferentClassSeparateExecution.
@Test
public void testSingleInstanceTwoMessagesDifferentClassSeparateExecution() throws Exception {
final CombinedMP prototype = new CombinedMP();
try (final LockingContainer manager = setupContainer(new MessageProcessor<CombinedMP>(prototype))) {
final DummyDispatcher dispatcher = ((DummyDispatcher) manager.getDispatcher());
assertEquals("starts with no instances", 0, manager.getProcessorCount());
final KeyedMessageWithType message1 = km(new MessageOne(123));
final InstanceWrapper wrapper = manager.getInstanceForKey(message1.key, message1.message);
manager.dispatch(message1, Operation.handle, true);
final CombinedMP instance = (CombinedMP) wrapper.getInstance();
assertEquals("instance was created", 1, manager.getProcessorCount());
assertEquals("instance activated", 1, instance.activationCount);
assertTrue("real activation time", instance.activationTime > 0);
assertSame("instance received message", message1.message, instance.messages.get(0));
assertEquals("message count", 1, instance.messages.size());
assertTrue("activated before first message", instance.activationTime < instance.firstMessageTime);
assertEquals(new ReturnString("MessageOne"), dispatcher.lastDispatched.message);
final KeyedMessageWithType message2 = km(new MessageTwo(123));
assertSame("same wrapper returned for second message", wrapper, manager.getInstanceForKey(message2.key, message2.message));
manager.dispatch(message2, Operation.handle, true);
assertEquals("no other instance was created", 1, manager.getProcessorCount());
assertEquals("no second activation", 1, instance.activationCount);
assertEquals("both messages delivered", 2, instance.messages.size());
assertSame("message1 delivered first", message1.message, instance.messages.get(0));
assertSame("message2 delivered second", message2.message, instance.messages.get(1));
assertEquals(new ReturnString("MessageTwo"), dispatcher.lastDispatched.message);
}
}
use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.
the class TestInstanceManager method testOutputShortCircuitsIfNoOutputMethod.
@Test
public void testOutputShortCircuitsIfNoOutputMethod() throws Exception {
final CombinedMP prototype = new CombinedMP();
final Container manager = setupContainer(new MessageProcessor<CombinedMP>(prototype));
final DummyDispatcher dispatcher = ((DummyDispatcher) manager.getDispatcher());
// we need to dispatch messages to create MP instances
final KeyedMessageWithType message1 = km(new MessageOne(1));
final KeyedMessageWithType message2 = km(new MessageOne(2));
manager.dispatch(message1, Operation.handle, true);
manager.dispatch(message2, Operation.handle, true);
assertEquals(new ReturnString("MessageOne"), dispatcher.lastDispatched.message);
manager.invokeOutput();
// output messages are NOT considered "processed" if there is no output method on the MP.
assertEquals("number of processed messages should include outputs.", 2, ((ClusterMetricGetters) statsCollector).getProcessedMessageCount());
}
use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.
the class TestInstanceManager method testOutputCountsOkay.
@Test
public void testOutputCountsOkay() throws Exception {
final OutputTestMP prototype = new OutputTestMP();
try (final Container manager = setupContainer(new MessageProcessor<OutputTestMP>(prototype))) {
final DummyDispatcher dispatcher = ((DummyDispatcher) manager.getDispatcher());
// we need to dispatch messages to create MP instances
final KeyedMessageWithType message1 = km(new MessageOne(1));
final KeyedMessageWithType message2 = km(new MessageOne(2));
manager.dispatch(message1, Operation.handle, true);
manager.dispatch(message2, Operation.handle, true);
assertEquals(new ReturnString("MessageOne"), dispatcher.lastDispatched.message);
manager.invokeOutput();
assertEquals("number of processed messages should include outputs.", 4, ((ClusterMetricGetters) statsCollector).getProcessedMessageCount());
}
}
Aggregations