Skip to content

Commit 4345c0d

Browse files
fix copyrights, remove typos, change sleep to a custom shorthand backportable shorthand without nanoseconds
1 parent 81493be commit 4345c0d

File tree

5 files changed

+69
-30
lines changed

5 files changed

+69
-30
lines changed

Sources/AsyncAlgorithms/AsyncShareSequence.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Async Algorithms open source project
44
//
5-
// Copyright (c) 2022 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -130,7 +130,7 @@ where Base.Element: Sendable, Base: _SendableMetatype, Base.AsyncIterator: _Send
130130
// **Cleanup**: Automatically unregisters and cancels pending operations on deinit
131131
final class Side {
132132
// Due to a runtime crash in 1.0 compatible versions, it's not possible to handle
133-
// a generic failure constrained to Base.Failure. We handle inner failure with a `any Error`
133+
// a generic failure constrained to Base.Failure. We handle inner failure with a `any Error`
134134
// and force unwrap it to the generic 1.2 generic type on the outside Iterator.
135135
typealias Failure = any Error
136136
// Tracks the state of a single consumer's iteration.

Sources/AsyncAlgorithms/Shims.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Async Algorithms open source project
44
//
5-
// Copyright (c) 2022 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information

Tests/AsyncAlgorithmsTests/Support/FailingSequence.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
//===----------------------------------------------------------------------===//
12
//
2-
// FailingSequence.swift
3-
// swift-async-algorithms
3+
// This source file is part of the Swift Async Algorithms open source project
44
//
5-
// Created by Stefano Mondino on 15/10/25.
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
67
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
711

812
@available(AsyncAlgorithms 1.2, *)
913
struct FailingSequence<Failure: Error>: AsyncSequence, Sendable {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Async Algorithms open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
/// Backportable versions of `sleep(for:)` for legacy platforms where this kind of method is not available
13+
extension Task where Success == Never, Failure == Never {
14+
15+
static func sleep(milliseconds duration: UInt64) async throws {
16+
try await sleep(duration, multiplier: 1_000_000)
17+
}
18+
static func sleep(microseconds duration: UInt64) async throws {
19+
try await sleep(duration, multiplier: 1_000)
20+
}
21+
static func sleep(seconds duration: UInt64) async throws {
22+
try await sleep(duration, multiplier: 1_000_000_000)
23+
}
24+
25+
private static func sleep(_ value: UInt64, multiplier: UInt64) async throws {
26+
guard UInt64.max / multiplier > value else {
27+
throw SleepError.durationOutOfBounds
28+
}
29+
try await sleep(nanoseconds: value * multiplier)
30+
}
31+
}
32+
33+
fileprivate enum SleepError: Error {
34+
case durationOutOfBounds
35+
}

Tests/AsyncAlgorithmsTests/TestShare.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ final class TestShare: XCTestCase {
9898
results1.withLock { $0.append(value) }
9999
}
100100
// Delay to allow consumer 2 to get ahead
101-
try? await Task.sleep(nanoseconds: 10_000_000)
101+
try? await Task.sleep(milliseconds: 10)
102102
// Continue reading
103103
while let value = await iterator.next() {
104104
results1.withLock { $0.append(value) }
@@ -146,7 +146,7 @@ final class TestShare: XCTestCase {
146146
while let value = await iterator.next() {
147147
results1.withLock { $0.append(value) }
148148
// Add some delay to consumer 1
149-
try? await Task.sleep(nanoseconds: 1_000_000)
149+
try? await Task.sleep(milliseconds: 1)
150150
}
151151
}
152152

@@ -193,7 +193,7 @@ final class TestShare: XCTestCase {
193193
slowResults.withLock { $0.append(value) }
194194
}
195195
// Add significant delay to let buffer fill up and potentially overflow
196-
try? await Task.sleep(nanoseconds: 50_000_000)
196+
try? await Task.sleep(milliseconds: 50)
197197
// Continue reading remaining elements
198198
while let value = await iterator.next() {
199199
slowResults.withLock { $0.append(value) }
@@ -202,13 +202,13 @@ final class TestShare: XCTestCase {
202202

203203
// Release all elements quickly to test buffer overflow behavior
204204
gated.advance() // 1
205-
try? await Task.sleep(nanoseconds: 5_000_000)
205+
try? await Task.sleep(milliseconds: 5)
206206
gated.advance() // 2
207-
try? await Task.sleep(nanoseconds: 5_000_000)
207+
try? await Task.sleep(milliseconds: 5)
208208
gated.advance() // 3
209-
try? await Task.sleep(nanoseconds: 5_000_000)
209+
try? await Task.sleep(milliseconds: 5)
210210
gated.advance() // 4
211-
try? await Task.sleep(nanoseconds: 5_000_000)
211+
try? await Task.sleep(milliseconds: 5)
212212
gated.advance() // 5
213213

214214
await fastConsumer.value
@@ -265,7 +265,7 @@ final class TestShare: XCTestCase {
265265
slowResults.withLock { $0.append(value) }
266266
}
267267
// Add significant delay to let buffer fill up and potentially overflow
268-
try? await Task.sleep(nanoseconds: 50_000_000)
268+
try? await Task.sleep(milliseconds: 50)
269269
// Continue reading remaining elements
270270
while let value = await iterator.next() {
271271
slowResults.withLock { $0.append(value) }
@@ -274,13 +274,13 @@ final class TestShare: XCTestCase {
274274

275275
// Release all elements quickly to test buffer overflow behavior
276276
gated.advance() // 1
277-
try? await Task.sleep(nanoseconds: 5_000_000)
277+
try? await Task.sleep(milliseconds: 5)
278278
gated.advance() // 2
279-
try? await Task.sleep(nanoseconds: 5_000_000)
279+
try? await Task.sleep(milliseconds: 5)
280280
gated.advance() // 3
281-
try? await Task.sleep(nanoseconds: 5_000_000)
281+
try? await Task.sleep(milliseconds: 5)
282282
gated.advance() // 4
283-
try? await Task.sleep(nanoseconds: 5_000_000)
283+
try? await Task.sleep(milliseconds: 5)
284284
gated.advance() // 5
285285

286286
await fastConsumer.value
@@ -490,7 +490,7 @@ final class TestShare: XCTestCase {
490490
gated.advance() // 2
491491

492492
// Give early consumer time to consume
493-
try? await Task.sleep(nanoseconds: 10_000_000)
493+
try? await Task.sleep(milliseconds: 10)
494494

495495
// Start late consumer
496496
let lateConsumer = Task {
@@ -587,18 +587,18 @@ final class TestShare: XCTestCase {
587587
}
588588

589589
func test_share_rethrows_failure_type_without_falling_back_to_any_error() async {
590-
if #available(AsyncAlgorithms 1.2, *) {
591-
// Ensure - at compile time - that error is effectively a TestError
592-
let shared: some AsyncSequence<Void, TestError> = FailingSequence(TestError.failure).share()
593-
do {
594-
for try await _ in shared {
595-
XCTFail("Expected to not get here")
596-
}
597-
} catch {
598-
XCTAssertEqual(error, TestError.failure)
590+
guard #available(AsyncAlgorithms 1.2, *) else {
591+
_ = XCTSkip("This test is not available before 1.2")
592+
return
593+
}
594+
// Ensure - at compile time - that error is effectively a TestError
595+
let shared: some AsyncSequence<Void, TestError> = FailingSequence(TestError.failure).share()
596+
do {
597+
for try await _ in shared {
598+
XCTFail("Expected to not get here")
599599
}
600-
} else {
601-
throw XCTSkip("This test is not available before 1.2")
600+
} catch {
601+
XCTAssertEqual(error, TestError.failure)
602602
}
603603
}
604604
}

0 commit comments

Comments
 (0)