Skip to content

Commit

Permalink
caching_session: use custom hasher instead of ahash in tests
Browse files Browse the repository at this point in the history
Currently, we are importing the `ahash` crate for only one reason: to
test that CachingSession works with custom hasher implementations. The
custom hasher quality doesn't matter at all in this test, so it's very
easy to write own implementation for testing purposes, rendering the
dependency on `ahash` obsolete.

The change was prompted by a report from one of our users which use a
fork of the driver and managed to trigger a bug in cargo - most likely
this one: rust-lang/cargo#7463 .
The most likely cause was that the project specified a different,
non-compatible version of ahash in the dependencies than the one in
scylla's dev-dependencies. While it doesn't fix the cargo bug and it can
still happen with other dev-dependencies, the bug should no longer
happen with conflicting `ahash`. Moreover, removing an unneeded
depdencency from a project is a good motivation in itself.
  • Loading branch information
piodul committed Jan 18, 2023
1 parent 0e139c9 commit 51bcc2c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
1 change: 0 additions & 1 deletion scylla/Cargo.toml
Expand Up @@ -51,7 +51,6 @@ ntest = "0.8.1"
criterion = "0.3"
tracing-subscriber = { version = "0.3.14", features = ["env-filter"] }
assert_matches = "1.5.0"
ahash = { version = "0.8.0", default-features = false, features=["compile-time-rng"] }

[[bench]]
name = "benchmark"
Expand Down
25 changes: 23 additions & 2 deletions scylla/src/transport/caching_session.rs
Expand Up @@ -392,11 +392,32 @@ mod tests {
/// This test checks that we can construct a CachingSession with custom HashBuilder implementations
#[tokio::test]
async fn test_custom_hasher() {
#[derive(Default, Clone)]
struct CustomBuildHasher;
impl std::hash::BuildHasher for CustomBuildHasher {
type Hasher = CustomHasher;
fn build_hasher(&self) -> Self::Hasher {
CustomHasher(0)
}
}

struct CustomHasher(u8);
impl std::hash::Hasher for CustomHasher {
fn write(&mut self, bytes: &[u8]) {
for b in bytes {
self.0 ^= *b;
}
}
fn finish(&self) -> u64 {
self.0 as u64
}
}

let _session: CachingSession<std::collections::hash_map::RandomState> =
CachingSession::from(new_for_test().await, 2);
let _session: CachingSession<ahash::RandomState> =
let _session: CachingSession<CustomBuildHasher> =
CachingSession::from(new_for_test().await, 2);
let _session: CachingSession<ahash::RandomState> =
let _session: CachingSession<CustomBuildHasher> =
CachingSession::with_hasher(new_for_test().await, 2, Default::default());
}

Expand Down

0 comments on commit 51bcc2c

Please sign in to comment.