|
14 | 14 |
|
15 | 15 | @testable import Lifecycle
|
16 | 16 | import LifecycleNIOCompat
|
| 17 | +import Metrics |
17 | 18 | import NIO
|
18 | 19 | import XCTest
|
19 | 20 |
|
@@ -1247,4 +1248,111 @@ final class ComponentLifecycleTests: XCTestCase {
|
1247 | 1248 |
|
1248 | 1249 | XCTAssertFalse(item.shutdown, "expected item to be shutdown")
|
1249 | 1250 | }
|
| 1251 | + |
| 1252 | + func testMetrics() { |
| 1253 | + let metrics = TestMetrics() |
| 1254 | + MetricsSystem.bootstrap(metrics) |
| 1255 | + |
| 1256 | + let items = (0 ..< 3).map { _ in GoodItem(id: UUID().uuidString, startDelay: 0.1, shutdownDelay: 0.1) } |
| 1257 | + let lifecycle = ComponentLifecycle(label: "test") |
| 1258 | + lifecycle.register(items) |
| 1259 | + lifecycle.start { startError in |
| 1260 | + XCTAssertNil(startError, "not expecting error") |
| 1261 | + lifecycle.shutdown { shutdownErrors in |
| 1262 | + XCTAssertNil(shutdownErrors, "not expecting error") |
| 1263 | + } |
| 1264 | + } |
| 1265 | + lifecycle.wait() |
| 1266 | + XCTAssertEqual(metrics.counters["\(lifecycle.label).lifecycle.start"]?.value, 1, "expected start counter to be 1") |
| 1267 | + XCTAssertEqual(metrics.counters["\(lifecycle.label).lifecycle.shutdown"]?.value, 1, "expected shutdown counter to be 1") |
| 1268 | + items.forEach { XCTAssertGreaterThan(metrics.timers["\(lifecycle.label).\($0.label).lifecycle.start"]?.value ?? 0, 0, "expected start timer to be non-zero") } |
| 1269 | + items.forEach { XCTAssertGreaterThan(metrics.timers["\(lifecycle.label).\($0.label).lifecycle.shutdown"]?.value ?? 0, 0, "expected shutdown timer to be non-zero") } |
| 1270 | + } |
| 1271 | +} |
| 1272 | + |
| 1273 | +class TestMetrics: MetricsFactory, RecorderHandler { |
| 1274 | + var counters = [String: TestCounter]() |
| 1275 | + var timers = [String: TestTimer]() |
| 1276 | + let lock = Lock() |
| 1277 | + |
| 1278 | + public init() {} |
| 1279 | + |
| 1280 | + public func makeCounter(label: String, dimensions: [(String, String)]) -> CounterHandler { |
| 1281 | + let counter = TestCounter(label: label) |
| 1282 | + self.lock.withLock { |
| 1283 | + self.counters[label] = counter |
| 1284 | + } |
| 1285 | + return counter |
| 1286 | + } |
| 1287 | + |
| 1288 | + public func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> RecorderHandler { |
| 1289 | + return self |
| 1290 | + } |
| 1291 | + |
| 1292 | + public func makeTimer(label: String, dimensions: [(String, String)]) -> TimerHandler { |
| 1293 | + let timer = TestTimer(label: label) |
| 1294 | + self.lock.withLock { |
| 1295 | + self.timers[label] = timer |
| 1296 | + } |
| 1297 | + return timer |
| 1298 | + } |
| 1299 | + |
| 1300 | + public func destroyCounter(_: CounterHandler) {} |
| 1301 | + public func destroyRecorder(_: RecorderHandler) {} |
| 1302 | + public func destroyTimer(_: TimerHandler) {} |
| 1303 | + |
| 1304 | + public func record(_: Int64) {} |
| 1305 | + public func record(_: Double) {} |
| 1306 | + |
| 1307 | + class TestCounter: CounterHandler { |
| 1308 | + let label: String |
| 1309 | + var _value: Int64 |
| 1310 | + let lock = Lock() |
| 1311 | + |
| 1312 | + init(label: String) { |
| 1313 | + self.label = label |
| 1314 | + self._value = 0 |
| 1315 | + } |
| 1316 | + |
| 1317 | + public func increment(by: Int64) { |
| 1318 | + self.lock.withLock { |
| 1319 | + self._value += by |
| 1320 | + } |
| 1321 | + } |
| 1322 | + |
| 1323 | + public func reset() { |
| 1324 | + self.lock.withLock { |
| 1325 | + self._value = 0 |
| 1326 | + } |
| 1327 | + } |
| 1328 | + |
| 1329 | + public var value: Int64 { |
| 1330 | + return self.lock.withLock { |
| 1331 | + return self._value |
| 1332 | + } |
| 1333 | + } |
| 1334 | + } |
| 1335 | + |
| 1336 | + class TestTimer: TimerHandler { |
| 1337 | + let label: String |
| 1338 | + var _value: Int64 |
| 1339 | + let lock = Lock() |
| 1340 | + |
| 1341 | + init(label: String) { |
| 1342 | + self.label = label |
| 1343 | + self._value = 0 |
| 1344 | + } |
| 1345 | + |
| 1346 | + public func recordNanoseconds(_ value: Int64) { |
| 1347 | + self.lock.withLock { |
| 1348 | + self._value = value |
| 1349 | + } |
| 1350 | + } |
| 1351 | + |
| 1352 | + public var value: Int64 { |
| 1353 | + return self.lock.withLock { |
| 1354 | + return self._value |
| 1355 | + } |
| 1356 | + } |
| 1357 | + } |
1250 | 1358 | }
|
0 commit comments