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`]. /// Send our inventory to a peer. Sent in response to [`Message::GetInventory`].
/// Nb. This should be the whole inventory, not a partial update. /// Nb. This should be the whole inventory, not a partial update.
Inventory { Inventory {
inv: Inventory, inv: Vec<ProjId>,
timestamp: Timestamp, timestamp: Timestamp,
/// Original peer this inventory came from. We don't set this when we /// Original peer this inventory came from. We don't set this when we
/// are the originator, only when relaying. /// are the originator, only when relaying.
@ -253,7 +253,6 @@ impl<T: ReadStorage + WriteStorage, S: address_book::Store> Protocol<S, T> {
.storage .storage
.inventory()? .inventory()?
.into_iter() .into_iter()
.map(|(id, _)| id)
.filter(|id| !blocked.contains(id)) .filter(|id| !blocked.contains(id))
.collect(), .collect(),
@ -714,7 +713,7 @@ where
/// Process a peer inventory announcement by updating our routing table. /// Process a peer inventory announcement by updating our routing table.
fn process_inventory(&mut self, inventory: &Inventory, from: PeerId) { fn process_inventory(&mut self, inventory: &Inventory, from: PeerId) {
for (proj_id, _refs) in inventory { for proj_id in inventory {
let inventory = self let inventory = self
.routing .routing
.entry(proj_id.clone()) .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 static IDENTITY_PATH: Lazy<&Path> = Lazy::new(|| Path::new(".rad/identity.toml"));
pub type BranchName = String; pub type BranchName = String;
pub type Inventory = Vec<(ProjId, HashMap<String, Remote<Unverified>>)>; pub type Inventory = Vec<ProjId>;
/// Storage error. /// Storage error.
#[derive(Error, Debug)] #[derive(Error, Debug)]

View File

@ -46,20 +46,7 @@ impl ReadStorage for Storage {
} }
fn inventory(&self) -> Result<Inventory, Error> { fn inventory(&self) -> Result<Inventory, Error> {
let projs = self.projects()?; 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)
} }
} }
@ -226,7 +213,7 @@ mod tests {
let dir = tempfile::tempdir().unwrap(); let dir = tempfile::tempdir().unwrap();
let storage = fixtures::storage(dir.path()); let storage = fixtures::storage(dir.path());
let inv = storage.inventory().unwrap(); let inv = storage.inventory().unwrap();
let (proj, _) = inv.first().unwrap(); let proj = inv.first().unwrap();
let refs = git::list_remotes(&Url { let refs = git::list_remotes(&Url {
host: Some(dir.path().to_string_lossy().to_string()), host: Some(dir.path().to_string_lossy().to_string()),
scheme: git_url::Scheme::File, scheme: git_url::Scheme::File,
@ -242,11 +229,12 @@ mod tests {
#[test] #[test]
fn test_fetch() { fn test_fetch() {
let path = tempfile::tempdir().unwrap().into_path(); let tmp = tempfile::tempdir().unwrap();
let alice = fixtures::storage(path.join("alice")); let alice = fixtures::storage(tmp.path().join("alice"));
let bob = Storage::new(path.join("bob")); let bob = Storage::new(tmp.path().join("bob"));
let inventory = alice.inventory().unwrap(); 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"; let refname = "refs/heads/master";
// Have Bob fetch Alice's refs. // Have Bob fetch Alice's refs.
@ -260,16 +248,16 @@ mod tests {
}) })
.unwrap(); .unwrap();
for remote in remotes.values() { for (id, _) in remotes.into_iter() {
let alice_oid = alice let alice_oid = alice
.repository(proj) .repository(proj)
.unwrap() .unwrap()
.find_reference(&remote.id, refname) .find_reference(&id, refname)
.unwrap(); .unwrap();
let bob_oid = bob let bob_oid = bob
.repository(proj) .repository(proj)
.unwrap() .unwrap()
.find_reference(&remote.id, refname) .find_reference(&id, refname)
.unwrap(); .unwrap();
assert_eq!(alice_oid, bob_oid); 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::collections::HashMap;
use crate::hash; use crate::hash;
use crate::identity::{ProjId, UserId}; use crate::identity::{ProjId, UserId};
use crate::storage; use crate::storage;
use crate::test::storage::MockStorage; 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 { pub fn gen<T: quickcheck::Arbitrary>(size: usize) -> T {
let mut gen = quickcheck::Gen::new(size); 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 { pub fn storage<P: AsRef<Path>>(path: P) -> Storage {
let path = path.as_ref(); let path = path.as_ref();
let storage = Storage::new(path); let storage = Storage::new(path);
let proj_ids = arbitrary::gen::<Vec<ProjId>>(9); let proj_ids = arbitrary::set::<ProjId>(3..5);
let user_ids = arbitrary::gen::<Vec<UserId>>(9); let user_ids = arbitrary::set::<UserId>(1..3);
crate::test::logger::init(log::Level::Debug);
for proj in proj_ids.iter() { for proj in proj_ids.iter() {
log::debug!("creating {}...", proj); log::debug!("creating {}...", proj);
@ -70,8 +72,8 @@ mod tests {
#[test] #[test]
fn smoke() { 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 let inventory = self
.inventory .inventory
.iter() .iter()
.map(|(id, remotes)| (id.clone(), remotes.clone().into())) .map(|(id, _)| id.clone())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Ok(inventory) Ok(inventory)