private void run() throws Exception { try(final DatagramChannel c0 = createListeningChannelOnPort(new InetSocketAddress(InetAddress.getLoopbackAddress(), 12769)); final DatagramChannel c1 = createListeningChannelOnPort(new InetSocketAddress(InetAddress.getLoopbackAddress(), 12770)); final DatagramChannel c2 = multicastListener(InetAddress.getByName("239.192.45.3"), 5000)) { Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(this::pollMonitors, 0L, 1L, TimeUnit.SECONDS); udpSocketMonitor.beginMonitoringOf((InetSocketAddress) c0.getLocalAddress()); udpSocketMonitor.beginMonitoringOf((InetSocketAddress) c1.getLocalAddress()); udpSocketMonitor.beginMonitoringOf((InetSocketAddress) c2.getLocalAddress()); waitForMonitoringToStart(); Thread.sleep(TimeUnit.SECONDS.toMillis(1L)); final DatagramChannel writer = DatagramChannel.open().connect(c0.getLocalAddress()); while(!Thread.currentThread().isInterrupted()) { // overflow receive buffers writer.write(ByteBuffer.wrap("deadcod".getBytes(StandardCharsets.UTF_8))); Thread.sleep(200L); } } }
@Test public void shouldBeAbleToSubscribeToAddressAndPortAndInode() throws Exception { monitor.beginMonitoringOf(getSocketAddress("192.168.122.2", 53).getAddress(), 15294); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "192.168.122.2", 53, 9, 0, 0, 15294); }
@Test public void shouldBeAbleToSubscribeToAddressAndPortAndInode() throws Exception { monitor.beginMonitoringOf(getSocketAddress("192.168.122.2", 53), 15294); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "192.168.122.2", 53, 9, 0, 0, 15294); } }