                                                   ["nested" "test-throughput"]
                                                   ["foo" "bar"]
                                                   ["outstanding" "fum"]]))]
-    (testing "retrieving metrics for specified services"
-      (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true)))
-      (is (zero? (count (.getMetrics metrics-registry))))
-      (create-metrics "test-service-1")
-      (is (all-service-metrics-available? (retrieve-service-metrics "test-service-1")))
-      (is (zero? (count (retrieve-service-metrics "test-service-2"))))
-      (is (zero? (count (retrieve-service-metrics "test-service-3"))))
-      (create-metrics "test-service-2")
-      (create-metrics "test-service-3")
-      (is (all-service-metrics-available? (retrieve-service-metrics "test-service-1")))
-      (is (all-service-metrics-available? (retrieve-service-metrics "test-service-2")))
-      (is (all-service-metrics-available? (retrieve-service-metrics "test-service-3")))
-      (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true))))))
+    (with-isolated-registry
+      (testing "retrieving metrics for specified services"
+        (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true)))
+        (is (zero? (count (.getMetrics metrics-registry))))
+        (create-metrics "test-service-1")
+        (is (all-service-metrics-available? (retrieve-service-metrics "test-service-1")))
+        (is (zero? (count (retrieve-service-metrics "test-service-2"))))
+        (is (zero? (count (retrieve-service-metrics "test-service-3"))))
+        (create-metrics "test-service-2")
+        (create-metrics "test-service-3")
+        (is (all-service-metrics-available? (retrieve-service-metrics "test-service-1")))
+        (is (all-service-metrics-available? (retrieve-service-metrics "test-service-2")))
+        (is (all-service-metrics-available? (retrieve-service-metrics "test-service-3")))
+        (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true)))))))
 
 (deftest test-get-waiter-metrics
   (let [metrics-registry mc/default-registry

                                                 ["nested" "test-throughput"]
                                                 ["foo" "bar"]
                                                 ["outstanding" "fum"]]))]
    (testing "retrieving metrics for specified services"
      (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true)))
      (is (zero? (count (.getMetrics metrics-registry))))
      (create-metrics "services.test-service-1")
      (create-metrics "waiter")
      (create-metrics "services.test-service-2")
      (is (all-waiter-metrics-available? (get-waiter-metrics)))
      (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true))))))

(deftest test-aggregate-router-codahale-metrics
  (let [router->metrics {"router-a" {"counters" {"instance-counts" {"a" 10, "b" 20 "in-use" 2 "my-instances" 1

                    {:name "old-counter-nil-input", :initial 2, :input nil, :expected 0}]]
    (doseq [{:keys [name initial input expected]} test-cases]
      (testing (str "Test " name)
        (with-isolated-registry
          (let [title ["waiter" "test" name]
                the-counter (counters/counter title)]
            (when initial
              (counters/clear! the-counter)
              (counters/inc! the-counter initial))
            (reset-counter the-counter input)
            (let [actual-value (counters/value the-counter)]
              (is (= expected actual-value)))))))))

(deftest test-prefix-and-contains-metrics-filter
  (with-isolated-registry
