Skip to content

Commit

Permalink
WASM HouseKeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Nov 27, 2024
1 parent 4512c27 commit befcac9
Show file tree
Hide file tree
Showing 43 changed files with 324 additions and 332 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"rust-analyzer.cargo.sysroot": "discover",
"rust-analyzer.cargo.allTargets": false,
"rust-analyzer.cargo.target": "wasm32-unknown-unknown",
"rust-analyzer.linkedProjects": ["Cargo.toml"],
"rust-analyzer.procMacro.enable": true,
"rust-analyzer.diagnostics.enable": true,
Expand Down
42 changes: 22 additions & 20 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ xmtp_api_grpc = { path = "xmtp_api_grpc" }
xmtp_id = { path = "xmtp_id" }
xmtp_mls = { path = "xmtp_mls" }
xmtp_proto = { path = "xmtp_proto" }
xmtp-common = { path = "common" }
xmtp_common = { path = "common" }

[profile.dev]
# Disabling debug info speeds up builds a bunch,
Expand Down
4 changes: 2 additions & 2 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1942,12 +1942,12 @@ mod tests {
}
}

pub fn rand_string() -> String {
pub fn rand_string::<24>() -> String {
Alphanumeric.sample_string(&mut rand::thread_rng(), 24)
}

pub fn tmp_path() -> String {
let db_name = rand_string();
let db_name = rand_string::<24>();
format!("{}/{}.db3", env::temp_dir().to_str().unwrap(), db_name)
}

Expand Down
13 changes: 8 additions & 5 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
[package]
name = "xmtp-common"
name = "xmtp_common"
edition = "2021"
version.workspace = true
license.workspace = true

[dependencies]
web-time.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
tokio = { workspace = true, features = ["time"] }
rand = "0.8"

parking_lot = { workspace = true, optional = true }
futures = { workspace = true, optional = true }
tracing-subscriber = { workspace = true, features = ["fmt", "env-filter", "ansi"], optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { workspace = true, features = ["js"] }
gloo-timers = { workspace = true, features = ["futures"] }
tracing-web = { version = "0.1", optional = true }
console_error_panic_hook = { version = "0.1", optional = true }

[dev-dependencies]
thiserror.workspace = true

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
tracing-web = "0.1"
console_error_panic_hook = "0.1"
tokio = { workspace = true, features = ["time", "macros", "rt", "sync"]}
wasm-bindgen-test.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { workspace = true, features = ["time", "macros", "rt-multi-thread", "sync"]}

[features]
test-utils = []
test-utils = ["dep:parking_lot", "dep:futures", "dep:tracing-subscriber", "dep:tracing-web", "dep:console_error_panic_hook"]
48 changes: 38 additions & 10 deletions common/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
//! Common Test Utilites
use rand::{
distributions::{Alphanumeric, DistString},
seq::IteratorRandom,
Rng, RngCore,
};
use std::cell::OnceCell;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
use std::sync::OnceLock;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

#[cfg(not(target_arch = "wasm32"))]
pub mod traced_test;
#[cfg(not(target_arch = "wasm32"))]
pub use traced_test::traced_test;

static INIT: OnceCell<()> = OnceCell::const_new();
mod macros;

static INIT: OnceLock<()> = OnceLock::new();

/// A simple test logger that defaults to the INFO level
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
pub fn logger() {
use tracing_subscriber::fmt;
INIT.get_or_init(|| {
let filter = EnvFilter::builder()
.with_default_directive(tracing::metadata::LevelFilter::INFO.into())
Expand All @@ -31,13 +35,18 @@ pub fn logger() {
/// A simple test logger that defaults to the INFO level
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
pub fn logger() {
use tracing_subscriber::fmt::format::Pretty;
INIT.get_or_init(|| {
let filter = EnvFilter::builder()
.with_default_directive(tracing::metadata::LevelFilter::INFO.into())
.from_env_lossy();

let fmt = tracing_subscriber::fmt::layer()
.with_ansi(true) // not supported by all browsers
.without_time() // std::time break things, but chrono might work
.with_writer(tracing_web::MakeWebConsoleWriter::new());

let subscriber = tracing_subscriber::registry()
tracing_subscriber::registry()
.with(fmt)
.with(filter)
.with(tracing_web::performance_layer().with_details_from_fields(Pretty::default()))
Expand All @@ -47,27 +56,46 @@ pub fn logger() {
});
}

pub fn rand_string() -> String {
Alphanumeric.sample_string(&mut rand::thread_rng(), 24)
pub fn rand_string<const N: usize>() -> String {
Alphanumeric.sample_string(&mut rand::thread_rng(), N)
}

pub fn rand_hexstring() -> String {
let hex_chars = "0123456789abcdef";
let v: String = (0..40)
.map(|_| hex_chars.chars().choose(&mut rand::thread_rng()).unwrap())
.collect();

format!("0x{}", v)
}

pub fn rand_account_address() -> String {
Alphanumeric.sample_string(&mut rand::thread_rng(), 42)
}

pub fn rand_vec() -> Vec<u8> {
rand::thread_rng().gen::<[u8; 24]>().to_vec()
pub fn rand_vec<const N: usize>() -> Vec<u8> {
rand_array::<N>().to_vec()
}

pub fn rand_array<const N: usize>() -> [u8; N] {
let mut buffer = [0u8; N];
rand::thread_rng().fill_bytes(&mut buffer);
buffer
}

pub fn rand_u64() -> u64 {
rand::thread_rng().gen()
}

#[cfg(not(target_arch = "wasm32"))]
pub fn tmp_path() -> String {
let db_name = rand_string();
let db_name = rand_string::<24>();
format!("{}/{}.db3", std::env::temp_dir().to_str().unwrap(), db_name)
}

#[cfg(target_arch = "wasm32")]
pub fn tmp_path() -> String {
let db_name = rand_string();
let db_name = rand_string::<24>();
format!("{}/{}.db3", "test_db", db_name)
}

Expand Down
2 changes: 1 addition & 1 deletion common/src/test/traced_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ where
#[macro_export]
macro_rules! assert_logged {
( $search:expr , $occurrences:expr ) => {
$crate::utils::test::traced_test::LOG_BUFFER.with(|buf| {
$crate::traced_test::LOG_BUFFER.with(|buf| {
let lines = {
buf.flush();
buf.as_string()
Expand Down
2 changes: 1 addition & 1 deletion mls_validation_service/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ mod tests {
#[tokio::test]
#[should_panic]
async fn test_get_association_state() {
let account_address = rand_string();
let account_address = rand_string::<24>();
let nonce = rand_u64();
let inbox_id = generate_inbox_id(&account_address, &nonce).unwrap();
let update = UnverifiedIdentityUpdate::new_test(
Expand Down
5 changes: 3 additions & 2 deletions xmtp_id/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ xmtp_cryptography.workspace = true
xmtp_proto = { workspace = true, features = ["proto_full"] }
web-time.workspace = true
ethers = { workspace = true, features = ["rustls"] }
xmtp-common.workspace = true
xmtp_common.workspace = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
openmls = { workspace = true, features = ["js"] }
Expand All @@ -41,9 +41,10 @@ openmls.workspace = true
[dev-dependencies]
xmtp_v2 = { path = "../xmtp_v2" }
ed25519-dalek = { workspace = true, features = ["digest", "rand_core"] }
xmtp_common = { workspace = true, features = ["test-utils"] }
wasm-bindgen-test.workspace = true

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test.workspace = true
gloo-timers.workspace = true

[features]
Expand Down
7 changes: 2 additions & 5 deletions xmtp_id/src/associations/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,12 @@ pub(crate) mod tests {
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_dedicated_worker);

use crate::associations::test_utils;

use super::*;

use test_utils::rand_string;
use xmtp_common::rand_hexstring;

impl Default for MemberIdentifier {
fn default() -> Self {
MemberIdentifier::Address(rand_string())
MemberIdentifier::Address(rand_hexstring())
}
}

Expand Down
Loading

0 comments on commit befcac9

Please sign in to comment.