@@ -60,6 +60,7 @@ use std::{
60
60
hash:: { Hash , Hasher } ,
61
61
} ;
62
62
63
+ // std::time::SystemTime panics on WASM, so use a different library there.
63
64
#[ cfg( not( target_family = "wasm" ) ) ]
64
65
use std:: time:: { SystemTime , UNIX_EPOCH } ;
65
66
#[ cfg( target_family = "wasm" ) ]
@@ -96,6 +97,7 @@ fn fingerprint() -> String {
96
97
thread_rng ( ) . gen :: < u128 > ( ) . to_be_bytes ( ) ,
97
98
#[ cfg( not( target_family = "wasm" ) ) ]
98
99
u128:: from ( std:: process:: id ( ) ) . to_be_bytes ( ) ,
100
+ // WASM has no concept of a PID, so just use another random block
99
101
#[ cfg( target_family = "wasm" ) ]
100
102
thread_rng ( ) . gen :: < u128 > ( ) . to_be_bytes ( ) ,
101
103
u128:: from ( get_thread_id ( ) ) . to_be_bytes ( ) ,
@@ -464,6 +466,18 @@ mod test {
464
466
465
467
use super :: * ;
466
468
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
+
467
481
#[ test]
468
482
fn counter_increments ( ) {
469
483
let start = get_count ( ) ;
@@ -472,17 +486,32 @@ mod test {
472
486
// concurrent test may have also incremented
473
487
assert ! ( next > start) ;
474
488
}
489
+ wasm_test ! ( counter_increments) ;
490
+
491
+ #[ test]
492
+ fn cuid_generation ( ) {
493
+ assert ! ( is_cuid( cuid( ) ) )
494
+ }
495
+ wasm_test ! ( cuid_generation) ;
475
496
497
+ // lesser version of the collisions test for WASM
476
498
#[ 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( ) ) ;
479
506
}
480
507
481
- #[ cfg( not( target_family = "wasm" ) ) ]
508
+ #[ cfg( not( target_family = "wasm" ) ) ] // uses num_cpus, which we can't compile on wasm
482
509
#[ test]
483
510
#[ ignore] // slow: run explicitly when desired
484
511
fn collisions ( ) {
485
512
// generate ~10e6 IDs across all available cores
513
+
514
+ use wasm_bindgen_test:: wasm_bindgen_test;
486
515
let cores = num_cpus:: get ( ) ;
487
516
let per_core = 10_000_000 / cores;
488
517
0 commit comments