node: Minimize inventory, fix tests

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier 2022-08-29 15:25:00 +02:00
parent 8a66cfa776
commit 2c519f5b5f
No known key found for this signature in database
6 changed files with 35 additions and 31 deletions

View File

@ -73,7 +73,7 @@ pub enum Message {
/// Send our inventory to a peer. Sent in response to [`Message::GetInventory`].
/// Nb. This should be the whole inventory, not a partial update.
Inventory {
inv: Inventory,
inv: Vec<ProjId>,
timestamp: Timestamp,
/// Original peer this inventory came from. We don't set this when we
/// are the originator, only when relaying.
@ -253,7 +253,6 @@ impl<T: ReadStorage + WriteStorage, S: address_book::Store> Protocol<S, T> {
.storage
.inventory()?
.into_iter()
.map(|(id, _)| id)
.filter(|id| !blocked.contains(id))
.collect(),
@ -714,7 +713,7 @@ where
/// Process a peer inventory announcement by updating our routing table.
fn process_inventory(&mut self, inventory: &Inventory, from: PeerId) {
for (proj_id, _refs) in inventory {
for proj_id in inventory {
let inventory = self
.routing
.entry(proj_id.clone())

View File

@ -21,7 +21,7 @@ use crate::identity::{ProjId, ProjIdError, UserId};
pub static IDENTITY_PATH: Lazy<&Path> = Lazy::new(|| Path::new(".rad/identity.toml"));
pub type BranchName = String;
pub type Inventory = Vec<(ProjId, HashMap<String, Remote<Unverified>>)>;
pub type Inventory = Vec<ProjId>;
/// Storage error.
#[derive(Error, Debug)]

View File

@ -46,20 +46,7 @@ impl ReadStorage for Storage {
}
fn inventory(&self) -> Result<Inventory, Error> {
let projs = self.projects()?;
let mut inv = Vec::new();
for proj in projs {
let repo = self.repository(&proj)?;
let remotes = repo
.remotes()?
.into_iter()
.map(|(id, r)| (id.to_string(), r))
.collect();
inv.push((proj, remotes));
}
Ok(inv)
self.projects()
}
}
@ -226,7 +213,7 @@ mod tests {
let dir = tempfile::tempdir().unwrap();
let storage = fixtures::storage(dir.path());
let inv = storage.inventory().unwrap();
let (proj, _) = inv.first().unwrap();
let proj = inv.first().unwrap();
let refs = git::list_remotes(&Url {
host: Some(dir.path().to_string_lossy().to_string()),
scheme: git_url::Scheme::File,
@ -242,11 +229,12 @@ mod tests {
#[test]
fn test_fetch() {
let path = tempfile::tempdir().unwrap().into_path();
let alice = fixtures::storage(path.join("alice"));
let bob = Storage::new(path.join("bob"));
let tmp = tempfile::tempdir().unwrap();
let alice = fixtures::storage(tmp.path().join("alice"));
let bob = Storage::new(tmp.path().join("bob"));
let inventory = alice.inventory().unwrap();
let (proj, remotes) = inventory.first().unwrap();
let proj = inventory.first().unwrap();
let remotes = alice.repository(proj).unwrap().remotes().unwrap();
let refname = "refs/heads/master";
// Have Bob fetch Alice's refs.
@ -260,16 +248,16 @@ mod tests {
})
.unwrap();
for remote in remotes.values() {
for (id, _) in remotes.into_iter() {
let alice_oid = alice
.repository(proj)
.unwrap()
.find_reference(&remote.id, refname)
.find_reference(&id, refname)
.unwrap();
let bob_oid = bob
.repository(proj)
.unwrap()
.find_reference(&remote.id, refname)
.find_reference(&id, refname)
.unwrap();
assert_eq!(alice_oid, bob_oid);

View File

@ -1,9 +1,24 @@
use std::collections::HashSet;
use std::hash::Hash;
use std::ops::RangeBounds;
use crate::collections::HashMap;
use crate::hash;
use crate::identity::{ProjId, UserId};
use crate::storage;
use crate::test::storage::MockStorage;
pub fn set<T: Eq + Hash + quickcheck::Arbitrary>(range: impl RangeBounds<usize>) -> HashSet<T> {
let size = fastrand::usize(range);
let mut set = HashSet::with_capacity(size);
let mut g = quickcheck::Gen::new(size);
while set.len() < size {
set.insert(T::arbitrary(&mut g));
}
set
}
pub fn gen<T: quickcheck::Arbitrary>(size: usize) -> T {
let mut gen = quickcheck::Gen::new(size);

View File

@ -8,8 +8,10 @@ use crate::test::arbitrary;
pub fn storage<P: AsRef<Path>>(path: P) -> Storage {
let path = path.as_ref();
let storage = Storage::new(path);
let proj_ids = arbitrary::gen::<Vec<ProjId>>(9);
let user_ids = arbitrary::gen::<Vec<UserId>>(9);
let proj_ids = arbitrary::set::<ProjId>(3..5);
let user_ids = arbitrary::set::<UserId>(1..3);
crate::test::logger::init(log::Level::Debug);
for proj in proj_ids.iter() {
log::debug!("creating {}...", proj);
@ -70,8 +72,8 @@ mod tests {
#[test]
fn smoke() {
let path = tempfile::tempdir().unwrap().into_path();
let tmp = tempfile::tempdir().unwrap();
storage(&path);
storage(&tmp.path());
}
}

View File

@ -34,7 +34,7 @@ impl ReadStorage for MockStorage {
let inventory = self
.inventory
.iter()
.map(|(id, remotes)| (id.clone(), remotes.clone().into()))
.map(|(id, _)| id.clone())
.collect::<Vec<_>>();
Ok(inventory)