-
-
Notifications
You must be signed in to change notification settings - Fork 298
fix(coordinator): reload table data on refresh while a query is in flight #1643
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
Merged
Changes from all commits
Commits
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
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
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
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
166 changes: 166 additions & 0 deletions
166
TableProTests/Views/Main/MainContentCoordinatorRefreshTests.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,166 @@ | ||
| // | ||
| // MainContentCoordinatorRefreshTests.swift | ||
| // TableProTests | ||
| // | ||
| // Tests for handleRefresh, the entry point behind the Refresh toolbar | ||
| // button and Cmd+R. Regression coverage for #1637: a refresh issued while | ||
| // a query was in flight was silently dropped because the in-flight | ||
| // cancellation cleared isExecuting asynchronously. | ||
| // | ||
|
|
||
| import Foundation | ||
| import Testing | ||
|
|
||
| @testable import TablePro | ||
|
|
||
| @Suite("MainContentCoordinator handleRefresh") | ||
| @MainActor | ||
| struct MainContentCoordinatorRefreshTests { | ||
| private func makeCoordinator() -> (MainContentCoordinator, QueryTabManager) { | ||
| let tabManager = QueryTabManager() | ||
| let coordinator = MainContentCoordinator( | ||
| connection: TestFixtures.makeConnection(), | ||
| tabManager: tabManager, | ||
| changeManager: DataChangeManager(), | ||
| toolbarState: ConnectionToolbarState() | ||
| ) | ||
| return (coordinator, tabManager) | ||
| } | ||
|
|
||
| private func addTableTab( | ||
| to tabManager: QueryTabManager, | ||
| tableName: String = "users", | ||
| query: String = "SELECT * FROM users" | ||
| ) -> UUID { | ||
| var tab = QueryTab( | ||
| title: tableName, | ||
| query: query, | ||
| tabType: .table, | ||
| tableName: tableName | ||
| ) | ||
| tab.tableContext.isEditable = true | ||
| tabManager.tabs.append(tab) | ||
| tabManager.selectedTabId = tab.id | ||
| return tab.id | ||
| } | ||
|
|
||
| private func addQueryTab( | ||
| to tabManager: QueryTabManager, | ||
| title: String = "Query 1", | ||
| query: String = "SELECT 1" | ||
| ) -> UUID { | ||
| let tab = QueryTab(title: title, query: query, tabType: .query) | ||
| tabManager.tabs.append(tab) | ||
| tabManager.selectedTabId = tab.id | ||
| return tab.id | ||
| } | ||
|
|
||
| private func simulateInFlightQuery( | ||
| _ coordinator: MainContentCoordinator, | ||
| _ tabManager: QueryTabManager, | ||
| at index: Int | ||
| ) -> Task<Void, Never> { | ||
| let inFlight = Task<Void, Never> { _ = try? await Task.sleep(for: .seconds(60)) } | ||
| coordinator.currentQueryTask = inFlight | ||
| tabManager.tabs[index].execution.isExecuting = true | ||
| tabManager.tabs[index].execution.lastExecutedAt = Date() | ||
| return inFlight | ||
| } | ||
|
|
||
| @Test("Refresh while a query is in flight cancels it and starts a new execution") | ||
| func refreshWithInFlightQueryStartsNewExecution() { | ||
| let (coordinator, tabManager) = makeCoordinator() | ||
| let tabId = addTableTab(to: tabManager) | ||
| guard let idx = tabManager.tabs.firstIndex(where: { $0.id == tabId }) else { | ||
| Issue.record("expected tab to exist") | ||
| return | ||
| } | ||
| let staleTask = simulateInFlightQuery(coordinator, tabManager, at: idx) | ||
| let initialGeneration = coordinator.queryGeneration | ||
|
|
||
| coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) | ||
| defer { coordinator.currentQueryTask?.cancel() } | ||
|
|
||
| #expect(staleTask.isCancelled == true) | ||
| #expect(coordinator.queryGeneration == initialGeneration + 2) | ||
| #expect(coordinator.currentQueryTask != nil) | ||
| #expect(tabManager.tabs[idx].execution.isExecuting == true) | ||
| } | ||
|
|
||
| @Test("Refresh on an idle table tab starts an execution") | ||
| func refreshOnIdleTabStartsExecution() { | ||
| let (coordinator, tabManager) = makeCoordinator() | ||
| let tabId = addTableTab(to: tabManager) | ||
| guard let idx = tabManager.tabs.firstIndex(where: { $0.id == tabId }) else { | ||
| Issue.record("expected tab to exist") | ||
| return | ||
| } | ||
| tabManager.tabs[idx].execution.lastExecutedAt = Date() | ||
| let initialGeneration = coordinator.queryGeneration | ||
|
|
||
| coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) | ||
| defer { coordinator.currentQueryTask?.cancel() } | ||
|
|
||
| #expect(coordinator.queryGeneration == initialGeneration + 2) | ||
| #expect(coordinator.currentQueryTask != nil) | ||
| #expect(tabManager.tabs[idx].execution.isExecuting == true) | ||
| } | ||
|
|
||
| @Test("Refresh rebuilds the table query from current state before executing") | ||
| func refreshRebuildsQuery() { | ||
| let (coordinator, tabManager) = makeCoordinator() | ||
| let tabId = addTableTab(to: tabManager, query: "SELECT outdated FROM users") | ||
| guard let idx = tabManager.tabs.firstIndex(where: { $0.id == tabId }) else { | ||
| Issue.record("expected tab to exist") | ||
| return | ||
| } | ||
| tabManager.tabs[idx].execution.lastExecutedAt = Date() | ||
|
|
||
| coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) | ||
| defer { coordinator.currentQueryTask?.cancel() } | ||
|
|
||
| #expect(tabManager.tabs[idx].content.query != "SELECT outdated FROM users") | ||
| #expect(tabManager.tabs[idx].content.query.contains("users")) | ||
| } | ||
|
|
||
| @Test("Refresh on a query tab does not execute or cancel anything") | ||
| func refreshOnQueryTabIsNoOp() { | ||
| let (coordinator, tabManager) = makeCoordinator() | ||
| let tabId = addQueryTab(to: tabManager) | ||
| guard let idx = tabManager.tabs.firstIndex(where: { $0.id == tabId }) else { | ||
| Issue.record("expected tab to exist") | ||
| return | ||
| } | ||
| let inFlight = Task<Void, Never> { _ = try? await Task.sleep(for: .seconds(60)) } | ||
| defer { inFlight.cancel() } | ||
| coordinator.currentQueryTask = inFlight | ||
| tabManager.tabs[idx].execution.isExecuting = true | ||
| let initialGeneration = coordinator.queryGeneration | ||
|
|
||
| coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) | ||
|
|
||
| #expect(inFlight.isCancelled == false) | ||
| #expect(coordinator.queryGeneration == initialGeneration) | ||
| #expect(tabManager.tabs[idx].execution.isExecuting == true) | ||
| } | ||
|
|
||
| @Test("Refresh in structure view leaves execution state untouched") | ||
| func refreshInStructureViewIsNoOp() { | ||
| let (coordinator, tabManager) = makeCoordinator() | ||
| let tabId = addTableTab(to: tabManager) | ||
| guard let idx = tabManager.tabs.firstIndex(where: { $0.id == tabId }) else { | ||
| Issue.record("expected tab to exist") | ||
| return | ||
| } | ||
| tabManager.tabs[idx].display.resultsViewMode = .structure | ||
| let staleTask = simulateInFlightQuery(coordinator, tabManager, at: idx) | ||
| defer { staleTask.cancel() } | ||
| let initialGeneration = coordinator.queryGeneration | ||
|
|
||
| coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) | ||
|
|
||
| #expect(staleTask.isCancelled == false) | ||
| #expect(coordinator.queryGeneration == initialGeneration) | ||
| #expect(tabManager.tabs[idx].execution.isExecuting == true) | ||
| } | ||
| } |
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.
When Refresh is pressed while a table query is in flight, this starts the replacement query immediately after cancelling the old task. The old task’s cleanup in
executeQueryInternalstill runs later and unconditionally clearscurrentQueryTask/isExecutingbefore checkingqueryGeneration, so the cancelled task can mark the newly-started refresh query as idle and drop its task reference while its SELECT is still running. In that state the toolbar shows idle and later Cancel/Refresh actions can no longer target the real query; guard the old task cleanup by generation or wait for cancellation before callingrunQuery().Useful? React with 👍 / 👎.