@@ -56,24 +56,43 @@ public final class TestMetrics: MetricsFactory {
56
56
// nothing to do
57
57
}
58
58
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
+
59
69
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
+ }
61
74
let item = TestCounter ( label: label, dimensions: dimensions)
62
75
self . counters [ . init( label: label, dimensions: dimensions) ] = item
63
76
return item
64
77
}
65
78
}
66
79
67
80
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
+ }
69
85
let item = TestRecorder ( label: label, dimensions: dimensions, aggregate: aggregate)
70
86
self . recorders [ . init( label: label, dimensions: dimensions) ] = item
71
87
return item
72
88
}
73
89
}
74
90
75
91
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
+ }
77
96
let item = TestTimer ( label: label, dimensions: dimensions)
78
97
self . timers [ . init( label: label, dimensions: dimensions) ] = item
79
98
return item
0 commit comments