26
26
//===----------------------------------------------------------------------===//
27
27
28
28
@testable import CoreMetrics
29
- @ testable import Metrics
29
+ import Metrics
30
30
import XCTest
31
31
32
32
/// Taken directly from `swift-cluster-memberships`'s own test target package, which
@@ -57,43 +57,50 @@ public final class TestMetrics: MetricsFactory {
57
57
}
58
58
59
59
public func makeCounter( label: String , dimensions: [ ( String , String ) ] ) -> CounterHandler {
60
- return self . make ( label: label, dimensions: dimensions, registry: & self . counters, maker: TestCounter . init)
60
+ return self . lock. withLock { ( ) -> TestCounter in
61
+ let item = TestCounter ( label: label, dimensions: dimensions)
62
+ self . counters [ . init( label: label, dimensions: dimensions) ] = item
63
+ return item
64
+ }
61
65
}
62
66
63
67
public func makeRecorder( label: String , dimensions: [ ( String , String ) ] , aggregate: Bool ) -> RecorderHandler {
64
- let maker = { ( label: String , dimensions: [ ( String , String ) ] ) -> RecorderHandler in
65
- TestRecorder ( label: label, dimensions: dimensions, aggregate: aggregate)
68
+ return self . lock. withLock { ( ) -> TestRecorder in
69
+ let item = TestRecorder ( label: label, dimensions: dimensions, aggregate: aggregate)
70
+ self . recorders [ . init( label: label, dimensions: dimensions) ] = item
71
+ return item
66
72
}
67
- return self . make ( label: label, dimensions: dimensions, registry: & self . recorders, maker: maker)
68
73
}
69
74
70
75
public func makeTimer( label: String , dimensions: [ ( String , String ) ] ) -> TimerHandler {
71
- return self . make ( label: label, dimensions: dimensions, registry: & self . timers, maker: TestTimer . init)
72
- }
73
-
74
- private func make< Item> ( label: String , dimensions: [ ( String , String ) ] , registry: inout [ FullKey : Item ] , maker: ( String , [ ( String , String ) ] ) -> Item ) -> Item {
75
- return self . lock. withLock { ( ) -> Item in
76
- let item = maker ( label, dimensions)
77
- registry [ . init( label: label, dimensions: dimensions) ] = item
76
+ return self . lock. withLock { ( ) -> TestTimer in
77
+ let item = TestTimer ( label: label, dimensions: dimensions)
78
+ self . timers [ . init( label: label, dimensions: dimensions) ] = item
78
79
return item
79
80
}
80
81
}
81
82
82
83
public func destroyCounter( _ handler: CounterHandler ) {
83
84
if let testCounter = handler as? TestCounter {
84
- self . counters. removeValue ( forKey: testCounter. key)
85
+ self . lock. withLock { ( ) -> Void in
86
+ self . counters. removeValue ( forKey: testCounter. key)
87
+ }
85
88
}
86
89
}
87
90
88
91
public func destroyRecorder( _ handler: RecorderHandler ) {
89
92
if let testRecorder = handler as? TestRecorder {
90
- self . recorders. removeValue ( forKey: testRecorder. key)
93
+ self . lock. withLock { ( ) -> Void in
94
+ self . recorders. removeValue ( forKey: testRecorder. key)
95
+ }
91
96
}
92
97
}
93
98
94
99
public func destroyTimer( _ handler: TimerHandler ) {
95
100
if let testTimer = handler as? TestTimer {
96
- self . timers. removeValue ( forKey: testTimer. key)
101
+ self . lock. withLock { ( ) -> Void in
102
+ self . timers. removeValue ( forKey: testTimer. key)
103
+ }
97
104
}
98
105
}
99
106
}
@@ -130,17 +137,15 @@ extension TestMetrics {
130
137
}
131
138
132
139
public func expectCounter( _ label: String , _ dimensions: [ ( String , String ) ] = [ ] ) throws -> TestCounter {
133
- let counter : CounterHandler
134
- if let c: CounterHandler = self . counters [ . init( label: label, dimensions: dimensions) ] {
135
- counter = c
136
- } else {
137
- throw TestMetricsError . missingMetric ( label: label, dimensions: [ ] )
140
+ let maybeItem = self . lock. withLock {
141
+ self . counters [ . init( label: label, dimensions: dimensions) ]
138
142
}
139
-
140
- guard let testCounter = counter as? TestCounter else {
141
- throw TestMetricsError . illegalMetricType ( metric: counter, expected: " \( TestCounter . self) " )
143
+ guard let maybeCounter = maybeItem else {
144
+ throw TestMetricsError . missingMetric ( label: label, dimensions: dimensions)
145
+ }
146
+ guard let testCounter = maybeCounter as? TestCounter else {
147
+ throw TestMetricsError . illegalMetricType ( metric: maybeCounter, expected: " \( TestCounter . self) " )
142
148
}
143
-
144
149
return testCounter
145
150
}
146
151
@@ -168,13 +173,15 @@ extension TestMetrics {
168
173
}
169
174
170
175
public func expectRecorder( _ label: String , _ dimensions: [ ( String , String ) ] = [ ] ) throws -> TestRecorder {
171
- guard let counter = self . recorders [ . init ( label : label , dimensions : dimensions ) ] else {
172
- throw TestMetricsError . missingMetric ( label: label, dimensions: [ ] )
176
+ let maybeItem = self . lock . withLock {
177
+ self . recorders [ . init ( label: label, dimensions: dimensions ) ]
173
178
}
174
- guard let testRecorder = counter as? TestRecorder else {
175
- throw TestMetricsError . illegalMetricType ( metric: counter, expected: " \( TestRecorder . self) " )
179
+ guard let maybeRecorder = maybeItem else {
180
+ throw TestMetricsError . missingMetric ( label: label, dimensions: dimensions)
181
+ }
182
+ guard let testRecorder = maybeRecorder as? TestRecorder else {
183
+ throw TestMetricsError . illegalMetricType ( metric: maybeRecorder, expected: " \( TestRecorder . self) " )
176
184
}
177
-
178
185
return testRecorder
179
186
}
180
187
@@ -190,13 +197,15 @@ extension TestMetrics {
190
197
}
191
198
192
199
public func expectTimer( _ label: String , _ dimensions: [ ( String , String ) ] = [ ] ) throws -> TestTimer {
193
- guard let counter = self . timers [ . init ( label : label , dimensions : dimensions ) ] else {
194
- throw TestMetricsError . missingMetric ( label: label, dimensions: [ ] )
200
+ let maybeItem = self . lock . withLock {
201
+ self . timers [ . init ( label: label, dimensions: dimensions ) ]
195
202
}
196
- guard let testTimer = counter as? TestTimer else {
197
- throw TestMetricsError . illegalMetricType ( metric: counter, expected: " \( TestTimer . self) " )
203
+ guard let maybeTimer = maybeItem else {
204
+ throw TestMetricsError . missingMetric ( label: label, dimensions: dimensions)
205
+ }
206
+ guard let testTimer = maybeTimer as? TestTimer else {
207
+ throw TestMetricsError . illegalMetricType ( metric: maybeTimer, expected: " \( TestTimer . self) " )
198
208
}
199
-
200
209
return testTimer
201
210
}
202
211
}
0 commit comments