Skip to content

Commit 3402510

Browse files
authored
Make 'values' on TestCounter, TestRecorder and TestMeter public (#129)
Co-authored-by: Hamzah Malik <[email protected]>
1 parent 8bcdb6e commit 3402510

File tree

4 files changed

+53
-51
lines changed

4 files changed

+53
-51
lines changed

Sources/MetricsTestKit/TestMetrics.swift

+23-21
Original file line numberDiff line numberDiff line change
@@ -324,20 +324,22 @@ public final class TestCounter: TestMetric, CounterHandler, Equatable {
324324
}
325325

326326
public var lastValue: Int64? {
327-
return self.values.last?.1
327+
return self.last?.1
328328
}
329329

330330
public var totalValue: Int64 {
331-
return self.values.map { $0.1 }.reduce(0, +)
331+
return self.values.reduce(0, +)
332332
}
333333

334334
public var last: (Date, Int64)? {
335-
return self.values.last
335+
return self.lock.withLock {
336+
self._values.last
337+
}
336338
}
337339

338-
var values: [(Date, Int64)] {
340+
public var values: [Int64] {
339341
return self.lock.withLock {
340-
self._values
342+
self._values.map { $0.1 }
341343
}
342344
}
343345

@@ -396,12 +398,14 @@ public final class TestMeter: TestMetric, MeterHandler, Equatable {
396398
}
397399

398400
public var last: (Date, Double)? {
399-
return self.values.last
401+
return self.lock.withLock {
402+
self._values.last
403+
}
400404
}
401405

402-
var values: [(Date, Double)] {
406+
public var values: [Double] {
403407
return self.lock.withLock {
404-
self._values
408+
self._values.map { $0.1 }
405409
}
406410
}
407411

@@ -446,12 +450,14 @@ public final class TestRecorder: TestMetric, RecorderHandler, Equatable {
446450
}
447451

448452
public var last: (Date, Double)? {
449-
return self.values.last
453+
return self.lock.withLock {
454+
self._values.last
455+
}
450456
}
451457

452-
var values: [(Date, Double)] {
458+
public var values: [Double] {
453459
return self.lock.withLock {
454-
self._values
460+
self._values.map { $0.1 }
455461
}
456462
}
457463

@@ -486,14 +492,12 @@ public final class TestTimer: TestMetric, TimerHandler, Equatable {
486492
}
487493
}
488494

489-
func retrieveValueInPreferredUnit(atIndex i: Int) -> Double {
490-
return self.lock.withLock {
491-
let value = _values[i].1
492-
guard let displayUnit = self.displayUnit else {
493-
return Double(value)
494-
}
495-
return Double(value) / Double(displayUnit.scaleFromNanoseconds)
495+
public func valueInPreferredUnit(atIndex i: Int) -> Double {
496+
let value = self.values[i]
497+
guard let displayUnit = self.displayUnit else {
498+
return Double(value)
496499
}
500+
return Double(value) / Double(displayUnit.scaleFromNanoseconds)
497501
}
498502

499503
public func recordNanoseconds(_ duration: Int64) {
@@ -503,9 +507,7 @@ public final class TestTimer: TestMetric, TimerHandler, Equatable {
503507
}
504508

505509
public var lastValue: Int64? {
506-
return self.lock.withLock {
507-
return self._values.last?.1
508-
}
510+
return self.last?.1
509511
}
510512

511513
public var values: [Int64] {

Tests/MetricsTests/CoreMetricsTests.swift

+18-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
@testable import CoreMetrics
16-
@testable import MetricsTestKit
16+
import MetricsTestKit
1717
import XCTest
1818

1919
class MetricsTests: XCTestCase {
@@ -49,7 +49,7 @@ class MetricsTests: XCTestCase {
4949
Counter(label: name).increment(by: value)
5050
let counter = try metrics.expectCounter(name)
5151
XCTAssertEqual(counter.values.count, 1, "expected number of entries to match")
52-
XCTAssertEqual(counter.values[0].1, Int64(value), "expected value to match")
52+
XCTAssertEqual(counter.values[0], Int64(value), "expected value to match")
5353
counter.reset()
5454
XCTAssertEqual(counter.values.count, 0, "expected number of entries to match")
5555
}
@@ -112,7 +112,7 @@ class MetricsTests: XCTestCase {
112112
fpCounter.increment(by: Double(sign: .plus, exponent: 63, significand: 1))
113113
// Much larger than Int64
114114
fpCounter.increment(by: Double.greatestFiniteMagnitude)
115-
let values = counter.values.map { $0.1 }
115+
let values = counter.values
116116
XCTAssertEqual(values.count, 2, "expected number of entries to match")
117117
XCTAssertEqual(values, [Int64.max, Int64.max], "expected extremely large values to be replaced with Int64.max")
118118
}
@@ -132,15 +132,15 @@ class MetricsTests: XCTestCase {
132132

133133
// Increment by a small value that should grow the accumulated buffer past 1.0 (perfectly representable)
134134
fpCounter.increment(by: 1.5)
135-
var values = counter.values.map { $0.1 }
135+
var values = counter.values
136136
XCTAssertEqual(values.count, 1, "expected number of entries to match")
137137
XCTAssertEqual(values, [2], "expected entries to match")
138138
XCTAssertEqual(rawFpCounter.fraction, 0.25, "")
139139

140140
// Increment by a large value that should leave a fraction in the accumulator
141141
// 1110506744053.76
142142
fpCounter.increment(by: Double(sign: .plus, exponent: 40, significand: 1.01))
143-
values = counter.values.map { $0.1 }
143+
values = counter.values
144144
XCTAssertEqual(values.count, 2, "expected number of entries to match")
145145
XCTAssertEqual(values, [2, 1_110_506_744_054], "expected entries to match")
146146
XCTAssertEqual(rawFpCounter.fraction, 0.010009765625, "expected fractional accumulated value")
@@ -178,7 +178,7 @@ class MetricsTests: XCTestCase {
178178
}
179179
XCTAssertEqual(values.count, testRecorder.values.count, "expected number of entries to match")
180180
for i in 0 ... values.count - 1 {
181-
XCTAssertEqual(Int32(testRecorder.values[i].1), values[i], "expected value #\(i) to match.")
181+
XCTAssertEqual(Int32(testRecorder.values[i]), values[i], "expected value #\(i) to match.")
182182
}
183183
}
184184

@@ -194,7 +194,7 @@ class MetricsTests: XCTestCase {
194194
}
195195
XCTAssertEqual(values.count, testRecorder.values.count, "expected number of entries to match")
196196
for i in 0 ... values.count - 1 {
197-
XCTAssertEqual(Float(testRecorder.values[i].1), values[i], "expected value #\(i) to match.")
197+
XCTAssertEqual(Float(testRecorder.values[i]), values[i], "expected value #\(i) to match.")
198198
}
199199
}
200200

@@ -373,7 +373,7 @@ class MetricsTests: XCTestCase {
373373
meter.set(value)
374374
let testMeter = meter._handler as! TestMeter
375375
XCTAssertEqual(testMeter.values.count, 1, "expected number of entries to match")
376-
XCTAssertEqual(testMeter.values[0].1, value, "expected value to match")
376+
XCTAssertEqual(testMeter.values[0], value, "expected value to match")
377377
}
378378

379379
func testMeterBlock() throws {
@@ -386,7 +386,7 @@ class MetricsTests: XCTestCase {
386386
Meter(label: name).set(value)
387387
let testMeter = try metrics.expectMeter(name)
388388
XCTAssertEqual(testMeter.values.count, 1, "expected number of entries to match")
389-
XCTAssertEqual(testMeter.values[0].1, value, "expected value to match")
389+
XCTAssertEqual(testMeter.values[0], value, "expected value to match")
390390
}
391391

392392
func testMUX_Counter() throws {
@@ -424,7 +424,7 @@ class MetricsTests: XCTestCase {
424424
let meter = factory.meters.first
425425
XCTAssertEqual(meter?.label, name, "expected label to match")
426426
XCTAssertEqual(meter?.values.count, 1, "expected number of entries to match")
427-
XCTAssertEqual(meter?.values[0].1, value, "expected value to match")
427+
XCTAssertEqual(meter?.values[0], value, "expected value to match")
428428
}
429429
}
430430

@@ -441,7 +441,7 @@ class MetricsTests: XCTestCase {
441441
let recorder = factory.recorders.first
442442
XCTAssertEqual(recorder?.label, name, "expected label to match")
443443
XCTAssertEqual(recorder?.values.count, 1, "expected number of entries to match")
444-
XCTAssertEqual(recorder?.values[0].1, value, "expected value to match")
444+
XCTAssertEqual(recorder?.values[0], value, "expected value to match")
445445
}
446446
}
447447

@@ -460,7 +460,7 @@ class MetricsTests: XCTestCase {
460460
XCTAssertEqual(timer?.values.count, 1, "expected number of entries to match")
461461
XCTAssertEqual(timer?.values[0], Int64(seconds * 1_000_000_000), "expected value to match")
462462
XCTAssertEqual(timer?.displayUnit, .minutes, "expected value to match")
463-
XCTAssertEqual(timer?.retrieveValueInPreferredUnit(atIndex: 0), Double(seconds) / 60.0, "seconds should be returned as minutes")
463+
XCTAssertEqual(timer?.valueInPreferredUnit(atIndex: 0), Double(seconds) / 60.0, "seconds should be returned as minutes")
464464
}
465465
}
466466

@@ -488,7 +488,7 @@ class MetricsTests: XCTestCase {
488488

489489
let recorder = gauge._handler as! TestRecorder
490490
XCTAssertEqual(recorder.values.count, 1, "expected number of entries to match")
491-
XCTAssertEqual(recorder.values.first!.1, value, "expected value to match")
491+
XCTAssertEqual(recorder.values.first, value, "expected value to match")
492492
XCTAssertEqual(metrics.recorders.count, 1, "recorder should have been stored")
493493

494494
let identity = ObjectIdentifier(recorder)
@@ -500,7 +500,7 @@ class MetricsTests: XCTestCase {
500500

501501
let recorderAgain = gaugeAgain._handler as! TestRecorder
502502
XCTAssertEqual(recorderAgain.values.count, 1, "expected number of entries to match")
503-
XCTAssertEqual(recorderAgain.values.first!.1, -value, "expected value to match")
503+
XCTAssertEqual(recorderAgain.values.first, -value, "expected value to match")
504504

505505
let identityAgain = ObjectIdentifier(recorderAgain)
506506
XCTAssertNotEqual(identity, identityAgain, "since the cached metric was released, the created a new should have a different identity")
@@ -518,7 +518,7 @@ class MetricsTests: XCTestCase {
518518

519519
let testMeter = meter._handler as! TestMeter
520520
XCTAssertEqual(testMeter.values.count, 1, "expected number of entries to match")
521-
XCTAssertEqual(testMeter.values.first!.1, value, "expected value to match")
521+
XCTAssertEqual(testMeter.values.first, value, "expected value to match")
522522
XCTAssertEqual(metrics.meters.count, 1, "recorder should have been stored")
523523

524524
let identity = ObjectIdentifier(testMeter)
@@ -530,7 +530,7 @@ class MetricsTests: XCTestCase {
530530

531531
let testMeterAgain = meterAgain._handler as! TestMeter
532532
XCTAssertEqual(testMeterAgain.values.count, 1, "expected number of entries to match")
533-
XCTAssertEqual(testMeterAgain.values.first!.1, -value, "expected value to match")
533+
XCTAssertEqual(testMeterAgain.values.first, -value, "expected value to match")
534534

535535
let identityAgain = ObjectIdentifier(testMeterAgain)
536536
XCTAssertNotEqual(identity, identityAgain, "since the cached metric was released, the created a new should have a different identity")
@@ -548,7 +548,7 @@ class MetricsTests: XCTestCase {
548548

549549
let testCounter = counter._handler as! TestCounter
550550
XCTAssertEqual(testCounter.values.count, 1, "expected number of entries to match")
551-
XCTAssertEqual(testCounter.values.first!.1, Int64(value), "expected value to match")
551+
XCTAssertEqual(testCounter.values.first, Int64(value), "expected value to match")
552552
XCTAssertEqual(metrics.counters.count, 1, "counter should have been stored")
553553

554554
let identity = ObjectIdentifier(counter)
@@ -560,7 +560,7 @@ class MetricsTests: XCTestCase {
560560

561561
let testCounterAgain = counterAgain._handler as! TestCounter
562562
XCTAssertEqual(testCounterAgain.values.count, 1, "expected number of entries to match")
563-
XCTAssertEqual(testCounterAgain.values.first!.1, Int64(value), "expected value to match")
563+
XCTAssertEqual(testCounterAgain.values.first, Int64(value), "expected value to match")
564564

565565
let identityAgain = ObjectIdentifier(counterAgain)
566566
XCTAssertNotEqual(identity, identityAgain, "since the cached metric was released, the created a new should have a different identity")

Tests/MetricsTests/MetricsTests.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
@testable import CoreMetrics
1616
@testable import Metrics
17-
@testable import MetricsTestKit
17+
import MetricsTestKit
1818
import XCTest
1919

2020
class MetricsExtensionsTests: XCTestCase {
@@ -132,25 +132,25 @@ class MetricsExtensionsTests: XCTestCase {
132132
let testTimer = timer._handler as! TestTimer
133133

134134
testTimer.preferDisplayUnit(.nanoseconds)
135-
XCTAssertEqual(testTimer.retrieveValueInPreferredUnit(atIndex: 0), value * 1000 * 1000 * 1000, accuracy: 1.0, "expected value to match")
135+
XCTAssertEqual(testTimer.valueInPreferredUnit(atIndex: 0), value * 1000 * 1000 * 1000, accuracy: 1.0, "expected value to match")
136136

137137
testTimer.preferDisplayUnit(.microseconds)
138-
XCTAssertEqual(testTimer.retrieveValueInPreferredUnit(atIndex: 0), value * 1000 * 1000, accuracy: 0.001, "expected value to match")
138+
XCTAssertEqual(testTimer.valueInPreferredUnit(atIndex: 0), value * 1000 * 1000, accuracy: 0.001, "expected value to match")
139139

140140
testTimer.preferDisplayUnit(.milliseconds)
141-
XCTAssertEqual(testTimer.retrieveValueInPreferredUnit(atIndex: 0), value * 1000, accuracy: 0.000001, "expected value to match")
141+
XCTAssertEqual(testTimer.valueInPreferredUnit(atIndex: 0), value * 1000, accuracy: 0.000001, "expected value to match")
142142

143143
testTimer.preferDisplayUnit(.seconds)
144-
XCTAssertEqual(testTimer.retrieveValueInPreferredUnit(atIndex: 0), value, accuracy: 0.000000001, "expected value to match")
144+
XCTAssertEqual(testTimer.valueInPreferredUnit(atIndex: 0), value, accuracy: 0.000000001, "expected value to match")
145145

146146
testTimer.preferDisplayUnit(.minutes)
147-
XCTAssertEqual(testTimer.retrieveValueInPreferredUnit(atIndex: 0), value / 60, accuracy: 0.000000001, "expected value to match")
147+
XCTAssertEqual(testTimer.valueInPreferredUnit(atIndex: 0), value / 60, accuracy: 0.000000001, "expected value to match")
148148

149149
testTimer.preferDisplayUnit(.hours)
150-
XCTAssertEqual(testTimer.retrieveValueInPreferredUnit(atIndex: 0), value / (60 * 60), accuracy: 0.000000001, "expected value to match")
150+
XCTAssertEqual(testTimer.valueInPreferredUnit(atIndex: 0), value / (60 * 60), accuracy: 0.000000001, "expected value to match")
151151

152152
testTimer.preferDisplayUnit(.days)
153-
XCTAssertEqual(testTimer.retrieveValueInPreferredUnit(atIndex: 0), value / (60 * 60 * 24), accuracy: 0.000000001, "expected value to match")
153+
XCTAssertEqual(testTimer.valueInPreferredUnit(atIndex: 0), value / (60 * 60 * 24), accuracy: 0.000000001, "expected value to match")
154154
}
155155
}
156156

Tests/MetricsTests/TestSendable.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
@testable import CoreMetrics
1616
import Dispatch
17-
@testable import MetricsTestKit
17+
import MetricsTestKit
1818
import XCTest
1919

2020
class SendableTest: XCTestCase {
@@ -32,7 +32,7 @@ class SendableTest: XCTestCase {
3232
let task = Task.detached { () -> [Int64] in
3333
counter.increment(by: value)
3434
let handler = counter._handler as! TestCounter
35-
return handler.values.map { $0.1 }
35+
return handler.values
3636
}
3737
let values = await task.value
3838
XCTAssertEqual(values.count, 1, "expected number of entries to match")
@@ -61,7 +61,7 @@ class SendableTest: XCTestCase {
6161
let task = Task.detached { () -> [Double] in
6262
recorder.record(value)
6363
let handler = recorder._handler as! TestRecorder
64-
return handler.values.map { $0.1 }
64+
return handler.values
6565
}
6666
let values = await task.value
6767
XCTAssertEqual(values.count, 1, "expected number of entries to match")
@@ -76,7 +76,7 @@ class SendableTest: XCTestCase {
7676
let task = Task.detached { () -> [Double] in
7777
meter.set(value)
7878
let handler = meter._handler as! TestMeter
79-
return handler.values.map { $0.1 }
79+
return handler.values
8080
}
8181
let values = await task.value
8282
XCTAssertEqual(values.count, 1, "expected number of entries to match")

0 commit comments

Comments
 (0)