Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Multiset for verifying no duplicate chunks #2278

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions tests/collections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! Custom collections used in our tests.
//!
//! Ideally, we would eventually move these to their own crate.
//! The only reason we have this in the `tests` directory for now,
//! is that it is only used in tests, so should not be compiled into
//! production code.

use std::collections::HashMap;
use std::hash::Hash;

/// An unordered collection of items that allows duplicates.
/// This is implemented as a `HashMap` where the keys are the items
/// and the values are the counts of the items.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HashMultiSet<T>
where
T: Hash + Eq,
{
pub inner: HashMap<T, usize>,
}

impl<T> HashMultiSet<T>
where
T: Hash + Eq,
{
pub fn new() -> Self {
Self {
inner: HashMap::new(),
}
}

pub fn insert(&mut self, item: T) {
*self.inner.entry(item).or_default() += 1;
}
}

impl<T> FromIterator<T> for HashMultiSet<T>
where
T: Hash + Eq,
{
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
let mut set = Self::new();
for item in iter {
set.insert(item);
}
set
}
}
5 changes: 3 additions & 2 deletions tests/integration/test_utils/chunk_upload.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Utilities for chunk upload tests.
use std::collections::HashSet;
use std::error::Error;
use std::str;
use std::sync::LazyLock;

use mockito::Request;
use regex::bytes::Regex;

use crate::collections::HashMultiSet;

/// This regex is used to extract the boundary from the content-type header.
/// We need to match the boundary, since it changes with each request.
/// The regex matches the format as specified in
Expand Down Expand Up @@ -41,7 +42,7 @@ impl HeaderContainer for Request {
pub fn split_chunk_body<'b>(
body: &'b [u8],
boundary: &str,
) -> Result<HashSet<&'b [u8]>, Box<dyn Error>> {
) -> Result<HashMultiSet<&'b [u8]>, Box<dyn Error>> {
let escaped_boundary = regex::escape(boundary);

let inner_body = entire_body_regex(&escaped_boundary)
Expand Down
1 change: 1 addition & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod collections;
mod integration;
Loading