Skip to content

Commit

Permalink
Refactored Task.sleep nanosecond clamping math to new `Task.sleep(s…
Browse files Browse the repository at this point in the history
…econds:)` method
  • Loading branch information
orchetect committed Oct 28, 2024
1 parent 9e16de5 commit b72afa0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
9 changes: 1 addition & 8 deletions Sources/OSCKit/OSCServerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,15 @@ extension _OSCServerProtocol {
timeTag: OSCTimeTag,
at secondsFromNow: TimeInterval
) async {
var secondsFromNow = secondsFromNow

// clamp lower bound to 0
guard secondsFromNow > 0 else {
// don't schedule, just dispatch it immediately
await _dispatch(message, timeTag: timeTag)
return
}

// safety check: protect again overflow
let maxSeconds = TimeInterval(UInt64.max / 1_000_000_000)
secondsFromNow = min(secondsFromNow, maxSeconds)
let nanoseconds = UInt64(secondsFromNow * 1_000_000_000)

Task {
try? await Task.sleep(nanoseconds: nanoseconds)
try? await Task.sleep(seconds: secondsFromNow)
await self.handler?(message, timeTag)
}
}
Expand Down
23 changes: 23 additions & 0 deletions Sources/OSCKitCore/Utilities/Concurrency Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Concurrency Extensions.swift
// OSCKit
//
// Created by Steffan Andrews on 2024-10-28.
//

import Foundation

fileprivate let maxSeconds = TimeInterval(UInt64.max / 1_000_000_000)

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension Task where Success == Never, Failure == Never {
/// Suspends the current task for at least the given duration in seconds.
package static func sleep(seconds: TimeInterval) async throws {
// safety check: protect again overflow

let secondsClamped = min(seconds, maxSeconds)
let nanoseconds = UInt64(secondsClamped * 1_000_000_000)

try await sleep(nanoseconds: nanoseconds)
}
}
2 changes: 1 addition & 1 deletion Tests/OSCKitTests/OSCServer Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(1))
try await Task.sleep(seconds: 1)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/OSCKitTests/OSCSocket Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(1))
try await Task.sleep(seconds: 1)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions Tests/OSCKitTests/OSCTimeTag OSC 1.0 Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(0.5))
try await Task.sleep(seconds: 0.5)
}
}

Expand All @@ -43,7 +43,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(0.5))
try await Task.sleep(seconds: 0.5)
}
}

Expand All @@ -62,7 +62,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(0.5))
try await Task.sleep(seconds: 0.5)
}
}

Expand All @@ -83,7 +83,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(0.9)) // just under 1 second
try await Task.sleep(seconds: 0.9) // just under 1 second
}
}

Expand All @@ -105,7 +105,7 @@ import Testing
await server._handle(payload: bundle)

// Note: this may be flaky on slow CI systems
try await Task.sleep(for: .seconds(1.1)) // just over 1 second
try await Task.sleep(seconds: 1.1) // just over 1 second
}
}

Expand All @@ -124,7 +124,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(0.5))
try await Task.sleep(seconds: 0.5)
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/OSCKitTests/OSCTimeTag OSC 1.1 Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(0.5))
try await Task.sleep(seconds: 0.5)
}
}

Expand All @@ -43,7 +43,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(0.5))
try await Task.sleep(seconds: 0.5)
}
}

Expand All @@ -62,7 +62,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(0.5))
try await Task.sleep(seconds: 0.5)
}
}

Expand All @@ -81,7 +81,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(0.5))
try await Task.sleep(seconds: 0.5)
}
}

Expand All @@ -100,7 +100,7 @@ import Testing

await server._handle(payload: bundle)

try await Task.sleep(for: .seconds(0.5))
try await Task.sleep(seconds: 0.5)
}
}
}
Expand Down

0 comments on commit b72afa0

Please sign in to comment.