### Question:
MetricRegistry { public void flipPhaseAndReport(@Nullable MetricsReporter metricsReporter) { try { phaser.readerLock(); ConcurrentMap<Labels.Immutable, MetricSet> temp = inactiveMetricSets; inactiveMetricSets = activeMetricSets; activeMetricSets = temp; phaser.flipPhase(); if (metricsReporter != null) { metricsReporter.report(inactiveMetricSets); } for (MetricSet metricSet : inactiveMetricSets.values()) { metricSet.resetState(); } } finally { phaser.readerUnlock(); } } MetricRegistry(ReporterConfiguration config); void addUnlessNan(String name, Labels labels, DoubleSupplier metric); void addUnlessNegative(String name, Labels labels, DoubleSupplier metric); void add(String name, Labels labels, DoubleSupplier metric); double getGaugeValue(String name, Labels labels); @Nullable DoubleSupplier getGauge(String name, Labels labels); void flipPhaseAndReport(@Nullable MetricsReporter metricsReporter); void updateTimer(String timerName, Labels labels, long durationUs); void updateTimer(String timerName, Labels labels, long durationUs, long count); void incrementCounter(String name, Labels labels); long writerCriticalSectionEnter(); void writerCriticalSectionExit(long criticalValueAtEnter); void removeGauge(String metricName, Labels labels);  }### Answer:
@Test void testBuffersRotationWithReport() throws ExecutionException, InterruptedException { final CompletableFuture<Map<? extends Labels, MetricSet>> originalMetricSets = new CompletableFuture<>(); metricRegistry.flipPhaseAndReport(originalMetricSets::complete); final CompletableFuture<Map<? extends Labels, MetricSet>> firstRotationMetricSets = new CompletableFuture<>(); metricRegistry.flipPhaseAndReport(firstRotationMetricSets::complete); assertThat(firstRotationMetricSets.get()).isNotSameAs(originalMetricSets.get()); final CompletableFuture<Map<? extends Labels, MetricSet>> secondRotationMetricSets = new CompletableFuture<>(); metricRegistry.flipPhaseAndReport(secondRotationMetricSets::complete); assertThat(secondRotationMetricSets.get()).isNotSameAs(firstRotationMetricSets.get()); assertThat(secondRotationMetricSets.get()).isSameAs(originalMetricSets.get()); }
@Test void testBuffersRotationWithoutReport() throws ExecutionException, InterruptedException { final CompletableFuture<Map<? extends Labels, MetricSet>> originalMetricSets = new CompletableFuture<>(); metricRegistry.flipPhaseAndReport(originalMetricSets::complete); metricRegistry.flipPhaseAndReport(null); final CompletableFuture<Map<? extends Labels, MetricSet>> firstRotationMetricSets = new CompletableFuture<>(); metricRegistry.flipPhaseAndReport(firstRotationMetricSets::complete); assertThat(firstRotationMetricSets.get()).isSameAs(originalMetricSets.get()); }