use of net.dempsy.transport.MessageTransportException in project Dempsy by Dempsy.
the class BlockingQueueTest method testBlockingQueueOverflow.
/**
* Test overflow for a blocking Transport around a queue with depth one. While the transport will not call the, and does not even have a , overflow handler,
* every message will call the overflow handler on
* the receiver since the queue is always full.
*
* @throws Throwable
*/
@Test
public void testBlockingQueueOverflow() throws Throwable {
final AtomicReference<String> message = new AtomicReference<String>(null);
final ArrayBlockingQueue<Object> input = new ArrayBlockingQueue<>(1);
try (@SuppressWarnings("resource") SystemPropertyManager // test only works when the blocking queue blocks
props = new SystemPropertyManager().set(BlockingQueueSenderFactory.class.getPackageName() + "." + BlockingQueueSenderFactory.BLOCKING_KEY, "true");
final TestInfrastructure infra = new TestInfrastructure(new DefaultThreadingModel("BQTest-testBlockingQueueOverflow-"));
final Receiver r = new BlockingQueueReceiver(input);
final TransportManager tranMan = chain(new TransportManager(), c -> c.start(infra));
final SenderFactory sf = tranMan.getAssociatedInstance(transportTypeId)) {
final Sender sender = sf.getSender(r.getAddress(infra));
final AtomicBoolean finallySent = new AtomicBoolean(false);
final AtomicLong receiveCount = new AtomicLong();
// fill up queue
sender.send("Hello");
final Thread t = new Thread(() -> {
try {
sender.send("Hello again");
} catch (final MessageTransportException | InterruptedException e) {
throw new RuntimeException(e);
}
finallySent.set(true);
});
t.start();
Thread.sleep(100);
// the thread should be hung blocked on the send
assertFalse(finallySent.get());
// Start the receiver to read
r.start((final String msg) -> {
message.set(new String(msg));
receiveCount.incrementAndGet();
return true;
}, infra);
// 2 messages should have been read and the 2nd should be "Hello again"
assertTrue(poll(o -> "Hello again".equals(message.get())));
// The thread should shut down eventually
assertTrue(poll(o -> !t.isAlive()));
}
}
use of net.dempsy.transport.MessageTransportException in project Dempsy by Dempsy.
the class PassthroughSenderFactory method getSender.
@Override
public synchronized PassthroughSender getSender(final NodeAddress destination) throws MessageTransportException {
PassthroughSender ret = senders.get(destination);
if (ret == null) {
final PassthroughReceiver r = PassthroughReceiver.receivers.get(Long.valueOf(((PassthroughAddress) destination).destinationId));
if (r == null)
throw new MessageTransportException("Recveiver for " + destination + " is shut down");
ret = new PassthroughSender(r, statsCollector, this);
senders.put(destination, ret);
}
return ret;
}
use of net.dempsy.transport.MessageTransportException in project Dempsy by Dempsy.
the class NioReceiver method start.
@SuppressWarnings("unchecked")
@Override
public void start(final Listener<?> listener, final Infrastructure infra) throws MessageTransportException {
if (!isRunning.get())
throw new IllegalStateException("Cannot restart an " + NioReceiver.class.getSimpleName());
thePlug = infra.getThePlug();
if (binding == null)
// sets binding via side affect.
getAddress(infra);
// before starting the acceptor, make sure we have Readers created.
try {
for (int i = 0; i < readers.length; i++) readers[i] = new Reader<T>(isRunning, address, (Listener<T>) listener, serializer, maxMessageSize, thePlug);
} catch (final IOException ioe) {
LOGGER.error(address.toString() + " failed to start up readers", ioe);
throw new MessageTransportException(address.toString() + " failed to start up readers", ioe);
}
final ThreadingModel threadingModel = infra.getThreadingModel();
// now start the readers.
for (int i = 0; i < readers.length; i++) threadingModel.runDaemon(readers[i], "nio-reader-" + i + "-" + address);
// start the acceptor
threadingModel.runDaemon(acceptor = new Acceptor(binding, isRunning, readers, address, thePlug), "nio-acceptor-" + address);
}
use of net.dempsy.transport.MessageTransportException in project Dempsy by Dempsy.
the class NioSenderFactory method getSender.
@Override
public NioSender getSender(final NodeAddress destination) throws MessageTransportException {
final TcpAddress tcpaddr = (TcpAddress) destination;
final NioSender ret;
if (isRunning.get()) {
ret = senders.computeIfAbsent(tcpaddr, a -> new NioSender(a, this));
} else
throw new MessageTransportException(nodeId + " sender had getSender called while stopped.");
try {
ret.connect(false);
} catch (final IOException e) {
throw new MessageTransportException(nodeId + " sender failed to connect to " + destination, e);
}
return ret;
}
Aggregations