Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(desktop-gpui): serialize tests that reset or count the shared API client

Richie McIlroy2c925450cdf9
diff --git a/apps/desktop-gpui/src/auth.rs b/apps/desktop-gpui/src/auth.rs
index abd118f28..59a201895 100644
--- a/apps/desktop-gpui/src/auth.rs
+++ b/apps/desktop-gpui/src/auth.rs
@@ -338,7 +338,9 @@ fn api_client_builder() -> reqwest::ClientBuilder {
.timeout(Duration::from_secs(45))
}
-/// A new client, for a caller that keeps one for a single upload.
+/// Multipart uploads hold their own client and drop it when the upload ends,
+/// so a later upload never starts on a pooled connection from an earlier
+/// network (#2522).
pub fn api_client() -> Result<reqwest::Client, AuthApiError> {
api_client_builder()
.build()
@@ -348,6 +350,12 @@ pub fn api_client() -> Result<reqwest::Client, AuthApiError> {
static SHARED_API_CLIENT: Mutex<Option<(std::time::SystemTime, reqwest::Client)>> =
Mutex::new(None);
+/// Held by every test that resets the shared client or counts its
+/// connections, so a reset on one thread cannot land in another's count.
+#[cfg(test)]
+pub(crate) static SHARED_API_CLIENT_TEST_LOCK: tokio::sync::Mutex<()> =
+ tokio::sync::Mutex::const_new(());
+
/// Drops the shared client, as quitting the app does, for tests that model a
/// relaunch.
#[cfg(test)]
diff --git a/apps/desktop-gpui/src/upload/multipart_tests.rs b/apps/desktop-gpui/src/upload/multipart_tests.rs
index 2db7499aa..954320f77 100644
--- a/apps/desktop-gpui/src/upload/multipart_tests.rs
+++ b/apps/desktop-gpui/src/upload/multipart_tests.rs
@@ -816,11 +816,11 @@ async fn authed_presign(part: u32) -> Duration {
async fn sequential_api_requests_reuse_one_connection_until_it_idles_out() {
let server = mock_network::start(fast_local_profile());
let _signed_in = SignedIn::new(server.addr);
+ let _shared_client = auth::SHARED_API_CLIENT_TEST_LOCK.lock().await;
for part in 1..=20 {
authed_presign(part).await;
}
- // One more if the shared client is replaced mid-loop, by its age or by a
- // relaunch test on another thread.
+ // One more if the shared client reaches its maximum age mid-loop.
let reused = server.connections.load(Ordering::Acquire);
assert!(reused <= 2, "{reused} connections for 20 requests");
diff --git a/apps/desktop-gpui/src/upload/queue/verify_resume_tests.rs b/apps/desktop-gpui/src/upload/queue/verify_resume_tests.rs
index 555b58f86..6b0739523 100644
--- a/apps/desktop-gpui/src/upload/queue/verify_resume_tests.rs
+++ b/apps/desktop-gpui/src/upload/queue/verify_resume_tests.rs
@@ -126,6 +126,7 @@ fn assert_verified(
#[test]
fn saved_instant_upload_resumes_after_quit_mid_transfer() {
let case = "queue_resume_after_quit_mid_transfer";
+ let _shared_client = crate::auth::SHARED_API_CLIENT_TEST_LOCK.blocking_lock();
let scratch = Scratch::new(case);
virtual_time(async {
let mock = MockCap::start(vec![rule(
@@ -179,6 +180,7 @@ fn saved_instant_upload_resumes_after_quit_mid_transfer() {
#[test]
fn saved_instant_upload_quit_during_verification_confirms_without_reupload() {
let case = "queue_resume_after_quit_during_verification";
+ let _shared_client = crate::auth::SHARED_API_CLIENT_TEST_LOCK.blocking_lock();
let scratch = Scratch::new(case);
virtual_time(async {
let mock = MockCap::start(vec![rule(Route::Api("recording-complete"), Fault::Hang)]).await;