Skip to content

Commit

Permalink
refactor(key_backups): Rename fast_exists_on_server to exists_on_server
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam committed Dec 9, 2024
1 parent 1ca31c4 commit c37b485
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions crates/matrix-sdk/src/encryption/backups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ impl Backups {
/// backup exists - instead use [`Self::fetch_exists_on_server`]. This
/// method is useful when performance is more important than guaranteed
/// accuracy, such as when classifying UTDs.
pub async fn fast_exists_on_server(&self) -> Result<bool, Error> {
pub async fn exists_on_server(&self) -> Result<bool, Error> {
// If we have an answer cached, return it immediately
if let Some(cached_value) = self.client.inner.e2ee.backup_state.backup_exists_on_server() {
return Ok(cached_value);
Expand Down Expand Up @@ -660,6 +660,9 @@ impl Backups {
}
};

// If the request succeeded, the backup is gone. If it failed, we are not really
// sure what the backup state is. Either way, clear the cache so we check next
// time we need to know.
self.client.inner.e2ee.backup_state.clear_backup_exists_on_server();

ret
Expand Down Expand Up @@ -1222,7 +1225,7 @@ mod test {
}

#[async_test]
async fn test_when_a_backup_exists_then_fast_exists_on_server_returns_true() {
async fn test_when_a_backup_exists_then_exists_on_server_returns_true() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

Expand All @@ -1231,15 +1234,15 @@ mod test {
let exists = client
.encryption()
.backups()
.fast_exists_on_server()
.exists_on_server()
.await
.expect("We should be able to check if backups exist on the server");

assert!(exists, "We should deduce that a backup exists on the server");
}

#[async_test]
async fn test_when_no_backup_exists_then_fast_exists_on_server_returns_false() {
async fn test_when_no_backup_exists_then_exists_on_server_returns_false() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

Expand All @@ -1248,23 +1251,23 @@ mod test {
let exists = client
.encryption()
.backups()
.fast_exists_on_server()
.exists_on_server()
.await
.expect("We should be able to check if backups exist on the server");

assert!(!exists, "We should deduce that no backup exists on the server");
}

#[async_test]
async fn test_when_server_returns_an_error_then_fast_exists_on_server_returns_an_error() {
async fn test_when_server_returns_an_error_then_exists_on_server_returns_an_error() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

{
let _scope =
server.mock_room_keys_version().error429().expect(1).mount_as_scoped().await;

client.encryption().backups().fast_exists_on_server().await.expect_err(
client.encryption().backups().exists_on_server().await.expect_err(
"If the /version endpoint returns a non 404 error we should throw an error",
);
}
Expand All @@ -1273,14 +1276,14 @@ mod test {
let _scope =
server.mock_room_keys_version().error404().expect(1).mount_as_scoped().await;

client.encryption().backups().fast_exists_on_server().await.expect_err(
client.encryption().backups().exists_on_server().await.expect_err(
"If the /version endpoint returns a non-Matrix 404 error we should throw an error",
);
}
}

#[async_test]
async fn test_repeated_calls_to_fast_exists_on_server_do_not_make_additional_requests() {
async fn test_repeated_calls_to_exists_on_server_do_not_make_additional_requests() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

Expand All @@ -1289,13 +1292,13 @@ mod test {

let backups = client.encryption().backups();

// Call fast_exists_on_server several times
backups.fast_exists_on_server().await.unwrap();
backups.fast_exists_on_server().await.unwrap();
backups.fast_exists_on_server().await.unwrap();
// Call exists_on_server several times
backups.exists_on_server().await.unwrap();
backups.exists_on_server().await.unwrap();
backups.exists_on_server().await.unwrap();

let exists = backups
.fast_exists_on_server()
.exists_on_server()
.await
.expect("We should be able to check if backups exist on the server");

Expand All @@ -1305,16 +1308,16 @@ mod test {
}

#[async_test]
async fn test_adding_a_backup_invalidates_fast_exists_on_server_cache() {
async fn test_adding_a_backup_invalidates_exists_on_server_cache() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;
let backups = client.encryption().backups();

{
let _scope = server.mock_room_keys_version().none().expect(1).mount_as_scoped().await;

// Call fast_exists_on_server to fill the cache
let exists = backups.fast_exists_on_server().await.unwrap();
// Call exists_on_server to fill the cache
let exists = backups.exists_on_server().await.unwrap();
assert!(!exists, "No backup exists at this point");
}

Expand All @@ -1324,24 +1327,24 @@ mod test {

server.mock_room_keys_version().exists().expect(1).mount().await;
let exists = backups
.fast_exists_on_server()
.exists_on_server()
.await
.expect("We should be able to check if backups exist on the server");

assert!(exists, "But now a backup does exist");
}

#[async_test]
async fn test_removing_a_backup_invalidates_fast_exists_on_server_cache() {
async fn test_removing_a_backup_invalidates_exists_on_server_cache() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;
let backups = client.encryption().backups();

{
let _scope = server.mock_room_keys_version().exists().expect(1).mount_as_scoped().await;

// Call fast_exists_on_server to fill the cache
let exists = backups.fast_exists_on_server().await.unwrap();
// Call exists_on_server to fill the cache
let exists = backups.exists_on_server().await.unwrap();
assert!(exists, "A backup exists at this point");
}

Expand All @@ -1351,7 +1354,7 @@ mod test {

server.mock_room_keys_version().none().expect(1).mount().await;
let exists = backups
.fast_exists_on_server()
.exists_on_server()
.await
.expect("We should be able to check if backups exist on the server");

Expand Down

0 comments on commit c37b485

Please sign in to comment.