Avoid possible memory leak of FakeServer in tests

This commit is contained in:
Max Brunsfeld 2022-05-10 21:45:12 -07:00
parent d0052ccfb5
commit 297fa1af55

View file

@ -41,12 +41,14 @@ impl FakeServer {
Arc::get_mut(client) Arc::get_mut(client)
.unwrap() .unwrap()
.override_authenticate({ .override_authenticate({
let state = server.state.clone(); let state = Arc::downgrade(&server.state);
move |cx| { move |cx| {
let mut state = state.lock(); let state = state.clone();
state.auth_count += 1;
let access_token = state.access_token.to_string();
cx.spawn(move |_| async move { cx.spawn(move |_| async move {
let state = state.upgrade().ok_or_else(|| anyhow!("server dropped"))?;
let mut state = state.lock();
state.auth_count += 1;
let access_token = state.access_token.to_string();
Ok(Credentials { Ok(Credentials {
user_id: client_user_id, user_id: client_user_id,
access_token, access_token,
@ -55,21 +57,23 @@ impl FakeServer {
} }
}) })
.override_establish_connection({ .override_establish_connection({
let peer = server.peer.clone(); let peer = Arc::downgrade(&server.peer).clone();
let state = server.state.clone(); let state = Arc::downgrade(&server.state);
move |credentials, cx| { move |credentials, cx| {
let peer = peer.clone(); let peer = peer.clone();
let state = state.clone(); let state = state.clone();
let credentials = credentials.clone(); let credentials = credentials.clone();
cx.spawn(move |cx| async move { cx.spawn(move |cx| async move {
assert_eq!(credentials.user_id, client_user_id); let state = state.upgrade().ok_or_else(|| anyhow!("server dropped"))?;
let peer = peer.upgrade().ok_or_else(|| anyhow!("server dropped"))?;
if state.lock().forbid_connections { if state.lock().forbid_connections {
Err(EstablishConnectionError::Other(anyhow!( Err(EstablishConnectionError::Other(anyhow!(
"server is forbidding connections" "server is forbidding connections"
)))? )))?
} }
assert_eq!(credentials.user_id, client_user_id);
if credentials.access_token != state.lock().access_token.to_string() { if credentials.access_token != state.lock().access_token.to_string() {
Err(EstablishConnectionError::Unauthorized)? Err(EstablishConnectionError::Unauthorized)?
} }