Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e477b89

Browse files
committedJan 9, 2025··
some more wasm tests
1 parent 86f6318 commit e477b89

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed
 

‎Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ cuid-util = { path = "./crates/cuid-util", version = "0.1.1" }
1414
cuid1 = { path = "./crates/cuid1", version = "0.1.0" }
1515
cuid2 = { path = "./crates/cuid2", version = "0.1.3" }
1616
criterion = "0.5.0"
17+
paste = "1.0.15"
1718
proptest = "1.0.0"
1819
rand = "0.8"
1920
wasm-bindgen-test = "0.3.0"

‎crates/cuid2/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ rand.workspace = true
3030
sha3 = "0.10.6"
3131

3232
[dev-dependencies]
33+
paste.workspace = true
3334
radix_fmt = "1.0.0"
3435
wasm-bindgen-test.workspace = true
3536

‎crates/cuid2/src/lib.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use std::{
6060
hash::{Hash, Hasher},
6161
};
6262

63+
// std::time::SystemTime panics on WASM, so use a different library there.
6364
#[cfg(not(target_family = "wasm"))]
6465
use std::time::{SystemTime, UNIX_EPOCH};
6566
#[cfg(target_family = "wasm")]
@@ -96,6 +97,7 @@ fn fingerprint() -> String {
9697
thread_rng().gen::<u128>().to_be_bytes(),
9798
#[cfg(not(target_family = "wasm"))]
9899
u128::from(std::process::id()).to_be_bytes(),
100+
// WASM has no concept of a PID, so just use another random block
99101
#[cfg(target_family = "wasm")]
100102
thread_rng().gen::<u128>().to_be_bytes(),
101103
u128::from(get_thread_id()).to_be_bytes(),
@@ -464,6 +466,18 @@ mod test {
464466

465467
use super::*;
466468

469+
/// Run an already-defined test in WASM as well.
470+
macro_rules! wasm_test {
471+
($name:ident) => {
472+
paste::paste! {
473+
#[wasm_bindgen_test::wasm_bindgen_test]
474+
fn [<wasm_ $name>]() {
475+
$name()
476+
}
477+
}
478+
};
479+
}
480+
467481
#[test]
468482
fn counter_increments() {
469483
let start = get_count();
@@ -472,17 +486,32 @@ mod test {
472486
// concurrent test may have also incremented
473487
assert!(next > start);
474488
}
489+
wasm_test!(counter_increments);
490+
491+
#[test]
492+
fn cuid_generation() {
493+
assert!(is_cuid(cuid()))
494+
}
495+
wasm_test!(cuid_generation);
475496

497+
// lesser version of the collisions test for WASM
476498
#[wasm_bindgen_test::wasm_bindgen_test]
477-
fn wasm_cuid_does_not_panic() {
478-
cuid();
499+
fn wasm_collisions() {
500+
let count = 10_000;
501+
let cuids = (0..count).fold(HashSet::with_capacity(count), |mut acc, _| {
502+
acc.insert(cuid());
503+
acc
504+
});
505+
assert_eq!(count, cuids.len());
479506
}
480507

481-
#[cfg(not(target_family = "wasm"))]
508+
#[cfg(not(target_family = "wasm"))] // uses num_cpus, which we can't compile on wasm
482509
#[test]
483510
#[ignore] // slow: run explicitly when desired
484511
fn collisions() {
485512
// generate ~10e6 IDs across all available cores
513+
514+
use wasm_bindgen_test::wasm_bindgen_test;
486515
let cores = num_cpus::get();
487516
let per_core = 10_000_000 / cores;
488517

0 commit comments

Comments
 (0)
Please sign in to comment.