1
+ use nodejs_snowflake:: { Snowflake , SnowflakeConfig } ;
2
+ use wasm_bindgen_test:: * ;
3
+
4
+ #[ wasm_bindgen_test]
5
+ fn create_snowflake_with_no_parameters ( ) {
6
+ let now = js_sys:: Date :: now ( ) ;
7
+
8
+ let uid = Snowflake :: new ( None ) . unwrap ( ) ;
9
+
10
+ let instance_id = uid. instance_id ( ) ;
11
+ let snowflake_epoch_offset = uid. custom_epoch ( ) ;
12
+
13
+ assert ! ( instance_id > 0.0 && instance_id < 4095.0 ) ;
14
+ assert ! ( snowflake_epoch_offset >= now && now < snowflake_epoch_offset + 2.0 )
15
+ }
16
+
17
+ #[ wasm_bindgen_test]
18
+ fn create_snowflake_with_empty_opts ( ) {
19
+ let opts = SnowflakeConfig {
20
+ custom_epoch : None ,
21
+ instance_id : None ,
22
+ } ;
23
+ let now = js_sys:: Date :: now ( ) ;
24
+
25
+ let uid = Snowflake :: new ( Some ( opts. into ( ) ) ) . unwrap ( ) ;
26
+
27
+ let instance_id = uid. instance_id ( ) ;
28
+ let snowflake_epoch_offset = uid. custom_epoch ( ) ;
29
+
30
+ assert ! ( instance_id > 0.0 && instance_id < 4095.0 ) ;
31
+ assert ! ( snowflake_epoch_offset >= now && now < snowflake_epoch_offset + 2.0 )
32
+ }
33
+
34
+ #[ wasm_bindgen_test]
35
+ fn create_snowflake_with_custom_epoch_only ( ) {
36
+ let custom_epoch: u64 = 1546300800000 ;
37
+
38
+ let opts = SnowflakeConfig {
39
+ custom_epoch : Some ( custom_epoch) ,
40
+ instance_id : None ,
41
+ } ;
42
+
43
+ let uid = Snowflake :: new ( Some ( opts. into ( ) ) ) . unwrap ( ) ;
44
+
45
+ let instance_id = uid. instance_id ( ) ;
46
+ let snowflake_epoch_offset = uid. custom_epoch ( ) as u64 ;
47
+
48
+ assert ! ( instance_id > 0.0 && instance_id < 4095.0 ) ;
49
+ assert_eq ! ( snowflake_epoch_offset, custom_epoch) ;
50
+ }
51
+
52
+ #[ wasm_bindgen_test]
53
+ fn create_snowflake_with_custom_instance_id_only ( ) {
54
+ let custom_instance_id: u16 = 1234 ;
55
+ let now = js_sys:: Date :: now ( ) ;
56
+
57
+ let opts = SnowflakeConfig {
58
+ custom_epoch : None ,
59
+ instance_id : Some ( custom_instance_id) ,
60
+ } ;
61
+
62
+ let uid = Snowflake :: new ( Some ( opts. into ( ) ) ) . unwrap ( ) ;
63
+
64
+ let instance_id = uid. instance_id ( ) ;
65
+ let snowflake_epoch_offset = uid. custom_epoch ( ) ;
66
+
67
+ assert_eq ! ( instance_id as u16 , custom_instance_id) ;
68
+ assert ! ( snowflake_epoch_offset >= now && now < snowflake_epoch_offset + 2.0 )
69
+ }
70
+
71
+ #[ wasm_bindgen_test]
72
+ fn create_snowflake_with_custom_instance_id_and_custom_epoch ( ) {
73
+ let custom_instance_id: u16 = 1234 ;
74
+ let custom_epoch: u64 = 1546300800000 ;
75
+
76
+ let opts = SnowflakeConfig {
77
+ custom_epoch : Some ( custom_epoch) ,
78
+ instance_id : Some ( custom_instance_id) ,
79
+ } ;
80
+
81
+ let uid = Snowflake :: new ( Some ( opts. into ( ) ) ) . unwrap ( ) ;
82
+
83
+ let instance_id = uid. instance_id ( ) ;
84
+ let snowflake_epoch_offset = uid. custom_epoch ( ) as u64 ;
85
+
86
+ assert_eq ! ( instance_id as u16 , custom_instance_id) ;
87
+ assert_eq ! ( custom_epoch, snowflake_epoch_offset) ;
88
+ }
89
+
90
+ #[ wasm_bindgen_test]
91
+ fn create_snowflake_with_invalid_instance_ids ( ) {
92
+ let custom_instance_ids = [ 10000 , 4096u16 ] ;
93
+
94
+ for custom_instance_id in custom_instance_ids. iter ( ) {
95
+ let opts = SnowflakeConfig {
96
+ custom_epoch : None ,
97
+ instance_id : Some ( * custom_instance_id) ,
98
+ } ;
99
+
100
+ let uid = Snowflake :: new ( Some ( opts. into ( ) ) ) ;
101
+
102
+ assert ! ( uid. is_err( ) ) ;
103
+ }
104
+ }
105
+
106
+ #[ wasm_bindgen_test]
107
+ fn verify_timestamp_of_creation ( ) {
108
+ // const ERROR_MARGIN: f64 = 2.0;
109
+
110
+ let mut uid = Snowflake :: new ( None ) . unwrap ( ) ;
111
+
112
+ let before = js_sys:: Date :: now ( ) ;
113
+ let id = uid. get_unique_id ( ) ;
114
+ let after = js_sys:: Date :: now ( ) ;
115
+
116
+ let ts = Snowflake :: timestamp_from_id ( id, uid. custom_epoch ( ) ) ;
117
+
118
+ assert ! ( ts >= before && ts <= after) ;
119
+ }
120
+
121
+ #[ wasm_bindgen_test]
122
+ fn verify_instance_id ( ) {
123
+ let mut uid = Snowflake :: new ( None ) . unwrap ( ) ;
124
+
125
+ let id = uid. get_unique_id ( ) ;
126
+ let ciid = uid. instance_id ( ) as i32 ;
127
+ let iid = Snowflake :: instance_id_from_id ( id) ;
128
+
129
+ assert_eq ! ( iid, ciid) ;
130
+ }
131
+
132
+ #[ wasm_bindgen_test]
133
+ fn generate_id_from_timestamp ( ) {
134
+ let uid = Snowflake :: new ( None ) . unwrap ( ) ;
135
+
136
+ let ts = js_sys:: Date :: now ( ) ;
137
+ let id = uid. id_from_timestamp ( ts) ;
138
+ let extracted_ts = Snowflake :: timestamp_from_id ( id, uid. custom_epoch ( ) ) ;
139
+
140
+ assert ! ( ( ts - extracted_ts) . abs( ) < f64 :: EPSILON ) ;
141
+ }
142
+
143
+ #[ wasm_bindgen_test]
144
+ fn integrity_test ( ) {
145
+ const SECOND : f64 = 1e3 ;
146
+ const RUN_FOR_SECONDS : f64 = 1.0 ;
147
+
148
+ let mut uid = Snowflake :: new ( None ) . unwrap ( ) ;
149
+ let mut ids = Vec :: new ( ) ;
150
+
151
+ let before = js_sys:: Date :: now ( ) ;
152
+ while js_sys:: Date :: now ( ) - before < RUN_FOR_SECONDS * SECOND {
153
+ ids. push ( uid. get_unique_id ( ) ) ;
154
+ }
155
+
156
+ let mut copy = ids. clone ( ) ;
157
+ copy. sort_unstable ( ) ;
158
+ copy. dedup ( ) ;
159
+
160
+ assert_eq ! ( copy. len( ) , ids. len( ) ) ;
161
+ }
0 commit comments