public void testConstructor() throws Exception { final Acceptor acceptor = new Acceptor("localhost", 0, 1); final FanOutServerSender serverSender = new FanOutServerSender(acceptor, ConnectionType.AGENT, 3); serverSender.shutdown(); acceptor.shutdown(); }
public void testSend() throws Exception { final Acceptor acceptor = new Acceptor("localhost", 0, 1); final FanOutServerSender serverSender = new FanOutServerSender(acceptor, ConnectionType.AGENT, 3); final Socket[] socket = new Socket[5]; for (int i = 0; i < socket.length; ++i) { socket[i] = new Connector( InetAddress.getByName(null).getHostName(), acceptor.getPort(), ConnectionType.AGENT) .connect(); } // Sleep until we've accepted all connections. Give up after a few // seconds. final ResourcePool socketSet = acceptor.getSocketSet(ConnectionType.AGENT); for (int i = 0; socketSet.countActive() != 5 && i < 10; ++i) { Thread.sleep(i * i * 10); } final SimpleMessage message1 = new SimpleMessage(); final SimpleMessage message2 = new SimpleMessage(); serverSender.send(message1); serverSender.send(message2); for (int i = 0; i < socket.length; ++i) { final InputStream socketInput = socket[i].getInputStream(); final Message m1 = readMessage(socketInput); final Message m2 = readMessage(socketInput); assertEquals(message1, m1); assertEquals(message2, m2); assertEquals(0, socketInput.available()); socket[i].close(); } serverSender.shutdown(); acceptor.shutdown(); }
public void testShutdown() throws Exception { final Acceptor acceptor = new Acceptor("localhost", 0, 1); final FanOutServerSender serverSender = new FanOutServerSender(acceptor, ConnectionType.AGENT, 3); assertEquals(1, acceptor.getThreadGroup().activeCount()); final Socket socket = new Connector( InetAddress.getByName(null).getHostName(), acceptor.getPort(), ConnectionType.AGENT) .connect(); // Sleep until we've accepted the connection. Give up after a few // seconds. final ResourcePool socketSet = acceptor.getSocketSet(ConnectionType.AGENT); for (int i = 0; socketSet.countActive() != 1 && i < 10; ++i) { Thread.sleep(i * i * 10); } final Message message = new SimpleMessage(); serverSender.send(message); final InputStream socketStream = socket.getInputStream(); final Object o1 = readMessage(socketStream); assertNotNull(o1); serverSender.shutdown(); try { serverSender.send(message); fail("Expected CommunicationException"); } catch (CommunicationException e) { } try { final Object o2 = readMessage(socketStream); assertTrue(o2 instanceof CloseCommunicationMessage); } catch (StreamCorruptedException e) { // Occasionally this occurs because the connection is shutdown. // Whatever. } acceptor.shutdown(); }
public void testSendAddressedMessage() throws Exception { final Acceptor acceptor = new Acceptor("localhost", 0, 1); final FanOutServerSender serverSender = new FanOutServerSender(acceptor, ConnectionType.AGENT, 3); final Socket[] socket = new Socket[5]; for (int i = 0; i < socket.length; ++i) { socket[i] = new Connector( InetAddress.getByName(null).getHostName(), acceptor.getPort(), ConnectionType.AGENT) .connect(new StubAddress(new Integer(i))); } // Sleep until we've accepted all connections. Give up after a few // seconds. final ResourcePool socketSet = acceptor.getSocketSet(ConnectionType.AGENT); for (int i = 0; socketSet.countActive() != 5 && i < 10; ++i) { Thread.sleep(i * i * 10); } final SimpleMessage message1 = new SimpleMessage(); final SimpleMessage message2 = new SimpleMessage(); serverSender.send(new StubAddress(new Integer(1)), message1); serverSender.send(new StubAddress(new Integer(2)), message2); for (int i = 0; i < socket.length; ++i) { final InputStream socketInput = socket[i].getInputStream(); if (i == 1) { final Message m = readMessage(socketInput); assertEquals(message1, m); } else if (i == 2) { final Message m = readMessage(socketInput); assertEquals(message2, m); } assertEquals(0, socketInput.available()); socket[i].close(); } serverSender.shutdown(); try { serverSender.send( new Address() { public boolean includes(Address address) { return false; } }, message1); fail("Expected CommunicationException"); } catch (CommunicationException e) { } acceptor.shutdown(); }