use of net.dempsy.Infrastructure in project Dempsy by Dempsy.
the class TcpTransportTest method runMultiMessage.
private void runMultiMessage(final String testName, final int numThreads, final int numMessagePerThread, final String message, final Serializer serializer) throws Exception {
try (final ServiceTracker tr = new ServiceTracker()) {
final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).numHandlers(2).useLocalHost(true).maxMessageSize(1024 * 1024 * 1024);
final ThreadingModel tm = tr.track(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + "." + testName));
final Infrastructure infra = tr.track(new TestInfrastructure(tm));
final TcpAddress addr = r.getAddress(infra);
LOGGER.debug(addr.toString());
final AtomicLong msgCount = new AtomicLong();
r.start((Listener<RoutedMessage>) msg -> {
msgCount.incrementAndGet();
return true;
}, infra);
final AtomicBoolean letMeGo = new AtomicBoolean(false);
final CountDownLatch waitToExit = new CountDownLatch(1);
final List<Thread> threads = IntStream.range(0, numThreads).mapToObj(threadNum -> new Thread(() -> {
try (final SenderFactory sf = senderFactory.get()) {
sf.start(new TestInfrastructure(null, null) {
@Override
public Map<String, String> getConfiguration() {
final Map<String, String> ret = new HashMap<>();
ret.put(sf.getClass().getPackage().getName() + "." + NioSenderFactory.CONFIG_KEY_SENDER_THREADS, NUM_SENDER_THREADS);
return ret;
}
});
final Sender sender = sf.getSender(addr);
while (!letMeGo.get()) Thread.yield();
try {
for (int i = 0; i < numMessagePerThread; i++) sender.send(new RoutedMessage(new int[] { 0 }, "Hello", message));
} catch (final InterruptedException ie) {
LOGGER.error("Interrupted in send.");
}
try {
waitToExit.await();
} catch (final InterruptedException ie) {
}
}
}, "testMultiMessage-Sender-" + threadNum)).map(th -> chain(th, t -> t.start())).collect(Collectors.toList());
Thread.sleep(10);
// here's we go.
letMeGo.set(true);
// the total number of messages sent should be this count.
assertTrue(poll(Long.valueOf((long) numThreads * (long) numMessagePerThread), v -> v.longValue() == msgCount.get()));
// let the threads exit
waitToExit.countDown();
// all threads should eventually exit.
assertTrue(poll(threads, o -> o.stream().filter(t -> t.isAlive()).count() == 0));
}
}
use of net.dempsy.Infrastructure in project Dempsy by Dempsy.
the class TcpTransportTest method testReceiverStartOnSpecifiedIf.
@Test
public void testReceiverStartOnSpecifiedIf() throws Exception {
final List<NetworkInterface> ifs = Collections.list(TcpUtils.getInterfaces(null)).stream().filter(nif -> !uncheck(() -> nif.isLoopback())).filter(nif -> nif.inetAddresses().filter(ia -> ia instanceof Inet4Address).anyMatch(ia -> uncheck(() -> ia.isReachable(100)))).collect(Collectors.toList());
final NetworkInterface nif = (ifs.size() > 1) ? ifs.get(1) : ((ifs.size() == 1) ? ifs.get(0) : null);
if (nif != null) {
// otherwise we can do no testing.
final String ifname = nif.getDisplayName();
if (Collections.list(nif.getInetAddresses()).size() > 0) {
// otherwise, we still can't really do anything without a lot of work
final AtomicBoolean resolverCalled = new AtomicBoolean(false);
try (ServiceTracker tr = new ServiceTracker()) {
final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).resolver(a -> {
resolverCalled.set(true);
return a;
}).numHandlers(2).useLocalHost(false);
final Infrastructure infra = tr.track(new TestInfrastructure(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + ".testReceiverStart")) {
@Override
public Map<String, String> getConfiguration() {
final HashMap<String, String> config = new HashMap<>();
config.put(NioReceiver.class.getPackage().getName() + "." + NioReceiver.CONFIG_KEY_RECEIVER_NETWORK_IF_NAME, ifname);
return config;
}
});
final TcpAddress addr = r.getAddress(infra);
assertTrue(Collections.list(nif.getInetAddresses()).contains(addr.inetAddress));
LOGGER.debug(addr.toString());
r.start(null, infra);
assertTrue(resolverCalled.get());
}
}
}
}
use of net.dempsy.Infrastructure in project Dempsy by Dempsy.
the class TcpTransportTest method testReceiverStart.
@Test
public void testReceiverStart() throws Exception {
final AtomicBoolean resolverCalled = new AtomicBoolean(false);
try (ServiceTracker tr = new ServiceTracker()) {
final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).resolver(a -> {
resolverCalled.set(true);
return a;
}).numHandlers(2).useLocalHost(true);
final Infrastructure infra = tr.track(new TestInfrastructure(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + ".testReceiverStart")));
final TcpAddress addr = r.getAddress(infra);
LOGGER.debug(addr.toString());
r.start(null, infra);
assertTrue(resolverCalled.get());
}
}
use of net.dempsy.Infrastructure in project Dempsy by Dempsy.
the class TcpTransportTest method testLargeMessage.
@Test
public void testLargeMessage() throws Exception {
final String huge = TestWordCount.readBible();
try (final ServiceTracker tr = new ServiceTracker()) {
final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).numHandlers(2).useLocalHost(true).maxMessageSize(1024 * 1024 * 1024);
final ThreadingModel tm = tr.track(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + ".testLargeMessage"));
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(null, null));
final Sender sender = sf.getSender(addr);
sender.send(new RoutedMessage(new int[] { 0 }, "Hello", huge));
assertTrue(poll(o -> rm.get() != null));
assertEquals(huge, rm.get().message);
}
}
}
use of net.dempsy.Infrastructure in project Dempsy by Dempsy.
the class TestGroupRoutingStrategy method testInboundResillience.
@Test
public void testInboundResillience() throws Exception {
final int numShardsToExpect = Integer.parseInt(Utils.DEFAULT_TOTAL_SHARDS);
final String groupName = "testInboundResillience";
final Manager<RoutingStrategy.Inbound> manager = new RoutingInboundManager();
try (final RoutingStrategy.Inbound ib = manager.getAssociatedInstance(ClusterGroupInbound.class.getPackage().getName() + ":" + groupName)) {
final ClusterId clusterId = super.setTestName("testInboundResillience");
final NodeAddress na = new DummyNodeAddress("theOnlyNode");
final ContainerAddress ca = new ContainerAddress(na, 0);
final GroupDetails gd = new GroupDetails(groupName, na);
final Infrastructure infra = makeInfra(session, sched);
final Utils<GroupDetails> msutils = new Utils<>(infra, groupName, gd);
ib.setContainerDetails(clusterId, ca, (l, m) -> {
});
ib.start(infra);
checkForShardDistribution(session, msutils, numShardsToExpect, 1);
disruptor.accept(session);
checkForShardDistribution(session, msutils, numShardsToExpect, 1);
}
}
Aggregations