From d31f5d434a7a87bebeb7ace235fb2d6b8a2fd817 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Fri, 5 Jan 2024 09:46:15 -0800 Subject: [PATCH] Fix access control A few APIs were `public` when they shouldn't be. They aren't primary APIs, and it is unlikely that folks rely on them. So though this is a breaking change, it shouldn't affect folks, and there is an alternative API they can rely on instead. --- Sources/Clocks/AnyClock.swift | 4 ++-- Sources/Clocks/ImmediateClock.swift | 10 +++++----- Sources/Clocks/TestClock.swift | 2 +- Tests/ClocksTests/ImmediateClockTests.swift | 3 ++- Tests/ClocksTests/TestClocksTests.swift | 3 ++- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Sources/Clocks/AnyClock.swift b/Sources/Clocks/AnyClock.swift index 5b2f4244..ace96939 100644 --- a/Sources/Clocks/AnyClock.swift +++ b/Sources/Clocks/AnyClock.swift @@ -61,7 +61,7 @@ self._sleep = { try await clock.sleep(until: start.advanced(by: $0.offset), tolerance: $1) } } - public var minimumResolution: Instant.Duration { + public var minimumResolution: Duration { self._minimumResolution() } @@ -69,7 +69,7 @@ self._now() } - public func sleep(until deadline: Instant, tolerance: Instant.Duration? = nil) async throws { + public func sleep(until deadline: Instant, tolerance: Duration? = nil) async throws { try await self._sleep(deadline, tolerance) } } diff --git a/Sources/Clocks/ImmediateClock.swift b/Sources/Clocks/ImmediateClock.swift index 78b532b6..6ecee1e5 100644 --- a/Sources/Clocks/ImmediateClock.swift +++ b/Sources/Clocks/ImmediateClock.swift @@ -114,8 +114,8 @@ Duration: Hashable { public struct Instant: InstantProtocol { - public let offset: Duration - + fileprivate let offset: Duration + public init(offset: Duration = .zero) { self.offset = offset } @@ -133,15 +133,15 @@ } } - public var now: Instant - public var minimumResolution: Duration = .zero + public private(set) var now: Instant + public private(set) var minimumResolution: Duration = .zero private let lock = NSLock() public init(now: Instant = .init()) { self.now = now } - public func sleep(until deadline: Instant, tolerance: Instant.Duration?) async throws { + public func sleep(until deadline: Instant, tolerance: Duration?) async throws { try Task.checkCancellation() self.lock.sync { self.now = deadline } await Task.megaYield() diff --git a/Sources/Clocks/TestClock.swift b/Sources/Clocks/TestClock.swift index aa725d41..3b586c94 100644 --- a/Sources/Clocks/TestClock.swift +++ b/Sources/Clocks/TestClock.swift @@ -66,7 +66,7 @@ @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) public final class TestClock: Clock, @unchecked Sendable { public struct Instant: InstantProtocol { - public let offset: Duration + fileprivate let offset: Duration public init(offset: Duration = .zero) { self.offset = offset diff --git a/Tests/ClocksTests/ImmediateClockTests.swift b/Tests/ClocksTests/ImmediateClockTests.swift index 47595dc5..38d7671b 100644 --- a/Tests/ClocksTests/ImmediateClockTests.swift +++ b/Tests/ClocksTests/ImmediateClockTests.swift @@ -22,8 +22,9 @@ final class ImmediateClockTests: XCTestCase { func testNow() async throws { let clock = ImmediateClock() + let start = clock.now try await clock.sleep(for: .seconds(5)) - XCTAssertEqual(clock.now.offset, .seconds(5)) + XCTAssertEqual(start.advanced(by: .seconds(5)), clock.now) } func testCooperativeCancellation() async throws { diff --git a/Tests/ClocksTests/TestClocksTests.swift b/Tests/ClocksTests/TestClocksTests.swift index cbbf8fba..29b47dd0 100644 --- a/Tests/ClocksTests/TestClocksTests.swift +++ b/Tests/ClocksTests/TestClocksTests.swift @@ -186,8 +186,9 @@ final class TestClockTests: XCTestCase, @unchecked Sendable { let task = Task { try await self.clock.sleep(for: .seconds(5)) } + let start = self.clock.now await self.clock.advance(by: .seconds(5)) - XCTAssertEqual(self.clock.now.offset, .seconds(5)) + XCTAssertEqual(start.advanced(by: .seconds(5)), self.clock.now) try await task.value }