ConfigurableUDPSource { static void clearSourceHandlers(String sourceName) { ExtensionWebSocketClient client = clients.get(sourceName); synchronized (socketLock) { for (Map.Entry<DatagramSocket,List<String>> entry : udpSocketToSources.entrySet()) { List<String> list = entry.getValue(); if (list.contains(client.getSourceName())) { list.remove(client.getSourceName()); if (list.isEmpty()) { DatagramSocket socket = entry.getKey(); udpSocketToSources.remove(socket); socket.close(); } break; } } } notificationHandlers.remove(sourceName); client.setPublishHandler(UDPDefaultPublish); sourceAddresses.remove(sourceName); sourcePorts.remove(sourceName); sourceServers.remove(sourceName); }  static DatagramSocket createUDPSocket(int port, InetAddress address, String sourceName); static DatagramSocket listenOnUDPSocket(int port, InetAddress address, String sourceName); static void setNotificationHandler(UDPNotificationHandler handler, String sourceName, Map incoming); static void main(String[] args); static void sendFromDatagram(DatagramPacket packet, List<String> sources);  }
@Test public void testClearSourceHandlers() throws UnknownHostException { InetAddress address = InetAddress.getByName("localhost"); int port = 10213; String firstSource = "first"; client = new FalseClient(firstSource); ConfigurableUDPSource.notificationHandlers.put(firstSource, new UDPNotificationHandler(new LinkedHashMap<>(), client)); ConfigurableUDPSource.clients.put(firstSource, client); String secondSource = "second"; client = new FalseClient(secondSource); ConfigurableUDPSource.notificationHandlers.put(secondSource, new UDPNotificationHandler(new LinkedHashMap<>(), client)); ConfigurableUDPSource.clients.put(secondSource, client); assumeTrue("Cannot perform test. Address at port:" + port + " and IP:" + address + "already bound" , socketCanBind(port, address)); DatagramSocket socket = ConfigurableUDPSource.createUDPSocket(port, address, firstSource); ConfigurableUDPSource.listenOnUDPSocket(port, address, secondSource); assert ConfigurableUDPSource.udpSocketToSources.get(socket) instanceof List; List<String> l = (List) ConfigurableUDPSource.udpSocketToSources.get(socket); assert l.contains(firstSource); assert l.contains(secondSource); assert ConfigurableUDPSource.notificationHandlers.containsKey(firstSource); ConfigurableUDPSource.clearSourceHandlers(firstSource); l = (List) ConfigurableUDPSource.udpSocketToSources.get(socket); assert !l.contains(firstSource); assert l.contains(secondSource); assert !ConfigurableUDPSource.notificationHandlers.containsKey(firstSource); assert !socket.isClosed(); assert ConfigurableUDPSource.notificationHandlers.containsKey(secondSource); ConfigurableUDPSource.clearSourceHandlers(secondSource); assert !ConfigurableUDPSource.udpSocketToSources.containsKey(socket); assert !ConfigurableUDPSource.notificationHandlers.containsKey(secondSource); assert socket.isClosed(); }