Skip to content

Improve tests for DownloadsCounter #3477

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

Merged
merged 2 commits into from
Mar 31, 2021
Merged
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
29 changes: 24 additions & 5 deletions src/downloads_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl DownloadsCounter {
shard: None,
counted_downloads,
counted_versions,
pending_downloads: old_pending - counted_downloads as i64,
pending_downloads: old_pending - counted_downloads as i64 - discarded_downloads as i64,
})
}

Expand Down Expand Up @@ -357,16 +357,35 @@ mod tests {
state.assert_downloads_count(&conn, v2, 5);
}

// TODO: temporarily ignored due to https://github.com/rust-lang/crates.io/issues/3462
#[test]
#[ignore]
fn test_increment_missing_version() {
fn test_increment_existing_and_missing_version_same_shard() {
test_increment_existing_and_missing_version(|map, v1, v2| {
map.determine_map(&v1) == map.determine_map(&v2)
})
}

#[test]
fn test_increment_existing_and_missing_version_different_shard() {
test_increment_existing_and_missing_version(|map, v1, v2| {
map.determine_map(&v1) != map.determine_map(&v2)
})
}

fn test_increment_existing_and_missing_version<F>(shard_condition: F)
where
F: Fn(&DashMap<i32, AtomicUsize>, i32, i32) -> bool,
{
let counter = DownloadsCounter::new();
let conn = crate::db::test_conn();
let mut state = State::new(&conn);

let v1 = state.new_version(&conn);
let v2 = v1 + 1; // Should not exist in the database!

// Generate the second version. It should **not** already be in the database.
let mut v2 = v1 + 1;
while !shard_condition(&counter.inner, v1, v2) {
v2 += 1;
}

// No error should happen when calling the increment method on a missing version.
counter.increment(v1);
Expand Down