Skip to content

Commit

Permalink
remove assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
rauhul authored and ktoso committed Aug 16, 2021
1 parent 42372a8 commit 2c58b01
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 41 deletions.
35 changes: 10 additions & 25 deletions Sources/CoreMetrics/Metrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
//
//===----------------------------------------------------------------------===//

// MARK: Testing API

internal var _enableAssertions = true

// MARK: User API

extension Counter {
Expand Down Expand Up @@ -596,29 +592,18 @@ internal class AccumulatingRoundingFloatingPointCounter: FloatingPointCounterHan
}

func increment(by amount: Double) {
// Drop values in illegal values (Asserting in debug builds)
guard !amount.isNaN else {
assert(!_enableAssertions, "cannot increment by NaN")
return
}

guard !amount.isInfinite else {
assert(!_enableAssertions, "cannot increment by infinite quantities")
return
}

guard amount.sign == .plus else {
assert(!_enableAssertions, "cannot increment by negative values")
return
}

guard !amount.isZero else {
return
}
// Drop illegal values
// - cannot increment by NaN
guard !amount.isNaN else { return }
// - cannot increment by infinite quantities
guard !amount.isInfinite else { return }
// - cannot increment by negative values
guard amount.sign == .plus else { return }
// - cannot increment by zero
guard !amount.isZero else { return }

// If amount is in Int64.max..<+Inf
if amount.exponent >= 63 {
// Ceil to Int64.max
// If amount is in Int64.max..<+Inf, ceil to Int64.max
self.lock.withLockVoid {
self.counterHandler.increment(by: .max)
}
Expand Down
16 changes: 0 additions & 16 deletions Tests/MetricsTests/CoreMetricsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,63 +57,47 @@ class MetricsTests: XCTestCase {
// bootstrap with our test metrics
let metrics = TestMetrics()
MetricsSystem.bootstrapInternal(metrics)
// disable assertions to test fallback path
_enableAssertions = false
let label = "\(#function)-fp-counter-\(UUID())"
let fpCounter = FloatingPointCounter(label: label)
let counter = metrics.counters[label] as! TestCounter
fpCounter.increment(by: Double.nan)
fpCounter.increment(by: Double.signalingNaN)
XCTAssertEqual(counter.values.count, 0, "expected nan values to be ignored")
// reenable assertions
_enableAssertions = true
}

func testDefaultFloatingPointCounter_ignoresInfinity() throws {
// bootstrap with our test metrics
let metrics = TestMetrics()
MetricsSystem.bootstrapInternal(metrics)
// disable assertions to test fallback path
_enableAssertions = false
let label = "\(#function)-fp-counter-\(UUID())"
let fpCounter = FloatingPointCounter(label: label)
let counter = metrics.counters[label] as! TestCounter
fpCounter.increment(by: Double.infinity)
fpCounter.increment(by: -Double.infinity)
XCTAssertEqual(counter.values.count, 0, "expected infinite values to be ignored")
// reenable assertions
_enableAssertions = true
}

func testDefaultFloatingPointCounter_ignoresNegativeValues() throws {
// bootstrap with our test metrics
let metrics = TestMetrics()
MetricsSystem.bootstrapInternal(metrics)
// disable assertions to test fallback path
_enableAssertions = false
let label = "\(#function)-fp-counter-\(UUID())"
let fpCounter = FloatingPointCounter(label: label)
let counter = metrics.counters[label] as! TestCounter
fpCounter.increment(by: -100)
XCTAssertEqual(counter.values.count, 0, "expected negative values to be ignored")
// reenable assertions
_enableAssertions = true
}

func testDefaultFloatingPointCounter_ignoresZero() throws {
// bootstrap with our test metrics
let metrics = TestMetrics()
MetricsSystem.bootstrapInternal(metrics)
// disable assertions to test fallback path
_enableAssertions = false
let label = "\(#function)-fp-counter-\(UUID())"
let fpCounter = FloatingPointCounter(label: label)
let counter = metrics.counters[label] as! TestCounter
fpCounter.increment(by: 0)
fpCounter.increment(by: -0)
XCTAssertEqual(counter.values.count, 0, "expected zero values to be ignored")
// reenable assertions
_enableAssertions = true
}

func testDefaultFloatingPointCounter_ceilsExtremeValues() {
Expand Down

0 comments on commit 2c58b01

Please sign in to comment.