-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Clean WordPress API cache during account and site removal #25864
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
Open
crazytonyli
wants to merge
5
commits into
trunk
Choose a base branch
from
task/wp-api-cache-single-bootstrap
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b09f247
Make WordPress API cache bootstrap recovery safe and testable
crazytonyli c1c29bf
Share a single WordPressApiCache instance across all WordPressClients
crazytonyli b0d3966
Clean WordPress API cache during account and site removal
crazytonyli 270c379
Clear every cache key a site has during removal
crazytonyli 40b973d
Use an isolated cache instead of the shared one in unit tests
crazytonyli 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
124 changes: 124 additions & 0 deletions
124
Modules/Tests/WordPressCoreTests/ApiCacheBootstrapTests.swift
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 |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import Foundation | ||
| import Testing | ||
| import WordPressApiCache | ||
| @testable import WordPressCore | ||
|
|
||
| @Suite | ||
| struct ApiCacheBootstrapTests { | ||
| private let directory: URL | ||
|
|
||
| init() throws { | ||
| directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) | ||
| try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) | ||
| } | ||
|
|
||
| private var databaseURL: URL { | ||
| directory.appending(path: "app.sqlite") | ||
| } | ||
|
|
||
| private var journalURL: URL { | ||
| URL(fileURLWithPath: databaseURL.path + "-journal") | ||
| } | ||
|
|
||
| private var walURL: URL { | ||
| URL(fileURLWithPath: databaseURL.path + "-wal") | ||
| } | ||
|
|
||
| private var shmURL: URL { | ||
| URL(fileURLWithPath: databaseURL.path + "-shm") | ||
| } | ||
|
|
||
| @Test func createsDatabaseOnFirstBootstrap() { | ||
| let outcome = WordPressApiCache.onDiskCache(at: databaseURL) | ||
|
|
||
| guard case .opened = outcome else { | ||
| Issue.record("Expected .opened, got \(outcome)") | ||
| return | ||
| } | ||
| #expect(FileManager.default.fileExists(at: databaseURL)) | ||
| } | ||
|
|
||
| @Test func recoversFromCorruptDatabaseAndRemovesSiblings() throws { | ||
| try Data("not a database".utf8).write(to: databaseURL) | ||
| try Data("stale journal".utf8).write(to: journalURL) | ||
| try Data("stale wal".utf8).write(to: walURL) | ||
| try Data("stale shm".utf8).write(to: shmURL) | ||
|
|
||
| let outcome = WordPressApiCache.onDiskCache(at: databaseURL) | ||
|
|
||
| guard case .recovered(_, let failure) = outcome else { | ||
| Issue.record("Expected .recovered, got \(outcome)") | ||
| return | ||
| } | ||
| // The corrupt file may fail at open or at migration; either is recovery. | ||
| #expect(failure.kind == .couldNotOpenDatabase || failure.kind == .migrationFailed) | ||
| #expect(FileManager.default.fileExists(at: databaseURL)) | ||
| #expect(!FileManager.default.fileExists(at: journalURL)) | ||
| #expect(!FileManager.default.fileExists(at: walURL)) | ||
| #expect(!FileManager.default.fileExists(at: shmURL)) | ||
| } | ||
|
|
||
| @Test func removesOrphanedSiblingsBeforeCreatingDatabase() throws { | ||
| // No database file, only leftovers a previous delete-on-failure may | ||
| // have stranded. SQLite ignores an unmatched -wal file in rollback | ||
| // journal mode, so only the pre-open cleanup removes it. | ||
| try Data("stale journal".utf8).write(to: journalURL) | ||
| try Data("stale wal".utf8).write(to: walURL) | ||
|
|
||
| let outcome = WordPressApiCache.onDiskCache(at: databaseURL) | ||
|
|
||
| guard case .opened = outcome else { | ||
| Issue.record("Expected .opened, got \(outcome)") | ||
| return | ||
| } | ||
| #expect(!FileManager.default.fileExists(at: journalURL)) | ||
| #expect(!FileManager.default.fileExists(at: walURL)) | ||
| } | ||
|
|
||
| @Test func failsWhenOrphanedSiblingCannotBeRemoved() throws { | ||
| // A stale sibling next to a missing database that cannot be removed | ||
| // must not be paired with a freshly created database. Make the | ||
| // directory read-only so removing the journal fails. | ||
| try Data("stale journal".utf8).write(to: journalURL) | ||
| let fileManager = FileManager.default | ||
| try fileManager.setAttributes([.posixPermissions: 0o555], ofItemAtPath: directory.path) | ||
| defer { | ||
| try? fileManager.setAttributes([.posixPermissions: 0o755], ofItemAtPath: directory.path) | ||
| } | ||
|
|
||
| let outcome = WordPressApiCache.onDiskCache(at: databaseURL) | ||
|
|
||
| guard case .failed(let failure) = outcome else { | ||
| Issue.record("Expected .failed, got \(outcome)") | ||
| return | ||
| } | ||
| #expect(failure.kind == .couldNotRemoveOrphanedSiblings) | ||
| #expect(!fileManager.fileExists(at: databaseURL)) | ||
| } | ||
|
|
||
| @Test func failsWhenDatabaseCannotBeCreated() { | ||
| let unwritableURL = | ||
| directory | ||
| .appending(path: "missing-subdirectory") | ||
| .appending(path: "app.sqlite") | ||
|
|
||
| let outcome = WordPressApiCache.onDiskCache(at: unwritableURL) | ||
|
|
||
| guard case .failed(let failure) = outcome else { | ||
| Issue.record("Expected .failed, got \(outcome)") | ||
| return | ||
| } | ||
| #expect(failure.kind == .couldNotOpenDatabase) | ||
| } | ||
|
|
||
| @Test func clientsShareSingleCacheInstance() async { | ||
| let clientA = WordPressClient(api: MockWordPressClientAPI(), siteURL: URL(string: "https://example.com")!) | ||
| let clientB = WordPressClient(api: MockWordPressClientAPI(), siteURL: URL(string: "https://example.org")!) | ||
|
|
||
| let cacheA = await clientA.cache | ||
| let cacheB = await clientB.cache | ||
|
|
||
| #expect(cacheA === cacheB) | ||
| #expect(cacheA === WordPressApiCache.shared) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Might need to inject this one so we're not creating
~/Library/app.sqlitewhen running the test suite locally? The default could beURL.libraryDirectorythough?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.
Addressed in 40b973d