Skip to content

Commit

Permalink
Some testing improvements (#1275)
Browse files Browse the repository at this point in the history
Since #1244, we were no longer installing a global tracing subscriber
that would print logs from (failing) tests. With this, we do again.

First two commits are drive-by improvements in response to local
warnings from my nightly toolchain about redundant imports.
  • Loading branch information
svix-jplatte authored Mar 14, 2024
2 parents de5b298 + c83a2f5 commit fd21a22
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 24 deletions.
6 changes: 5 additions & 1 deletion rust/src/webhooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,12 @@ mod private {
#[cfg(test)]
mod tests {
use http02::HeaderMap;
use time::OffsetDateTime;

use super::*;
use super::{
Webhook, SVIX_MSG_ID_KEY, SVIX_MSG_SIGNATURE_KEY, SVIX_MSG_TIMESTAMP_KEY,
UNBRANDED_MSG_ID_KEY, UNBRANDED_MSG_SIGNATURE_KEY, UNBRANDED_MSG_TIMESTAMP_KEY,
};

fn get_svix_headers(msg_id: &str, signature: &str) -> HeaderMap {
let mut headers = HeaderMap::new();
Expand Down
11 changes: 11 additions & 0 deletions server/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 server/svix-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ anyhow = "1.0.56"
assert_matches = "1.5.0"
# NOTE: Purposely not the latest version such as not to mess up the `hyper` fork patch
axum-server = { version = "0.5", features = ["tls-openssl"] }
ctor = "0.2.7"

[features]
default = ["jemalloc"]
Expand Down
11 changes: 9 additions & 2 deletions server/svix-server/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,15 @@ pub fn load() -> Result<Arc<ConfigurationInner>> {

#[cfg(test)]
mod tests {
use super::*;
use crate::core::security::JWTAlgorithm;
use std::{sync::Arc, time::Duration};

use figment::{
providers::{Format as _, Toml},
Figment,
};

use super::{load, CacheBackend, CacheType, QueueBackend, QueueType};
use crate::core::security::{JWTAlgorithm, JwtSigningConfig};

#[test]
fn test_retry_schedule_parsing() {
Expand Down
2 changes: 1 addition & 1 deletion server/svix-server/src/core/cryptography.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Default for Encryption {

#[cfg(test)]
mod tests {
use super::*;
use super::Encryption;

#[test]
fn test_encryption() {
Expand Down
19 changes: 11 additions & 8 deletions server/svix-server/src/core/message_app.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
collections::HashSet,
convert::{TryFrom, TryInto},
time::Duration,
};
use std::{collections::HashSet, time::Duration};

use chrono::{DateTime, FixedOffset, Utc};
use sea_orm::{DatabaseConnection, DatabaseTransaction, TransactionTrait};
Expand Down Expand Up @@ -219,9 +215,16 @@ impl AppEndpointKey {

#[cfg(test)]
mod tests {
use super::*;
use crate::core::cryptography::Encryption;
use crate::core::types::{EndpointSecret, ExpiringSigningKey};
use chrono::Utc;

use super::CreateMessageEndpoint;
use crate::core::{
cryptography::Encryption,
types::{
EndpointId, EndpointSecret, EndpointSecretInternal, ExpiringSigningKey,
ExpiringSigningKeys,
},
};

#[test]
fn test_valid_signing_keys() {
Expand Down
10 changes: 7 additions & 3 deletions server/svix-server/src/core/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,12 +1371,16 @@ pub type FeatureFlagSet = HashSet<FeatureFlag>;

#[cfg(test)]
mod tests {
use super::*;
use crate::core::types::{EventChannel, EventTypeName};

use std::collections::HashMap;

use validator::Validate;

use super::{
validate_header_map, ApplicationId, ApplicationUid, EndpointHeaders, EndpointHeadersPatch,
EndpointSecret, EventChannel, EventTypeName,
};
use crate::core::cryptography::AsymmetricKey;

#[test]
fn test_id_validation() {
let app_id = ApplicationId("app_24NVKcPqNLXKu3xQhJnw8fSumZK".to_owned());
Expand Down
5 changes: 4 additions & 1 deletion server/svix-server/src/core/webhook_http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,15 @@ mod tests {
net::{IpAddr, TcpListener},
path::PathBuf,
str::FromStr,
sync::Arc,
};

use axum::{routing, Router};
use axum_server::tls_openssl::{OpenSSLAcceptor, OpenSSLConfig};
use http::{HeaderValue, Method, Version};
use ipnet::IpNet;

use super::*;
use super::{is_allowed, CaseSensitiveHeaderMap, RequestBuilder, WebhookClient};

#[test]
fn is_allowed_test() {
Expand Down
22 changes: 22 additions & 0 deletions server/svix-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,28 @@ pub fn setup_tracing(
(registry, sentry_guard)
}

pub fn setup_tracing_for_tests() {
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
// Output is only printed for failing tests, but still we shouldn't overload
// the output with unnecessary info. When debugging a specific test, it's easy
// to override this default by setting the `RUST_LOG` environment variable.
"svix_server=debug".into()
}),
)
.with(tracing_subscriber::fmt::layer().with_test_writer())
.init();
}

#[cfg(test)]
#[ctor::ctor]
fn test_setup() {
setup_tracing_for_tests();
}

mod docs {
use aide::{axum::ApiRouter, openapi::OpenApi};
use axum::{
Expand Down
8 changes: 4 additions & 4 deletions server/svix-server/src/redis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ pub async fn new_redis_pool(redis_dsn: &str, cfg: &Configuration) -> RedisPool {
mod tests {
use redis::AsyncCommands;

use super::*;
use crate::cfg::CacheType;
use super::RedisPool;
use crate::cfg::{CacheType, Configuration};

async fn get_pool(redis_dsn: &str, cfg: &Configuration) -> RedisPool {
match cfg.cache_type {
CacheType::RedisCluster => crate::redis::new_redis_pool_clustered(redis_dsn, cfg).await,
CacheType::Redis => crate::redis::new_redis_pool(redis_dsn, cfg).await,
CacheType::RedisCluster => super::new_redis_pool_clustered(redis_dsn, cfg).await,
CacheType::Redis => super::new_redis_pool(redis_dsn, cfg).await,
_ => panic!(
"This test should only be run when redis is configured as the cache provider"
),
Expand Down
11 changes: 7 additions & 4 deletions server/svix-server/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,13 +1004,16 @@ pub async fn queue_handler(

#[cfg(test)]
mod tests {
use super::*;
use crate::core::cryptography::AsymmetricKey;
use crate::core::types::{BaseId, EndpointSecret};
use std::collections::HashMap;

use bytes::Bytes;
use ed25519_compact::Signature;
use std::collections::HashMap;

use super::{bytes_to_string, generate_msg_headers, sign_msg, CaseSensitiveHeaderMap};
use crate::core::{
cryptography::{AsymmetricKey, Encryption},
types::{BaseId, EndpointHeaders, EndpointSecret, EndpointSecretInternal, MessageId},
};

// [`generate_msg_headers`] tests
const TIMESTAMP: i64 = 1;
Expand Down
5 changes: 5 additions & 0 deletions server/svix-server/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ mod message_app;
mod redis_queue;
mod utils;
mod worker;

#[ctor::ctor]
fn test_setup() {
svix_server::setup_tracing_for_tests();
}

0 comments on commit fd21a22

Please sign in to comment.