-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix inverted logic on registry cache expiration #9144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
plemarquand
merged 2 commits into
swiftlang:main
from
ZachNagengast:fix-registry-client-cache-expiration
Sep 29, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Basics | ||
@testable import Basics | ||
import _Concurrency | ||
import Foundation | ||
import PackageFingerprint | ||
|
@@ -440,6 +440,126 @@ fileprivate var availabilityURL = URL("\(registryURL)/availability") | |
assert(metadataSync) | ||
} | ||
|
||
@Test func getPackageVersionMetadataInCache() async throws { | ||
let checksumAlgorithm: HashAlgorithm = MockHashAlgorithm() | ||
let expectedChecksums: [Version: String] = [ | ||
Version("1.1.1"): "a2ac54cf25fbc1ad0028f03f0aa4b96833b83bb05a14e510892bb27dea4dc812", | ||
Version("1.1.0"): checksumAlgorithm.hash(emptyZipFile).hexadecimalRepresentation | ||
] | ||
|
||
let counter = SendableBox(0) | ||
let handler: HTTPClient.Implementation = { request, _ in | ||
await counter.increment() | ||
switch (request.method, request.url) { | ||
case (.get, releasesURL.appending(component: "1.1.1")): | ||
let expectedChecksum = expectedChecksums[Version("1.1.1")]! | ||
#expect(request.headers.get("Accept").first == "application/vnd.swift.registry.v1+json") | ||
|
||
let data = """ | ||
{ | ||
"id": "mona.LinkedList", | ||
"version": "1.1.1", | ||
"resources": [ | ||
{ | ||
"name": "source-archive", | ||
"type": "application/zip", | ||
"checksum": "\(expectedChecksum)" | ||
} | ||
], | ||
"metadata": { | ||
"author": { | ||
"name": "J. Appleseed" | ||
}, | ||
"licenseURL": "https://github.com/mona/LinkedList/license", | ||
"readmeURL": "https://github.com/mona/LinkedList/readme", | ||
"repositoryURLs": [ | ||
"https://github.com/mona/LinkedList", | ||
"ssh://[email protected]:mona/LinkedList.git", | ||
"[email protected]:mona/LinkedList.git" | ||
] | ||
} | ||
} | ||
""".data(using: .utf8)! | ||
|
||
return .init( | ||
statusCode: 200, | ||
headers: .init([ | ||
.init(name: "Content-Length", value: "\(data.count)"), | ||
.init(name: "Content-Type", value: "application/json"), | ||
.init(name: "Content-Version", value: "1"), | ||
]), | ||
body: data | ||
) | ||
case (.get, releasesURL.appending(component: "1.1.0")): | ||
let expectedChecksum = expectedChecksums[Version("1.1.0")]! | ||
#expect(request.headers.get("Accept").first == "application/vnd.swift.registry.v1+json") | ||
|
||
let data = """ | ||
{ | ||
"id": "mona.LinkedList", | ||
"version": "1.1.0", | ||
"resources": [ | ||
{ | ||
"name": "source-archive", | ||
"type": "application/zip", | ||
"checksum": "\(expectedChecksum)", | ||
} | ||
], | ||
"metadata": { | ||
"author": { | ||
"name": "J. Appleseed" | ||
}, | ||
"licenseURL": "https://github.com/mona/LinkedList/license", | ||
"readmeURL": "https://github.com/mona/LinkedList/readme", | ||
"repositoryURLs": [ | ||
"https://github.com/mona/LinkedList", | ||
"ssh://[email protected]:mona/LinkedList.git", | ||
"[email protected]:mona/LinkedList.git" | ||
] | ||
} | ||
} | ||
""".data(using: .utf8)! | ||
|
||
return .init( | ||
statusCode: 200, | ||
headers: .init([ | ||
.init(name: "Content-Length", value: "\(data.count)"), | ||
.init(name: "Content-Type", value: "application/json"), | ||
.init(name: "Content-Version", value: "1"), | ||
]), | ||
body: data | ||
) | ||
default: | ||
throw StringError("method and url should match") | ||
} | ||
} | ||
|
||
let httpClient = HTTPClient(implementation: handler) | ||
var configuration = RegistryConfiguration() | ||
configuration.defaultRegistry = Registry(url: registryURL, supportsAvailability: false) | ||
|
||
let registryClient = makeRegistryClient(configuration: configuration, httpClient: httpClient) | ||
|
||
var expectedRequestCount = 0 | ||
try await check(version: Version("1.1.1"), expectCached: false) | ||
try await check(version: Version("1.1.0"), expectCached: false) | ||
try await check(version: Version("1.1.1"), expectCached: true) | ||
try await check(version: Version("1.1.0"), expectCached: true) | ||
|
||
func check(version: Version, expectCached: Bool) async throws { | ||
let metadata = try await registryClient.getPackageVersionMetadata(package: identity, version: version) | ||
|
||
if !expectCached { | ||
expectedRequestCount += 1 | ||
} | ||
|
||
let count = await counter.value | ||
#expect(count == expectedRequestCount) | ||
#expect(metadata.author?.name == "J. Appleseed") | ||
#expect(metadata.resources[0].checksum == expectedChecksums[version]!) | ||
} | ||
} | ||
|
||
func getPackageVersionMetadata_404() async throws { | ||
let serverErrorHandler = ServerErrorHandler( | ||
method: .get, | ||
|
@@ -3701,6 +3821,87 @@ fileprivate var availabilityURL = URL("\(registryURL)/availability") | |
try await registryClient.checkAvailability(registry: registry) | ||
} | ||
} | ||
|
||
@Test func withAvailabilityCheck() async throws { | ||
let handler: HTTPClient.Implementation = { request, _ in | ||
switch (request.method, request.url) { | ||
case (.get, availabilityURL): | ||
return .okay() | ||
default: | ||
throw StringError("method and url should match") | ||
} | ||
} | ||
|
||
let httpClient = HTTPClient(implementation: handler) | ||
let registry = Registry(url: registryURL, supportsAvailability: true) | ||
|
||
let registryClient = makeRegistryClient( | ||
configuration: .init(), | ||
httpClient: httpClient | ||
) | ||
|
||
try await registryClient.withAvailabilityCheck( | ||
registry: registry, | ||
observabilityScope: ObservabilitySystem.NOOP | ||
) | ||
} | ||
|
||
@Test func withAvailabilityCheckServerError() async throws { | ||
let handler: HTTPClient.Implementation = { request, _ in | ||
switch (request.method, request.url) { | ||
case (.get, availabilityURL): | ||
return .serverError(reason: "boom") | ||
default: | ||
throw StringError("method and url should match") | ||
} | ||
} | ||
|
||
let httpClient = HTTPClient(implementation: handler) | ||
let registry = Registry(url: registryURL, supportsAvailability: true) | ||
|
||
let registryClient = makeRegistryClient( | ||
configuration: .init(), | ||
httpClient: httpClient | ||
) | ||
|
||
await #expect(throws: StringError("unknown server error (500)")) { | ||
try await registryClient.withAvailabilityCheck( | ||
registry: registry, | ||
observabilityScope: ObservabilitySystem.NOOP | ||
) | ||
} | ||
} | ||
|
||
@Test func withAvailabilityCheckInCache() async throws { | ||
let counter = SendableBox(0) | ||
let handler: HTTPClient.Implementation = { request, _ in | ||
await counter.increment() | ||
switch (request.method, request.url) { | ||
case (.get, availabilityURL): | ||
return .okay() | ||
default: | ||
throw StringError("method and url should match") | ||
} | ||
} | ||
|
||
let httpClient = HTTPClient(implementation: handler) | ||
let registry = Registry(url: registryURL, supportsAvailability: true) | ||
|
||
let registryClient = makeRegistryClient( | ||
configuration: .init(), | ||
httpClient: httpClient | ||
) | ||
|
||
// Request count should not increase after first check | ||
for _ in 0..<5 { | ||
try await registryClient.withAvailabilityCheck( | ||
registry: registry, | ||
observabilityScope: ObservabilitySystem.NOOP | ||
) | ||
let count = await counter.value | ||
#expect(count == 1) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Sugar | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section is just moved below to group internal scoped functions.