Skip to content

Commit 1b49341

Browse files
authored
Convert package registry tests to async/await (#7135)
Move `PackageRegistryTests` to `async`/`await` ### Motivation: To keep moving things from `temp_await` to actual `await` ### Modifications: Add `async` alternatives to registry methods Mark non-async methods `noasync` to encourage migration ### Result: All, but one use of `temp_await` is removed from the tests
1 parent 6bc77dc commit 1b49341

13 files changed

+1039
-832
lines changed

Sources/Basics/Concurrency/ConcurrencyHelpers.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extension DispatchQueue {
4848
}
4949

5050
/// Bridges between potentially blocking methods that take a result completion closure and async/await
51-
public func safe_async<T, ErrorType: Error>(_ body: @escaping (@escaping (Result<T, ErrorType>) -> Void) -> Void) async throws -> T {
51+
public func safe_async<T, ErrorType: Error>(_ body: @Sendable @escaping (@Sendable @escaping (Result<T, ErrorType>) -> Void) -> Void) async throws -> T {
5252
try await withCheckedThrowingContinuation { continuation in
5353
// It is possible that body make block indefinitely on a lock, sempahore,
5454
// or similar then synchrously call the completion handler. For full safety

Sources/Basics/FileSystem/TemporaryFile.swift

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public func withTemporaryDirectory<Result>(
6666
/// return value for the `withTemporaryDirectory` function.
6767
///
6868
/// - Throws: An error when creating directory and rethrows all errors from `body`.
69+
@discardableResult
6970
public func withTemporaryDirectory<Result>(
7071
fileSystem: FileSystem = localFileSystem,
7172
dir: AbsolutePath? = nil,

Sources/PackageRegistry/ChecksumTOFU.swift

+49-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,30 @@ struct PackageVersionChecksumTOFU {
3636
}
3737

3838
// MARK: - source archive
39-
39+
func validateSourceArchive(
40+
registry: Registry,
41+
package: PackageIdentity.RegistryIdentity,
42+
version: Version,
43+
checksum: String,
44+
timeout: DispatchTimeInterval?,
45+
observabilityScope: ObservabilityScope,
46+
callbackQueue: DispatchQueue
47+
) async throws {
48+
try await safe_async {
49+
self.validateSourceArchive(
50+
registry: registry,
51+
package: package,
52+
version: version,
53+
checksum: checksum,
54+
timeout: timeout,
55+
observabilityScope: observabilityScope,
56+
callbackQueue: callbackQueue,
57+
completion: $0
58+
)
59+
}
60+
}
61+
62+
@available(*, noasync, message: "Use the async alternative")
4063
func validateSourceArchive(
4164
registry: Registry,
4265
package: PackageIdentity.RegistryIdentity,
@@ -135,7 +158,31 @@ struct PackageVersionChecksumTOFU {
135158
}
136159

137160
// MARK: - manifests
138-
161+
func validateManifest(
162+
registry: Registry,
163+
package: PackageIdentity.RegistryIdentity,
164+
version: Version,
165+
toolsVersion: ToolsVersion?,
166+
checksum: String,
167+
timeout: DispatchTimeInterval?,
168+
observabilityScope: ObservabilityScope,
169+
callbackQueue: DispatchQueue
170+
) async throws {
171+
try await safe_async {
172+
self.validateManifest(
173+
registry: registry,
174+
package: package,
175+
version: version,
176+
toolsVersion: toolsVersion,
177+
checksum: checksum,
178+
timeout: timeout,
179+
observabilityScope: observabilityScope,
180+
callbackQueue: callbackQueue,
181+
completion: $0
182+
)
183+
}
184+
}
185+
@available(*, noasync, message: "Use the async alternative")
139186
func validateManifest(
140187
registry: Registry,
141188
package: PackageIdentity.RegistryIdentity,

Sources/PackageRegistry/RegistryClient.swift

+195-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,25 @@ public final class RegistryClient: Cancellable {
147147
callback: completion
148148
)
149149
}
150+
151+
public func getPackageMetadata(
152+
package: PackageIdentity,
153+
timeout: DispatchTimeInterval? = .none,
154+
observabilityScope: ObservabilityScope,
155+
callbackQueue: DispatchQueue
156+
) async throws -> PackageMetadata {
157+
try await safe_async {
158+
self.getPackageMetadata(
159+
package: package,
160+
timeout: timeout,
161+
observabilityScope: observabilityScope,
162+
callbackQueue: callbackQueue,
163+
completion: $0
164+
)
165+
}
166+
}
150167

168+
@available(*, noasync, message: "Use the async alternative")
151169
public func getPackageMetadata(
152170
package: PackageIdentity,
153171
timeout: DispatchTimeInterval? = .none,
@@ -260,7 +278,29 @@ public final class RegistryClient: Cancellable {
260278
)
261279
}
262280
}
281+
282+
public func getPackageVersionMetadata(
283+
package: PackageIdentity,
284+
version: Version,
285+
timeout: DispatchTimeInterval? = .none,
286+
fileSystem: FileSystem,
287+
observabilityScope: ObservabilityScope,
288+
callbackQueue: DispatchQueue
289+
) async throws -> PackageVersionMetadata {
290+
try await safe_async {
291+
self.getPackageVersionMetadata(
292+
package: package,
293+
version: version,
294+
timeout: timeout,
295+
fileSystem: fileSystem,
296+
observabilityScope: observabilityScope,
297+
callbackQueue: callbackQueue,
298+
completion: $0
299+
)
300+
}
301+
}
263302

303+
@available(*, noasync, message: "Use the async alternative")
264304
public func getPackageVersionMetadata(
265305
package: PackageIdentity,
266306
version: Version,
@@ -462,6 +502,26 @@ public final class RegistryClient: Cancellable {
462502
}
463503
}
464504

505+
public func getAvailableManifests(
506+
package: PackageIdentity,
507+
version: Version,
508+
timeout: DispatchTimeInterval? = .none,
509+
observabilityScope: ObservabilityScope,
510+
callbackQueue: DispatchQueue
511+
) async throws -> [String: (toolsVersion: ToolsVersion, content: String?)]{
512+
try await safe_async {
513+
self.getAvailableManifests(
514+
package: package,
515+
version: version,
516+
timeout: timeout,
517+
observabilityScope: observabilityScope,
518+
callbackQueue: callbackQueue,
519+
completion: $0
520+
)
521+
}
522+
}
523+
524+
@available(*, noasync, message: "Use the async alternative")
465525
public func getAvailableManifests(
466526
package: PackageIdentity,
467527
version: Version,
@@ -689,7 +749,28 @@ public final class RegistryClient: Cancellable {
689749
}
690750
}
691751
}
692-
752+
public func getManifestContent(
753+
package: PackageIdentity,
754+
version: Version,
755+
customToolsVersion: ToolsVersion?,
756+
timeout: DispatchTimeInterval? = .none,
757+
observabilityScope: ObservabilityScope,
758+
callbackQueue: DispatchQueue
759+
) async throws -> String {
760+
try await safe_async {
761+
self.getManifestContent(
762+
package: package,
763+
version: version,
764+
customToolsVersion: customToolsVersion,
765+
timeout: timeout,
766+
observabilityScope: observabilityScope,
767+
callbackQueue: callbackQueue,
768+
completion: $0
769+
)
770+
}
771+
}
772+
773+
@available(*, noasync, message: "Use the async alternative")
693774
public func getManifestContent(
694775
package: PackageIdentity,
695776
version: Version,
@@ -898,7 +979,32 @@ public final class RegistryClient: Cancellable {
898979
}
899980
}
900981
}
982+
public func downloadSourceArchive(
983+
package: PackageIdentity,
984+
version: Version,
985+
destinationPath: AbsolutePath,
986+
progressHandler: ((_ bytesReceived: Int64, _ totalBytes: Int64?) -> Void)?,
987+
timeout: DispatchTimeInterval? = .none,
988+
fileSystem: FileSystem,
989+
observabilityScope: ObservabilityScope,
990+
callbackQueue: DispatchQueue
991+
) async throws {
992+
try await safe_async {
993+
self.downloadSourceArchive(
994+
package: package,
995+
version: version,
996+
destinationPath: destinationPath,
997+
progressHandler: progressHandler,
998+
timeout: timeout,
999+
fileSystem: fileSystem,
1000+
observabilityScope: observabilityScope,
1001+
callbackQueue: callbackQueue,
1002+
completion: $0
1003+
)
1004+
}
1005+
}
9011006

1007+
@available(*, noasync, message: "Use the async alternative")
9021008
public func downloadSourceArchive(
9031009
package: PackageIdentity,
9041010
version: Version,
@@ -1189,7 +1295,25 @@ public final class RegistryClient: Cancellable {
11891295
}
11901296
}
11911297
}
1298+
1299+
public func lookupIdentities(
1300+
scmURL: SourceControlURL,
1301+
timeout: DispatchTimeInterval? = .none,
1302+
observabilityScope: ObservabilityScope,
1303+
callbackQueue: DispatchQueue
1304+
) async throws -> Set<PackageIdentity> {
1305+
try await safe_async {
1306+
self.lookupIdentities(
1307+
scmURL: scmURL,
1308+
timeout: timeout,
1309+
observabilityScope: observabilityScope,
1310+
callbackQueue: callbackQueue,
1311+
completion: $0
1312+
)
1313+
}
1314+
}
11921315

1316+
@available(*, noasync, message: "Use the async alternative")
11931317
public func lookupIdentities(
11941318
scmURL: SourceControlURL,
11951319
timeout: DispatchTimeInterval? = .none,
@@ -1294,7 +1418,25 @@ public final class RegistryClient: Cancellable {
12941418
)
12951419
}
12961420
}
1421+
1422+
public func login(
1423+
loginURL: URL,
1424+
timeout: DispatchTimeInterval? = .none,
1425+
observabilityScope: ObservabilityScope,
1426+
callbackQueue: DispatchQueue
1427+
) async throws {
1428+
try await safe_async {
1429+
self.login(
1430+
loginURL: loginURL,
1431+
timeout: timeout,
1432+
observabilityScope: observabilityScope,
1433+
callbackQueue: callbackQueue,
1434+
completion: $0
1435+
)
1436+
}
1437+
}
12971438

1439+
@available(*, noasync, message: "Use the async alternative")
12981440
public func login(
12991441
loginURL: URL,
13001442
timeout: DispatchTimeInterval? = .none,
@@ -1331,7 +1473,41 @@ public final class RegistryClient: Cancellable {
13311473
}
13321474
}
13331475
}
1476+
1477+
public func publish(
1478+
registryURL: URL,
1479+
packageIdentity: PackageIdentity,
1480+
packageVersion: Version,
1481+
packageArchive: AbsolutePath,
1482+
packageMetadata: AbsolutePath?,
1483+
signature: [UInt8]?,
1484+
metadataSignature: [UInt8]?,
1485+
signatureFormat: SignatureFormat?,
1486+
timeout: DispatchTimeInterval? = .none,
1487+
fileSystem: FileSystem,
1488+
observabilityScope: ObservabilityScope,
1489+
callbackQueue: DispatchQueue
1490+
) async throws -> PublishResult {
1491+
try await safe_async {
1492+
self.publish(
1493+
registryURL: registryURL,
1494+
packageIdentity: packageIdentity,
1495+
packageVersion: packageVersion,
1496+
packageArchive: packageArchive,
1497+
packageMetadata: packageMetadata,
1498+
signature: signature,
1499+
metadataSignature: metadataSignature,
1500+
signatureFormat: signatureFormat,
1501+
timeout: timeout,
1502+
fileSystem: fileSystem,
1503+
observabilityScope: observabilityScope,
1504+
callbackQueue: callbackQueue,
1505+
completion: $0
1506+
)
1507+
}
1508+
}
13341509

1510+
@available(*, noasync, message: "Use the async alternative")
13351511
public func publish(
13361512
registryURL: URL,
13371513
packageIdentity: PackageIdentity,
@@ -1499,8 +1675,26 @@ public final class RegistryClient: Cancellable {
14991675
)
15001676
}
15011677
}
1678+
1679+
func checkAvailability(
1680+
registry: Registry,
1681+
timeout: DispatchTimeInterval? = .none,
1682+
observabilityScope: ObservabilityScope,
1683+
callbackQueue: DispatchQueue
1684+
) async throws -> AvailabilityStatus {
1685+
try await safe_async {
1686+
self.checkAvailability(
1687+
registry: registry,
1688+
timeout: timeout,
1689+
observabilityScope: observabilityScope,
1690+
callbackQueue: callbackQueue,
1691+
completion: $0
1692+
)
1693+
}
1694+
}
15021695

15031696
// marked internal for testing
1697+
@available(*, noasync, message: "Use the async alternative")
15041698
func checkAvailability(
15051699
registry: Registry,
15061700
timeout: DispatchTimeInterval? = .none,

Sources/PackageRegistry/RegistryDownloadsManager.swift

+20
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,27 @@ public class RegistryDownloadsManager: Cancellable {
4343
self.registryClient = registryClient
4444
self.delegate = delegate
4545
}
46+
47+
public func lookup(
48+
package: PackageIdentity,
49+
version: Version,
50+
observabilityScope: ObservabilityScope,
51+
delegateQueue: DispatchQueue,
52+
callbackQueue: DispatchQueue
53+
) async throws -> AbsolutePath {
54+
try await safe_async {
55+
self.lookup(
56+
package: package,
57+
version: version,
58+
observabilityScope: observabilityScope,
59+
delegateQueue: delegateQueue,
60+
callbackQueue: callbackQueue,
61+
completion: $0
62+
)
63+
}
64+
}
4665

66+
@available(*, noasync, message: "Use the async alternative")
4767
public func lookup(
4868
package: PackageIdentity,
4969
version: Version,

0 commit comments

Comments
 (0)