[Swift] Migrate uuid metric to UniFFI

This commit is contained in:
Jan-Erik Rediger 2022-02-21 12:56:24 +01:00 коммит произвёл Jan-Erik Rediger
Родитель 5db0f79f1c
Коммит 2d14c6f243
2 изменённых файлов: 26 добавлений и 76 удалений

Просмотреть файл

@ -2,8 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
/// This implements the developer facing API for recording UUID metrics.
///
/// Instances of this class type are automatically generated by the parsers at build time,
@ -12,43 +10,19 @@ import Foundation
/// The UUID API only exposes the `UuidMetricType.generateAndSet()` and `UuidMetricType.set(_:)` methods,
/// which takes care of validating the input data and making sure that limits are enforced.
public class UuidMetricType {
let handle: UInt64
let disabled: Bool
let sendInPings: [String]
let inner: UuidMetric
/// The public constructor used by automatically generated metrics.
public init(category: String, name: String, sendInPings: [String], lifetime: Lifetime, disabled: Bool) {
self.disabled = disabled
self.sendInPings = sendInPings
self.handle = withArrayOfCStrings(sendInPings) { pingArray in
glean_new_uuid_metric(
category,
name,
pingArray,
Int32(sendInPings.count),
lifetime.rawValue,
disabled.toByte()
)
}
}
/// Destroy this metric.
deinit {
if self.handle != 0 {
glean_destroy_uuid_metric(self.handle)
}
public init(_ meta: CommonMetricData) {
self.inner = UuidMetric(meta)
}
/// Generate a new UUID and set it in the metric store.
///
/// - returns: The `UUID` that was generated or `nil` if disabled.
public func generateAndSet() -> UUID? {
guard !self.disabled else { return nil }
let uuid = UUID()
set(uuid)
return uuid
public func generateAndSet() -> UUID {
let uuid = inner.generateAndSet()
return UUID(uuidString: uuid)!
}
/// Explicitly set an existing UUID value.
@ -56,11 +30,7 @@ public class UuidMetricType {
/// - parameters:
/// * value: A valid `UUID` to set the metric to.
public func set(_ value: UUID) {
guard !self.disabled else { return }
Dispatchers.shared.launchAPI {
glean_uuid_set(self.handle, value.uuidString.lowercased())
}
inner.set(value.uuidString.lowercased())
}
/// Tests whether a value is stored for the metric for testing purposes only. This function will
@ -72,10 +42,7 @@ public class UuidMetricType {
/// Defaults to the first value in `sendInPings`.
/// - returns: true if metric value exists, otherwise false
public func testHasValue(_ pingName: String? = nil) -> Bool {
Dispatchers.shared.assertInTestingMode()
let pingName = pingName ?? self.sendInPings[0]
return glean_uuid_test_has_value(self.handle, pingName).toBool()
return inner.testGetValue(pingName) != nil
}
/// Returns the stored value for testing purposes only. This function will attempt to await the
@ -88,17 +55,8 @@ public class UuidMetricType {
/// Defaults to the first value in `sendInPings`.
///
/// - returns: value of the stored metric
public func testGetValue(_ pingName: String? = nil) throws -> UUID {
Dispatchers.shared.assertInTestingMode()
let pingName = pingName ?? self.sendInPings[0]
if !testHasValue(pingName) {
throw "Missing value"
}
let uuid = String(freeingGleanString: glean_uuid_test_get_value(self.handle, pingName))
public func testGetValue(_ pingName: String? = nil) -> UUID? {
guard let uuid = inner.testGetValue() else { return nil }
return UUID(uuidString: uuid)!
}
}

Просмотреть файл

@ -5,8 +5,6 @@
@testable import Glean
import XCTest
// swiftlint:disable force_cast
// REASON: Used in a test
class UuidMetricTypeTests: XCTestCase {
override func setUp() {
resetGleanDiscardingInitialPings(testCase: self, tag: "UuidMetricTypeTests")
@ -17,83 +15,77 @@ class UuidMetricTypeTests: XCTestCase {
}
func testUuidSavesToStorage() {
let uuidMetric = UuidMetricType(
let uuidMetric = UuidMetricType(CommonMetricData(
category: "telemetry",
name: "uuid_metric",
sendInPings: ["store1"],
lifetime: .application,
disabled: false
)
))
// Check that there is no UUID recorded
XCTAssertFalse(uuidMetric.testHasValue())
XCTAssertNil(uuidMetric.testGetValue())
// Record two UUID's of the same type, with a little delay
let uuid = uuidMetric.generateAndSet()
// Check that the data was properly recorded
XCTAssertTrue(uuidMetric.testHasValue())
XCTAssertEqual(uuid, try uuidMetric.testGetValue())
XCTAssertEqual(uuid, uuidMetric.testGetValue())
let uuid2 = UUID(uuidString: "ce2adeb8-843a-4232-87a5-a099ed1e7bb3")!
uuidMetric.set(uuid2)
// Check that the data was properly recorded
XCTAssertTrue(uuidMetric.testHasValue())
XCTAssertEqual(uuid2, try uuidMetric.testGetValue())
XCTAssertEqual(uuid2, uuidMetric.testGetValue())
}
func testUuidMustNotRecordIfDisabled() {
let uuidMetric = UuidMetricType(
let uuidMetric = UuidMetricType(CommonMetricData(
category: "telemetry",
name: "uuid_metric",
sendInPings: ["store1"],
lifetime: .application,
disabled: true
)
))
XCTAssertFalse(uuidMetric.testHasValue())
XCTAssertNil(uuidMetric.testGetValue())
_ = uuidMetric.generateAndSet()
XCTAssertFalse(uuidMetric.testHasValue(), "UUIDs must not be recorded if they are disabled")
XCTAssertNil(uuidMetric.testGetValue(), "UUIDs must not be recorded if they are disabled")
}
func testUuidGetValueThrowsExceptionIfNothingIsStored() {
let uuidMetric = UuidMetricType(
let uuidMetric = UuidMetricType(CommonMetricData(
category: "telemetry",
name: "uuid_metric",
sendInPings: ["store1"],
lifetime: .application,
disabled: false
)
))
XCTAssertThrowsError(try uuidMetric.testGetValue()) { error in
XCTAssertEqual(error as! String, "Missing value")
}
XCTAssertNil(uuidMetric.testGetValue())
}
func testUuidSavesToSecondaryPings() {
let uuidMetric = UuidMetricType(
let uuidMetric = UuidMetricType(CommonMetricData(
category: "telemetry",
name: "uuid_metric",
sendInPings: ["store1", "store2"],
lifetime: .application,
disabled: false
)
))
// Record two UUID's of the same type, with a little delay
let uuid = uuidMetric.generateAndSet()
// Check that the data was properly recorded
XCTAssertTrue(uuidMetric.testHasValue("store2"))
XCTAssertEqual(uuid, try uuidMetric.testGetValue("store2"))
XCTAssertEqual(uuid, uuidMetric.testGetValue("store2"))
let uuid2 = UUID(uuidString: "ce2adeb8-843a-4232-87a5-a099ed1e7bb3")!
uuidMetric.set(uuid2)
// Check that the data was properly recorded
XCTAssertTrue(uuidMetric.testHasValue("store2"))
XCTAssertEqual(uuid2, try uuidMetric.testGetValue("store2"))
XCTAssertEqual(uuid2, uuidMetric.testGetValue("store2"))
}
}