use of net.dempsy.Infrastructure in project Dempsy by Dempsy.
the class TestManagedRoutingStrategy method testInboundWithOutbound.
@Test
public void testInboundWithOutbound() throws Exception {
final int numShardsToExpect = Integer.parseInt(Utils.DEFAULT_TOTAL_SHARDS);
final Manager<RoutingStrategy.Inbound> manager = new Manager<>(RoutingStrategy.Inbound.class);
try (final RoutingStrategy.Inbound ib = manager.getAssociatedInstance(ManagedInbound.class.getPackage().getName())) {
final ClusterId cid = setTestName("testInboundWithOutbound");
final ContainerAddress oca = new ContainerAddress(new DummyNodeAddress("here"), 0);
final Infrastructure infra = makeInfra(session, sched);
final Utils<ContainerAddress> msutils = new Utils<>(infra, cid.clusterName, oca);
ib.setContainerDetails(cid, oca, (l, m) -> {
});
ib.start(infra);
checkForShardDistribution(session, msutils, numShardsToExpect, 1);
try (final ClusterInfoSession ses2 = sessFact.createSession();
final RoutingStrategyManager obman = chain(new RoutingStrategyManager(), o -> o.start(makeInfra(ses2, sched)));
final RoutingStrategy.Factory obf = obman.getAssociatedInstance(ManagedInbound.class.getPackage().getName())) {
obf.start(makeInfra(ses2, sched));
assertTrue(poll(o -> obf.isReady()));
final RoutingStrategy.Router ob = obf.getStrategy(cid);
assertTrue(poll(o -> obf.isReady()));
final KeyedMessageWithType km = new KeyedMessageWithType(new Object(), new Object(), "");
assertTrue(poll(o -> ob.selectDestinationForMessage(km) != null));
final ContainerAddress ca = ob.selectDestinationForMessage(km);
assertNotNull(ca);
assertEquals("here", ((DummyNodeAddress) ca.node).name);
// now disrupt the session
session.close();
try (ClusterInfoSession ses3 = sessFact.createSession();
RoutingStrategy.Inbound ib2 = manager.getAssociatedInstance(ManagedInbound.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.Infrastructure in project Dempsy by Dempsy.
the class TestManagedRoutingStrategy method testInboundResillience.
@Test
public void testInboundResillience() throws Exception {
final int numShardsToExpect = Integer.parseInt(Utils.DEFAULT_TOTAL_SHARDS);
final Manager<RoutingStrategy.Inbound> manager = new Manager<>(RoutingStrategy.Inbound.class);
try (final RoutingStrategy.Inbound ib = manager.getAssociatedInstance(ManagedInbound.class.getPackage().getName())) {
final ClusterId clusterId = super.setTestName("testInboundResillience");
final ContainerAddress ca = new ContainerAddress(new DummyNodeAddress("theOnlyNode"), 0);
final Infrastructure infra = makeInfra(session, sched);
final Utils<ContainerAddress> msutils = new Utils<>(infra, clusterId.clusterName, ca);
ib.setContainerDetails(clusterId, ca, (l, m) -> {
});
ib.start(infra);
checkForShardDistribution(session, msutils, numShardsToExpect, 1);
disruptor.accept(session);
checkForShardDistribution(session, msutils, numShardsToExpect, 1);
}
}
use of net.dempsy.Infrastructure in project Dempsy by Dempsy.
the class TcpTransportTest method testMessage.
@Test
public void testMessage() throws Exception {
try (ServiceTracker tr = new ServiceTracker()) {
final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).setNumHandlers(2).setUseLocalHost(true);
final ThreadingModel tm = tr.track(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + ".testMessage"));
final Infrastructure infra = tr.track(new TestInfrastructure(tm));
final TcpAddress addr = r.getAddress(infra);
LOGGER.debug(addr.toString());
final AtomicReference<RoutedMessage> rm = new AtomicReference<>(null);
r.start((Listener<RoutedMessage>) msg -> {
rm.set(msg);
return true;
}, infra);
try (final SenderFactory sf = senderFactory.get()) {
sf.start(new TestInfrastructure(tm) {
@Override
public String getNodeId() {
return "test";
}
});
final Sender sender = sf.getSender(addr);
sender.send(new RoutedMessage(new int[] { 0 }, "Hello", "Hello"));
assertTrue(poll(o -> rm.get() != null));
assertEquals("Hello", rm.get().message);
}
}
}
use of net.dempsy.Infrastructure in project Dempsy by Dempsy.
the class TcpTransportTest method testConnectionRecovery.
@Test
public void testConnectionRecovery() throws Exception {
try (final ServiceTracker tr = new ServiceTracker()) {
final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).setNumHandlers(2).setUseLocalHost(true);
// can't test connection recovery here.
if (!(r instanceof DisruptableRecevier))
return;
final ThreadingModel tm = tr.track(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + ".testConnectionRecovery"));
final Infrastructure infra = tr.track(new TestInfrastructure(tm));
final TcpAddress addr = r.getAddress(infra);
LOGGER.debug(addr.toString());
final AtomicReference<RoutedMessage> rm = new AtomicReference<>(null);
r.start((Listener<RoutedMessage>) msg -> {
rm.set(msg);
return true;
}, infra);
try (final SenderFactory sf = senderFactory.get()) {
sf.start(new TestInfrastructure(tm) {
@Override
public String getNodeId() {
return "test";
}
});
final Sender sender = sf.getSender(addr);
sender.send(new RoutedMessage(new int[] { 0 }, "Hello", "Hello"));
assertTrue(poll(o -> rm.get() != null));
assertEquals("Hello", rm.get().message);
assertTrue(((DisruptableRecevier) r).disrupt(addr));
final AtomicBoolean stop = new AtomicBoolean(false);
final RoutedMessage resetMessage = new RoutedMessage(new int[] { 0 }, "RESET", "RESET");
final Thread senderThread = new Thread(() -> {
while (!stop.get()) {
sender.send(resetMessage);
dontInterrupt(() -> Thread.sleep(100));
}
}, "testConnectionRecovery-sender");
senderThread.start();
try {
assertTrue(poll(o -> "RESET".equals(rm.get().message)));
} finally {
stop.set(true);
if (!poll(senderThread, t -> {
dontInterrupt(() -> t.join(10000));
return !t.isAlive();
}))
LOGGER.error("FAILED TO SHUT DOWN TEST SENDING THREAD. THREAD LEAK!");
}
}
}
}
use of net.dempsy.Infrastructure in project Dempsy by Dempsy.
the class TestGroupRoutingStrategy method testInboundWithOutbound.
@Test
public void testInboundWithOutbound() throws Exception {
final int numShardsToExpect = Integer.parseInt(Utils.DEFAULT_TOTAL_SHARDS);
final String groupName = "testInboundWithOutbound";
final Manager<RoutingStrategy.Inbound> manager = new RoutingInboundManager();
try (final RoutingStrategy.Inbound ib = manager.getAssociatedInstance(ClusterGroupInbound.class.getPackage().getName() + ":" + groupName)) {
final ClusterId cid = setTestName("testInboundWithOutbound");
final NodeAddress na = new DummyNodeAddress("here");
final ContainerAddress oca = new ContainerAddress(na, 0);
final Infrastructure infra = makeInfra(session, sched);
final GroupDetails ogd = new GroupDetails(groupName, na);
final Map<String, ContainerAddress> ocaiByCluster = new HashMap<>();
ocaiByCluster.put(cid.clusterName, oca);
ogd.fillout(ocaiByCluster);
final Utils<GroupDetails> msutils = new Utils<>(infra, groupName, ogd);
ib.setContainerDetails(cid, oca, (l, m) -> {
});
ib.start(infra);
checkForShardDistribution(session, msutils, numShardsToExpect, 1);
try (final ClusterInfoSession ses2 = sessFact.createSession();
final RoutingStrategyManager obman = chain(new RoutingStrategyManager(), o -> o.start(makeInfra(ses2, sched)));
final RoutingStrategy.Factory obf = obman.getAssociatedInstance(ClusterGroupInbound.class.getPackage().getName() + ":" + groupName)) {
obf.start(makeInfra(ses2, sched));
assertTrue(poll(o -> obf.isReady()));
final RoutingStrategy.Router ob = obf.getStrategy(cid);
assertTrue(poll(o -> obf.isReady()));
final KeyedMessageWithType km = new KeyedMessageWithType(new Object(), new Object(), "");
assertTrue(poll(o -> ob.selectDestinationForMessage(km) != null));
final ContainerAddress ca = ob.selectDestinationForMessage(km);
assertNotNull(ca);
assertEquals("here", ((DummyNodeAddress) ca.node).name);
// now disrupt the session
session.close();
// the destination should clear until a new one runs
// NO: destination will not necessarily clear.
// poll(o -> ob.selectDestinationForMessage(km) == null);
final ContainerAddress nca = new ContainerAddress(new DummyNodeAddress("here-again"), 0);
ogd.fillout(ocaiByCluster);
try (ClusterInfoSession ses3 = sessFact.createSession();
RoutingStrategy.Inbound ib2 = new RoutingInboundManager().getAssociatedInstance(ClusterGroupInbound.class.getPackage().getName() + ":" + groupName)) {
ib2.setContainerDetails(cid, nca, (l, m) -> {
});
ib2.start(makeInfra(ses3, sched));
assertTrue(poll(o -> ob.selectDestinationForMessage(km) != null));
}
}
}
}
Aggregations