Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set Xcode version
run: sudo xcode-select -s /Applications/Xcode_16.4.app
run: sudo xcode-select -s /Applications/Xcode_26.0.1.app

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

- name: Build and run tests for the matrix
run: xcodebuild ${{ matrix.xcodebuild-command }} -scheme NautilusTelemetry-Package -destination "${{ matrix.xcodebuild-destination }}"

7 changes: 6 additions & 1 deletion Sources/NautilusTelemetry/Utilities/TimeReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public struct TimeReference {
/// I.e., if the server clock is exactly one hour ahead, this value should be `3600.0`.
/// - Parameter serverOffset: time offset to server.
public init(serverOffset: TimeInterval) {
serverOffsetNanos = Int64(serverOffset * Double(NSEC_PER_SEC))
if serverOffset.isFinite {
serverOffsetNanos = Int64(serverOffset * Double(NSEC_PER_SEC))
} else {
assertionFailure("expected finite serverOffset")
serverOffsetNanos = 0
}
}

// MARK: Internal
Expand Down
32 changes: 25 additions & 7 deletions Tests/NautilusTelemetryTests/Utilities/TimeReferenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
// Created by Ladd Van Tol on 3/22/22.
//

import XCTest
import Foundation
import Testing
@testable import NautilusTelemetry

final class TimeReferenceTests: XCTestCase {
@Suite("TimeReference Tests")
struct TimeReferenceTests {

let toleranceMS: Int64 = 500

func testTimeReference() {
@Test("TimeReference nanoseconds conversion")
func timeReference() {
let timeReference = TimeReference(serverOffset: 0)

let time = ContinuousClock.now
Expand All @@ -21,21 +24,36 @@ final class TimeReferenceTests: XCTestCase {
let nanosecondsSinceEpoch = timeReference.nanosecondsSinceEpoch(from: time)
let nanosecondsSinceEpochFromDate = Int64(date.timeIntervalSince1970 * 1_000_000_000.0)
let diff3 = abs(nanosecondsSinceEpoch - nanosecondsSinceEpochFromDate)
XCTAssertLessThan(diff3, toleranceMS * 1_000_000)
#expect(diff3 < toleranceMS * 1_000_000)
}

func testNanosecondConversion() {
@Test("Nanosecond duration conversion")
func nanosecondConversion() {
let time1 = ContinuousClock.now
print("something very short")
let time2 = ContinuousClock.now

XCTAssert(time1 < time2)
#expect(time1 < time2)

let elapsed = time2 - time1
let elapsedInverse = time1 - time2

XCTAssertEqual(elapsed.asNanoseconds, -elapsedInverse.asNanoseconds)
#expect(elapsed.asNanoseconds == -elapsedInverse.asNanoseconds)

// Can't really assert exact timings without making the test flakey
}

#if os(macOS)
@Test("Infinite and NaN")
func infiniteAndNan() async {
Comment thread
ladd marked this conversation as resolved.
_ = await #expect(processExitsWith: .failure) {
_ = TimeReference(serverOffset: Double.infinity)
}

_ = await #expect(processExitsWith: .failure) {
_ = TimeReference(serverOffset: Double.nan)
}
}
#endif

}