### Question:
AbstractMonitorFactory implements MonitorFactory { @Override public Monitor getMonitor(URL url) { url = url.setPath(MonitorService.class.getName()).addParameter(Constants.INTERFACE_KEY, MonitorService.class.getName()); String key = url.toServiceStringWithoutResolving(); Monitor monitor = MONITORS.get(key); Future<Monitor> future = FUTURES.get(key); if (monitor != null || future != null) { return monitor; } LOCK.lock(); try { monitor = MONITORS.get(key); future = FUTURES.get(key); if (monitor != null || future != null) { return monitor; } final URL monitorUrl = url; final CompletableFuture<Monitor> completableFuture = CompletableFuture.supplyAsync(() -> AbstractMonitorFactory.this.createMonitor(monitorUrl)); completableFuture.thenRunAsync(new MonitorListener(key), executor); FUTURES.put(key, completableFuture); return null; } finally { LOCK.unlock(); } }  static Collection<Monitor> getMonitors(); @Override Monitor getMonitor(URL url);  }### Answer:
@Test public void testMonitorFactoryCache() throws Exception { URL url = URL.valueOf("dubbo: Monitor monitor1 = monitorFactory.getMonitor(url); Monitor monitor2 = monitorFactory.getMonitor(url); if (monitor1 == null || monitor2 == null) { Thread.sleep(2000); monitor1 = monitorFactory.getMonitor(url); monitor2 = monitorFactory.getMonitor(url); } Assertions.assertEquals(monitor1, monitor2); }
@Test public void testMonitorFactoryIpCache() throws Exception { URL url = URL.valueOf("dubbo: Monitor monitor1 = monitorFactory.getMonitor(url); Monitor monitor2 = monitorFactory.getMonitor(url); if (monitor1 == null || monitor2 == null) { Thread.sleep(2000); monitor1 = monitorFactory.getMonitor(url); monitor2 = monitorFactory.getMonitor(url); } Assertions.assertEquals(monitor1, monitor2); }
@Test public void testMonitorFactoryGroupCache() throws Exception { URL url1 = URL.valueOf("dubbo: URL url2 = URL.valueOf("dubbo: Monitor monitor1 = monitorFactory.getMonitor(url1); Monitor monitor2 = monitorFactory.getMonitor(url2); if (monitor1 == null || monitor2 == null) { Thread.sleep(2000); monitor1 = monitorFactory.getMonitor(url1); monitor2 = monitorFactory.getMonitor(url2); } Assertions.assertNotSame(monitor1, monitor2); }