Skip to content

Commit

Permalink
Add test-util features to hide test utilities
Browse files Browse the repository at this point in the history
We had a handful of items intended for use exclusively in tests that
were marked `pub` so that targets like `monolithic_integration_test` can
use them, but tagged `#[doc(hidden)]` because they aren't part of the
proper public interface to any component of Janus. We now instead hide
those test utilities behind a new `test-util` feature defined on crates
`janus_core`, `janus_client` and `janus_server`. We could also have
moved items to the `test_util` crate but there's something to be said
for having e.g. the code that inserts tasks into the datastore be right
next to all the other datastore code, instead of `test_util/lib.rs`
becoming a dumping ground of items unrelated to each other save that
they are used in tests. Additionally, if we ever want to "promote" any
of these `test_util` items to being used in production configurations,
that's a simple matter of deleting a `#[cfg(feature = "test-util")]`
instead of having to move a function and annihilate its git history.

Resolves #141
  • Loading branch information
tgeoghegan committed Jul 6, 2022
1 parent 40a176d commit 4463aed
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions janus_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ url = "2.2.2"

[dev-dependencies]
assert_matches = "1"
janus_core = { path = "../janus_core", features = ["test-util"]}
janus_test_util = { path = "../test_util" }
mockito = "0.31.0"
tracing-log = "0.1.3"
Expand Down
1 change: 1 addition & 0 deletions janus_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rust-version = "1.58"

[features]
database = ["bytes", "postgres-protocol", "postgres-types"]
test-util = []

[dependencies]
anyhow = "1"
Expand Down
3 changes: 1 addition & 2 deletions janus_core/src/hpke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ pub fn open(
.map_err(Into::into)
}

// This is public to allow use in integration tests.
#[doc(hidden)]
#[cfg(feature = "test-util")]
pub mod test_util {
use crate::{
hpke::HpkePrivateKey,
Expand Down
2 changes: 2 additions & 0 deletions janus_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tokio-console = ["console-subscriber"]
jaeger = ["tracing-opentelemetry", "opentelemetry-jaeger"]
otlp = ["tracing-opentelemetry", "opentelemetry-otlp", "opentelemetry-semantic-conventions", "tonic"]
prometheus = ["opentelemetry-prometheus", "dep:prometheus"]
test-util = []

[dependencies]
anyhow = "1"
Expand Down Expand Up @@ -62,6 +63,7 @@ warp = { version = "^0.3", features = ["tls"] }
assert_matches = "1"
hex = { version = "0.4.3", features = ["serde"] }
hyper = "0.14.19"
janus_server = { path = ".", features = ["test-util"] }
janus_test_util = { path = "../test_util" }
lazy_static = "1"
libc = "0.2.126"
Expand Down
9 changes: 4 additions & 5 deletions janus_server/src/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ pub struct Transaction<'a, C: Clock> {
}

impl<C: Clock> Transaction<'_, C> {
// This is pub to be used in integration tests
#[doc(hidden)]
/// Allows tests to insert tasks into the datastore.
#[cfg(feature = "test-util")]
#[tracing::instrument(skip(self), err)]
pub async fn put_task(&self, task: &Task) -> Result<(), Error> {
let aggregator_role = AggregatorRole::from_role(task.role)?;
Expand Down Expand Up @@ -817,10 +817,9 @@ impl<C: Clock> Transaction<'_, C> {
.transpose()
}

/// get_aggregation_jobs_for_task_id returns all aggregation jobs for a given task ID. It is
/// intended for use in tests.
/// get_aggregation_jobs_for_task_id returns all aggregation jobs for a given task ID.
#[cfg(feature = "test-util")]
#[tracing::instrument(skip(self), err)]
#[doc(hidden)]
pub async fn get_aggregation_jobs_for_task_id<const L: usize, A: vdaf::Aggregator<L>>(
&self,
task_id: TaskId,
Expand Down
2 changes: 1 addition & 1 deletion janus_server/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ impl Decode for CollectResp {
}
}

#[doc(hidden)]
#[cfg(feature = "test-util")]
pub mod test_util {
use super::{Nonce, TaskId};
use janus_core::message::{Report, Time};
Expand Down
3 changes: 1 addition & 2 deletions janus_server/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ impl Task {
}
}

// This is public to allow use in integration tests.
#[doc(hidden)]
#[cfg(feature = "test-util")]
pub mod test_util {
use std::iter;

Expand Down
2 changes: 1 addition & 1 deletion monolithic_integration_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ deadpool-postgres = "0.10.1"
futures = "0.3.21"
janus_core = { path = "../janus_core" }
janus_client = { path = "../janus_client" }
janus_server = { path = "../janus_server" }
janus_server = { path = "../janus_server", features = ["test-util"] }
lazy_static = "1"
prio = "0.8.0"
rand = "0.8"
Expand Down

0 comments on commit 4463aed

Please sign in to comment.