Test that we reuse credentials when reconnecting

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo 2021-09-14 18:30:17 -06:00
parent 44a457e8b6
commit 77a4a36eb3
2 changed files with 23 additions and 11 deletions

View file

@ -581,6 +581,7 @@ mod tests {
status.recv().await, status.recv().await,
Some(Status::Connected { .. }) Some(Status::Connected { .. })
)); ));
assert_eq!(server.auth_count(), 1);
server.forbid_connections(); server.forbid_connections();
server.disconnect().await; server.disconnect().await;
@ -589,6 +590,7 @@ mod tests {
server.allow_connections(); server.allow_connections();
cx.foreground().advance_clock(Duration::from_secs(10)); cx.foreground().advance_clock(Duration::from_secs(10));
while !matches!(status.recv().await, Some(Status::Connected { .. })) {} while !matches!(status.recv().await, Some(Status::Connected { .. })) {}
assert_eq!(server.auth_count(), 1); // Client reused the cached credentials when reconnecting
} }
#[test] #[test]

View file

@ -21,7 +21,7 @@ use std::{
marker::PhantomData, marker::PhantomData,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::{ sync::{
atomic::{AtomicBool, Ordering::SeqCst}, atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst},
Arc, Arc,
}, },
}; };
@ -209,6 +209,7 @@ pub struct FakeServer {
incoming: Mutex<Option<mpsc::Receiver<Box<dyn proto::AnyTypedEnvelope>>>>, incoming: Mutex<Option<mpsc::Receiver<Box<dyn proto::AnyTypedEnvelope>>>>,
connection_id: Mutex<Option<ConnectionId>>, connection_id: Mutex<Option<ConnectionId>>,
forbid_connections: AtomicBool, forbid_connections: AtomicBool,
auth_count: AtomicUsize,
} }
impl FakeServer { impl FakeServer {
@ -217,26 +218,31 @@ impl FakeServer {
client: &mut Arc<Client>, client: &mut Arc<Client>,
cx: &TestAppContext, cx: &TestAppContext,
) -> Arc<Self> { ) -> Arc<Self> {
let result = Arc::new(Self { let server = Arc::new(Self {
peer: Peer::new(), peer: Peer::new(),
incoming: Default::default(), incoming: Default::default(),
connection_id: Default::default(), connection_id: Default::default(),
forbid_connections: Default::default(), forbid_connections: Default::default(),
auth_count: Default::default(),
}); });
Arc::get_mut(client) Arc::get_mut(client)
.unwrap() .unwrap()
.override_authenticate(move |cx| { .override_authenticate({
cx.spawn(|_| async move { let server = server.clone();
let access_token = "the-token".to_string(); move |cx| {
Ok(Credentials { server.auth_count.fetch_add(1, SeqCst);
user_id: client_user_id, cx.spawn(move |_| async move {
access_token, let access_token = "the-token".to_string();
Ok(Credentials {
user_id: client_user_id,
access_token,
})
}) })
}) }
}) })
.override_establish_connection({ .override_establish_connection({
let server = result.clone(); let server = server.clone();
move |credentials, cx| { move |credentials, cx| {
assert_eq!(credentials.user_id, client_user_id); assert_eq!(credentials.user_id, client_user_id);
assert_eq!(credentials.access_token, "the-token"); assert_eq!(credentials.access_token, "the-token");
@ -251,7 +257,7 @@ impl FakeServer {
.authenticate_and_connect(&cx.to_async()) .authenticate_and_connect(&cx.to_async())
.await .await
.unwrap(); .unwrap();
result server
} }
pub async fn disconnect(&self) { pub async fn disconnect(&self) {
@ -273,6 +279,10 @@ impl FakeServer {
} }
} }
pub fn auth_count(&self) -> usize {
self.auth_count.load(SeqCst)
}
pub fn forbid_connections(&self) { pub fn forbid_connections(&self) {
self.forbid_connections.store(true, SeqCst); self.forbid_connections.store(true, SeqCst);
} }