@Test public void testBroadcast() { peer1 = vertx.createDatagramSocket(new DatagramSocketOptions().setBroadcast(true)); peer2 = vertx.createDatagramSocket(new DatagramSocketOptions().setBroadcast(true)); peer2.exceptionHandler(t -> fail(t.getMessage())); peer2.listen( 1234, "0.0.0.0", ar1 -> { assertTrue(ar1.succeeded()); Buffer buffer = TestUtils.randomBuffer(128); peer2.packetHandler( packet -> { assertEquals(buffer, packet.data()); testComplete(); }); peer1.send( buffer, 1234, "255.255.255.255", ar2 -> { assertTrue(ar2.succeeded()); }); }); await(); }
@Test public void testEcho() { peer1 = vertx.createDatagramSocket(new DatagramSocketOptions()); peer2 = vertx.createDatagramSocket(new DatagramSocketOptions()); peer1.exceptionHandler(t -> fail(t.getMessage())); peer2.exceptionHandler(t -> fail(t.getMessage())); peer2.listen( 1234, "127.0.0.1", ar -> { assertTrue(ar.succeeded()); Buffer buffer = TestUtils.randomBuffer(128); peer2.packetHandler( packet -> { assertEquals("127.0.0.1", packet.sender().hostAddress()); assertEquals(1235, packet.sender().hostPort()); assertEquals(buffer, packet.data()); peer2.send(packet.data(), 1235, "127.0.0.1", ar2 -> assertTrue(ar2.succeeded())); }); peer1.listen( 1235, "127.0.0.1", ar2 -> { peer1.packetHandler( packet -> { assertEquals(buffer, packet.data()); assertEquals("127.0.0.1", packet.sender().hostAddress()); assertEquals(1234, packet.sender().hostPort()); testComplete(); }); peer1.send(buffer, 1234, "127.0.0.1", ar3 -> assertTrue(ar3.succeeded())); }); }); await(); }
@Test public void testPublishBuffer() { Buffer sent = TestUtils.randomBuffer(100); testPublish( sent, (buffer) -> { assertEquals(sent, buffer); assertFalse(sent == buffer); // Make sure it's copied }); }
@Test public void testReplyBuffer() { Buffer sent = TestUtils.randomBuffer(100); testReply( sent, (bytes) -> { assertEquals(sent, bytes); assertFalse(sent == bytes); // Make sure it's copied }); }
@Test public void testMulticastJoinLeave() throws Exception { Buffer buffer = TestUtils.randomBuffer(128); String groupAddress = "230.0.0.1"; String iface = NetworkInterface.getByInetAddress(InetAddress.getByName("127.0.0.1")).getName(); AtomicBoolean received = new AtomicBoolean(); peer1 = vertx.createDatagramSocket(new DatagramSocketOptions().setMulticastNetworkInterface(iface)); peer2 = vertx.createDatagramSocket(new DatagramSocketOptions().setMulticastNetworkInterface(iface)); peer1.packetHandler( packet -> { assertEquals(buffer, packet.data()); received.set(true); }); peer1.listen( 1234, "0.0.0.0", ar1 -> { assertTrue(ar1.succeeded()); peer1.listenMulticastGroup( groupAddress, iface, null, ar2 -> { assertTrue(ar2.succeeded()); peer2.send( buffer, 1234, groupAddress, ar3 -> { assertTrue(ar3.succeeded()); // leave group in 1 second so give it enough time to really receive the packet // first vertx.setTimer( 1000, id -> { peer1.unlistenMulticastGroup( groupAddress, iface, null, ar4 -> { assertTrue(ar4.succeeded()); AtomicBoolean receivedAfter = new AtomicBoolean(); peer1.packetHandler( packet -> { // Should not receive any more event as it left the group receivedAfter.set(true); }); peer2.send( buffer, 1234, groupAddress, ar5 -> { assertTrue(ar5.succeeded()); // schedule a timer which will check in 1 second if we // received a message after the group // was left before vertx.setTimer( 1000, id2 -> { assertFalse(receivedAfter.get()); assertTrue(received.get()); testComplete(); }); }); }); }); }); }); }); await(); }