Skip to content

Commit 4011baa

Browse files
authored
[TestMetrics] Cache created handlers (#15)
1 parent 7251965 commit 4011baa

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

Sources/MetricsTestUtils/TestMetrics.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,43 @@ public final class TestMetrics: MetricsFactory {
5656
// nothing to do
5757
}
5858

59+
/// Reset method to destroy all created ``TestCounter``, ``TestRecorder`` and ``TestTimer``.
60+
/// Invoke this method in between test runs to verify that Counters are created as needed.
61+
public func reset() {
62+
self.lock.withLock {
63+
self.counters = [:]
64+
self.recorders = [:]
65+
self.timers = [:]
66+
}
67+
}
68+
5969
public func makeCounter(label: String, dimensions: [(String, String)]) -> CounterHandler {
60-
return self.lock.withLock { () -> TestCounter in
70+
return self.lock.withLock { () -> CounterHandler in
71+
if let existing = self.counters[.init(label: label, dimensions: dimensions)] {
72+
return existing
73+
}
6174
let item = TestCounter(label: label, dimensions: dimensions)
6275
self.counters[.init(label: label, dimensions: dimensions)] = item
6376
return item
6477
}
6578
}
6679

6780
public func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> RecorderHandler {
68-
return self.lock.withLock { () -> TestRecorder in
81+
return self.lock.withLock { () -> RecorderHandler in
82+
if let existing = self.recorders[.init(label: label, dimensions: dimensions)] {
83+
return existing
84+
}
6985
let item = TestRecorder(label: label, dimensions: dimensions, aggregate: aggregate)
7086
self.recorders[.init(label: label, dimensions: dimensions)] = item
7187
return item
7288
}
7389
}
7490

7591
public func makeTimer(label: String, dimensions: [(String, String)]) -> TimerHandler {
76-
return self.lock.withLock { () -> TestTimer in
92+
return self.lock.withLock { () -> TimerHandler in
93+
if let existing = self.timers[.init(label: label, dimensions: dimensions)] {
94+
return existing
95+
}
7796
let item = TestTimer(label: label, dimensions: dimensions)
7897
self.timers[.init(label: label, dimensions: dimensions)] = item
7998
return item

0 commit comments

Comments
 (0)