### Question:
PrometheusReporter extends AbstractPrometheusReporter { @VisibleForTesting int getPort() { Preconditions.checkState(httpServer != null, "Server has not been initialized."); return port; }  @Override void open(MetricConfig config); @Override void close();  }### Answer:
@Test public void metricIsRemovedWhenCollectorIsNotUnregisteredYet() throws UnirestException { TaskManagerMetricGroup tmMetricGroup = new TaskManagerMetricGroup(registry, HOST_NAME, TASK_MANAGER); String metricName = "metric"; Counter metric1 = new SimpleCounter(); FrontMetricGroup<TaskManagerJobMetricGroup> metricGroup1 = new FrontMetricGroup<>(0, new TaskManagerJobMetricGroup(registry, tmMetricGroup, JobID.generate(), "job_1")); reporter.notifyOfAddedMetric(metric1, metricName, metricGroup1); Counter metric2 = new SimpleCounter(); FrontMetricGroup<TaskManagerJobMetricGroup> metricGroup2 = new FrontMetricGroup<>(0, new TaskManagerJobMetricGroup(registry, tmMetricGroup, JobID.generate(), "job_2")); reporter.notifyOfAddedMetric(metric2, metricName, metricGroup2); reporter.notifyOfRemovedMetric(metric1, metricName, metricGroup1); String response = pollMetrics(reporter.getPort()).getBody(); assertThat(response, not(containsString("job_1"))); }
@Test public void cannotStartTwoReportersOnSamePort() throws Exception { final MetricRegistryImpl fixedPort1 = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(createConfigWithOneReporter("test1", portRangeProvider.next()))); assertThat(fixedPort1.getReporters(), hasSize(1)); PrometheusReporter firstReporter = (PrometheusReporter) fixedPort1.getReporters().get(0); final MetricRegistryImpl fixedPort2 = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(createConfigWithOneReporter("test2", String.valueOf(firstReporter.getPort())))); assertThat(fixedPort2.getReporters(), hasSize(0)); fixedPort1.shutdown().get(); fixedPort2.shutdown().get(); }
@Test public void metricIsRemovedWhenCollectorIsNotUnregisteredYet() throws UnirestException { TaskManagerMetricGroup tmMetricGroup = new TaskManagerMetricGroup(registry, HOST_NAME, TASK_MANAGER); String metricName = "metric"; Counter metric1 = new SimpleCounter(); FrontMetricGroup<TaskManagerJobMetricGroup> metricGroup1 = new FrontMetricGroup<>( createReporterScopedSettings(), new TaskManagerJobMetricGroup(registry, tmMetricGroup, JobID.generate(), "job_1")); reporter.notifyOfAddedMetric(metric1, metricName, metricGroup1); Counter metric2 = new SimpleCounter(); FrontMetricGroup<TaskManagerJobMetricGroup> metricGroup2 = new FrontMetricGroup<>( createReporterScopedSettings(), new TaskManagerJobMetricGroup(registry, tmMetricGroup, JobID.generate(), "job_2")); reporter.notifyOfAddedMetric(metric2, metricName, metricGroup2); reporter.notifyOfRemovedMetric(metric1, metricName, metricGroup1); String response = pollMetrics(reporter.getPort()).getBody(); assertThat(response, not(containsString("job_1"))); }