From c5351a12761416405426b380ad7246a68447ed03 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 30 Jun 2022 14:51:22 -0700 Subject: [PATCH] Ensure that usernames, user ids, and client ids match in random collaboration test This makes the logs easier to interpret --- crates/client/src/client.rs | 17 ++++++++--------- crates/collab/src/db.rs | 2 +- crates/collab/src/integration_tests.rs | 19 ++++++++++++------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index 084d43af1b86eda598f1fadfca726e29b82bac2f..538b0fa4b0101be73c64d366faf09238e5d8da16 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -28,10 +28,7 @@ use std::{ convert::TryFrom, fmt::Write as _, future::Future, - sync::{ - atomic::{AtomicUsize, Ordering}, - Arc, Weak, - }, + sync::{Arc, Weak}, time::{Duration, Instant}, }; use thiserror::Error; @@ -232,12 +229,8 @@ impl Drop for Subscription { impl Client { pub fn new(http: Arc) -> Arc { - lazy_static! { - static ref NEXT_CLIENT_ID: AtomicUsize = AtomicUsize::default(); - } - Arc::new(Self { - id: NEXT_CLIENT_ID.fetch_add(1, Ordering::SeqCst), + id: 0, peer: Peer::new(), http, state: Default::default(), @@ -257,6 +250,12 @@ impl Client { self.http.clone() } + #[cfg(any(test, feature = "test-support"))] + pub fn set_id(&mut self, id: usize) -> &Self { + self.id = id; + self + } + #[cfg(any(test, feature = "test-support"))] pub fn tear_down(&self) { let mut state = self.state.write(); diff --git a/crates/collab/src/db.rs b/crates/collab/src/db.rs index ce494db59bb6222b90533d0129f86c21e651e697..6c40996ae4fc04ac5f57463cf6c33546be462e60 100644 --- a/crates/collab/src/db.rs +++ b/crates/collab/src/db.rs @@ -2117,7 +2117,7 @@ pub mod tests { Self { background, users: Default::default(), - next_user_id: Mutex::new(1), + next_user_id: Mutex::new(0), projects: Default::default(), worktree_extensions: Default::default(), next_project_id: Mutex::new(1), diff --git a/crates/collab/src/integration_tests.rs b/crates/collab/src/integration_tests.rs index 3da1fc86925e8e36fcaeed0c3b17a2d2b1f0cd6d..d901bd060ceb1be74beb2e0d18b24c13b59acae7 100644 --- a/crates/collab/src/integration_tests.rs +++ b/crates/collab/src/integration_tests.rs @@ -4426,8 +4426,16 @@ async fn test_random_collaboration( let mut server = TestServer::start(cx.foreground(), cx.background()).await; let db = server.app_state.db.clone(); let host_user_id = db.create_user("host", None, false).await.unwrap(); - for username in ["guest-1", "guest-2", "guest-3", "guest-4"] { + let mut available_guests = vec![ + "guest-1".to_string(), + "guest-2".to_string(), + "guest-3".to_string(), + "guest-4".to_string(), + ]; + + for username in &available_guests { let guest_user_id = db.create_user(username, None, false).await.unwrap(); + assert_eq!(*username, format!("guest-{}", guest_user_id)); server .app_state .db @@ -4621,12 +4629,7 @@ async fn test_random_collaboration( } else { max_operations }; - let mut available_guests = vec![ - "guest-1".to_string(), - "guest-2".to_string(), - "guest-3".to_string(), - "guest-4".to_string(), - ]; + let mut operations = 0; while operations < max_operations { if operations == disconnect_host_at { @@ -4729,6 +4732,7 @@ async fn test_random_collaboration( server.disconnect_client(removed_guest_id); deterministic.advance_clock(RECEIVE_TIMEOUT); deterministic.start_waiting(); + log::info!("Waiting for guest {} to exit...", removed_guest_id); let (guest, guest_project, mut guest_cx, guest_err) = guest.await; deterministic.finish_waiting(); server.allow_connections(); @@ -4945,6 +4949,7 @@ impl TestServer { Arc::get_mut(&mut client) .unwrap() + .set_id(user_id.0 as usize) .override_authenticate(move |cx| { cx.spawn(|_| async move { let access_token = "the-token".to_string();