Skip to content

Commit

Permalink
Fix non-1.70.0 usage, fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Dec 25, 2023
1 parent 63b4e5b commit 9f90a75
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 45 deletions.
14 changes: 6 additions & 8 deletions taskchampion/taskchampion/src/server/cloud/gcp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(unused_variables, dead_code)]
use super::service::{Service, ObjectInfo};
use super::service::{ObjectInfo, Service};
use crate::errors::Result;
use google_cloud_storage::client::{Client, ClientConfig};
use google_cloud_storage::http::error::ErrorResponse;
Expand Down Expand Up @@ -28,9 +28,7 @@ fn is_http_error<T>(query: u16, res: &std::result::Result<T, http::Error>) -> bo
impl GcpService {
pub(in crate::server) fn new(bucket: String) -> Result<Self> {
let rt = Runtime::new()?;
let config = rt.block_on(
ClientConfig::default().with_auth(),
)?;
let config = rt.block_on(ClientConfig::default().with_auth())?;
Ok(Self {
client: Client::new(config),
rt,
Expand Down Expand Up @@ -328,7 +326,7 @@ mod tests {
};

assert!(svc
.compare_and_swap(&pfx("testy"), None, b"bar".into())
.compare_and_swap(&pfx("testy"), None, b"bar".to_vec())
.unwrap());
let got = svc.get(&pfx("testy")).unwrap();
assert_eq!(got, Some(b"bar".to_vec()));
Expand All @@ -347,7 +345,7 @@ mod tests {
svc.put(&pfx("testy"), b"foo1").unwrap();
svc.put(&pfx("testy"), b"foo2").unwrap();
assert!(svc
.compare_and_swap(&pfx("testy"), Some(b"foo2".into()), b"bar".into())
.compare_and_swap(&pfx("testy"), Some(b"foo2".to_vec()), b"bar".to_vec())
.unwrap());
let got = svc.get(&pfx("testy")).unwrap();
assert_eq!(got, Some(b"bar".to_vec()));
Expand All @@ -365,7 +363,7 @@ mod tests {
// Create the existing file, with two generations.
svc.put(&pfx("testy"), b"foo1").unwrap();
assert!(!svc
.compare_and_swap(&pfx("testy"), None, b"bar".into())
.compare_and_swap(&pfx("testy"), None, b"bar".to_vec())
.unwrap());
let got = svc.get(&pfx("testy")).unwrap();
assert_eq!(got, Some(b"foo1".to_vec()));
Expand All @@ -384,7 +382,7 @@ mod tests {
svc.put(&pfx("testy"), b"foo1").unwrap();
svc.put(&pfx("testy"), b"foo2").unwrap();
assert!(!svc
.compare_and_swap(&pfx("testy"), Some(b"foo1".into()), b"bar".into())
.compare_and_swap(&pfx("testy"), Some(b"foo1".to_vec()), b"bar".to_vec())
.unwrap());
let got = svc.get(&pfx("testy")).unwrap();
assert_eq!(got, Some(b"foo2".to_vec()));
Expand Down
98 changes: 61 additions & 37 deletions taskchampion/taskchampion/src/server/cloud/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use super::service::{Service, ObjectInfo};
#[cfg(not(test))]
use std::time::{SystemTime, UNIX_EPOCH};
use super::service::{ObjectInfo, Service};
use crate::errors::{Error, Result};
use crate::server::crypto::{Cryptor, Sealed, Unsealed};
use crate::server::{
Expand All @@ -9,6 +7,8 @@ use crate::server::{
};
use ring::rand;
use std::collections::{HashMap, HashSet};
#[cfg(not(test))]
use std::time::{SystemTime, UNIX_EPOCH};
use uuid::Uuid;

/// Implement the Server trait for a cloud service implemented by [`Service`].
Expand Down Expand Up @@ -140,7 +140,7 @@ impl<SVC: Service> CloudServer<SVC> {
rand::SystemRandom::new()
.fill(&mut randint)
.map_err(|_| Error::Server("Random number generator failure".into()))?;
Ok(randint[0])
Ok(randint[0])
}

/// Get the version from "latest", or None if the object does not exist. This always fetches a fresh
Expand Down Expand Up @@ -249,21 +249,27 @@ impl<SVC: Service> CloudServer<SVC> {
// Collect all versions older than MAX_VERSION_AGE_SECS
#[cfg(not(test))]
let age_threshold = {
let now = SystemTime::now().duration_since(UNIX_EPOCH).map(|t| t.as_secs()).unwrap_or(0);
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|t| t.as_secs())
.unwrap_or(0);
now.saturating_sub(MAX_VERSION_AGE_SECS)
};

// In testing, cutoff age is 1000.
#[cfg(test)]
let age_threshold = 1000;

let old_versions: HashSet<Uuid> = versions.iter().filter_map(|(c, _, creation)| {
if *creation < age_threshold {
Some(*c)
} else {
None
}
}).collect();
let old_versions: HashSet<Uuid> = versions
.iter()
.filter_map(|(c, _, creation)| {
if *creation < age_threshold {
Some(*c)
} else {
None
}
})
.collect();

// Now, any pair not present in that chain can be deleted. However, another replica
// may be in the state where it has uploaded a version but not changed "latest" yet,
Expand All @@ -275,16 +281,20 @@ impl<SVC: Service> CloudServer<SVC> {
}

// Collect a set of all snapshots.
let snapshots = self.service.list(b"s-").filter_map(|res| match res {
Ok(ObjectInfo { name , .. }) => {
if let Some(v) = Self::parse_snapshot_name(&name) {
Some(Ok(v))
} else {
None
let snapshots = self
.service
.list(b"s-")
.filter_map(|res| match res {
Ok(ObjectInfo { name, .. }) => {
if let Some(v) = Self::parse_snapshot_name(&name) {
Some(Ok(v))
} else {
None
}
}
}
Err(e) => Some(Err(e)),
}).collect::<Result<HashSet<_>>>()?;
Err(e) => Some(Err(e)),
})
.collect::<Result<HashSet<_>>>()?;

// Find the latest snapshot by iterating back from "latest". Note that this iteration is
// guaranteed not to be cyclical, as that was checked above.
Expand All @@ -304,7 +314,7 @@ impl<SVC: Service> CloudServer<SVC> {
}

// If there's a latest snapshot, delete all other snapshots.
let Some(latest_snapshot) = latest_snapshot else {
let Some(latest_snapshot) = latest_snapshot else {
// If there's no snapshot, no further cleanup is possible.
return Ok(());
};
Expand Down Expand Up @@ -482,7 +492,8 @@ mod tests {

impl Service for MockService {
fn put(&mut self, name: &[u8], value: &[u8]) -> Result<()> {
self.0.insert(name.to_vec(), (INSERTION_TIME, value.to_vec()));
self.0
.insert(name.to_vec(), (INSERTION_TIME, value.to_vec()));
Ok(())
}

Expand All @@ -508,16 +519,21 @@ mod tests {
Ok(false)
}

fn list<'a>(&'a mut self, prefix: &[u8]) -> Box<dyn Iterator<Item = Result<ObjectInfo>> + 'a> {
fn list<'a>(
&'a mut self,
prefix: &[u8],
) -> Box<dyn Iterator<Item = Result<ObjectInfo>> + 'a> {
let prefix = prefix.to_vec();
Box::new(
self.0
.iter()
.filter(move |(k, _)| k.starts_with(&prefix))
.map(|(k, (t, _))| Ok(ObjectInfo {
name: k.to_vec(),
creation: *t,
})),
.map(|(k, (t, _))| {
Ok(ObjectInfo {
name: k.to_vec(),
creation: *t,
})
}),
)
}
}
Expand All @@ -536,7 +552,13 @@ mod tests {

// Add some testing utilities to CloudServer.
impl CloudServer<MockService> {
fn mock_add_version(&mut self, parent: VersionId, child: VersionId, creation: u64, data: &[u8]) {
fn mock_add_version(
&mut self,
parent: VersionId,
child: VersionId,
creation: u64,
data: &[u8],
) {
let name = Self::version_name(&parent, &child);
let sealed = self
.cryptor
Expand All @@ -562,7 +584,9 @@ mod tests {

fn mock_set_latest(&mut self, latest: VersionId) {
let latest = version_to_bytes(latest);
self.service.0.insert(LATEST.to_vec(), (INSERTION_TIME, latest));
self.service
.0
.insert(LATEST.to_vec(), (INSERTION_TIME, latest));
}

/// Create a copy of this server without any data; used for creating a MockService
Expand Down Expand Up @@ -781,7 +805,7 @@ mod tests {
fn add_version_empty() {
let mut server = make_server();
let parent = Uuid::new_v4();
let (res, _) = server.add_version(parent, b"history".into()).unwrap();
let (res, _) = server.add_version(parent, b"history".to_vec()).unwrap();
assert!(matches!(res, AddVersionResult::Ok(_)));
}

Expand All @@ -791,7 +815,7 @@ mod tests {
let (v1, v2) = (Uuid::new_v4(), Uuid::new_v4());
server.mock_add_version(v1, v2, 1000, b"first");
server.mock_set_latest(v2);
let (res, _) = server.add_version(v2, b"history".into()).unwrap();
let (res, _) = server.add_version(v2, b"history".to_vec()).unwrap();
let AddVersionResult::Ok(new_version) = res else {
panic!("expected OK");
};
Expand All @@ -808,7 +832,7 @@ mod tests {
let (v1, v2) = (Uuid::new_v4(), Uuid::new_v4());
server.mock_add_version(v1, v2, 1000, b"first");
server.mock_set_latest(v2);
let (res, _) = server.add_version(v1, b"history".into()).unwrap();
let (res, _) = server.add_version(v1, b"history".to_vec()).unwrap();
assert_eq!(res, AddVersionResult::ExpectedParentVersion(v2));
let mut expected = server.empty_copy();
expected.mock_add_version(v1, v2, 1000, b"first");
Expand All @@ -831,7 +855,7 @@ mod tests {
server.add_version_intercept = Some(|service| {
service.put(&LATEST, &version_to_bytes(V3)).unwrap();
});
let (res, _) = server.add_version(v2, b"history".into()).unwrap();
let (res, _) = server.add_version(v2, b"history".to_vec()).unwrap();
assert_eq!(res, AddVersionResult::ExpectedParentVersion(V3));
let mut expected = server.empty_copy();
expected.mock_add_version(v1, v2, 1000, b"first");
Expand All @@ -847,7 +871,7 @@ mod tests {
server.mock_add_version(v1, v2, 1000, b"first");
server.mock_set_latest(v2);
let (res, _) = server
.add_version(Uuid::new_v4(), b"history".into())
.add_version(Uuid::new_v4(), b"history".to_vec())
.unwrap();
assert_eq!(res, AddVersionResult::ExpectedParentVersion(v2));
let mut expected = server.empty_copy();
Expand Down Expand Up @@ -1100,7 +1124,7 @@ mod tests {
fn add_snapshot() {
let mut server = make_server();
let v = Uuid::new_v4();
server.add_snapshot(v, b"SNAP".into()).unwrap();
server.add_snapshot(v, b"SNAP".to_vec()).unwrap();
let mut expected = server.empty_copy();
expected.mock_add_snapshot(v, INSERTION_TIME, b"SNAP");
assert_eq!(server.unencrypted(), expected.unencrypted());
Expand All @@ -1117,6 +1141,6 @@ mod tests {
let mut server = make_server();
let v = Uuid::new_v4();
server.mock_add_snapshot(v, 1000, b"SNAP");
assert_eq!(server.get_snapshot().unwrap(), Some((v, b"SNAP".into())));
assert_eq!(server.get_snapshot().unwrap(), Some((v, b"SNAP".to_vec())));
}
}

0 comments on commit 9f90a75

Please sign in to comment.