@@ -99,9 +99,14 @@ impl FeatureSnapshot {
99
99
}
100
100
}
101
101
102
+ fn normalized_hash ( data : & str ) -> u32 {
103
+ let hash = murmur3_32 ( & mut Cursor :: new ( data) , 0 ) . expect ( "Cannot hash the value." ) ;
104
+ ( f64:: from ( hash) / f64:: from ( u32:: MAX ) * 100.0 ) as u32
105
+ }
106
+
102
107
fn should_rollout ( rollout_percentage : u32 , entity : & impl Entity , feature_id : & str ) -> bool {
103
108
let tag = format ! ( "{}:{}" , entity. get_id( ) , feature_id) ;
104
- rollout_percentage == 100 || random_value ( & tag) < rollout_percentage
109
+ rollout_percentage == 100 || Self :: normalized_hash ( & tag) < rollout_percentage
105
110
}
106
111
107
112
fn use_rollout_percentage_to_get_value_from_feature_directly (
@@ -139,15 +144,6 @@ impl Feature for FeatureSnapshot {
139
144
}
140
145
}
141
146
142
- pub ( crate ) fn random_value ( v : & str ) -> u32 {
143
- let max_hash = u32:: MAX ;
144
- ( f64:: from ( hash ( v) ) / f64:: from ( max_hash) * 100.0 ) as u32
145
- }
146
-
147
- fn hash ( v : & str ) -> u32 {
148
- murmur3_32 ( & mut Cursor :: new ( v) , 0 ) . expect ( "Cannot hash the value." )
149
- }
150
-
151
147
#[ cfg( test) ]
152
148
pub mod tests {
153
149
@@ -213,7 +209,9 @@ pub mod tests {
213
209
attributes : entity_attributes. clone ( ) ,
214
210
} ;
215
211
assert_eq ! (
216
- random_value( format!( "{}:{}" , entity. id, feature. feature_id) . as_str( ) ) ,
212
+ FeatureSnapshot :: normalized_hash(
213
+ format!( "{}:{}" , entity. id, feature. feature_id) . as_str( )
214
+ ) ,
217
215
68
218
216
) ;
219
217
let value = feature. get_value ( & entity) . unwrap ( ) ;
@@ -225,7 +223,9 @@ pub mod tests {
225
223
attributes : entity_attributes,
226
224
} ;
227
225
assert_eq ! (
228
- random_value( format!( "{}:{}" , entity. id, feature. feature_id) . as_str( ) ) ,
226
+ FeatureSnapshot :: normalized_hash(
227
+ format!( "{}:{}" , entity. id, feature. feature_id) . as_str( )
228
+ ) ,
229
229
29
230
230
) ;
231
231
let value = feature. get_value ( & entity) . unwrap ( ) ;
@@ -407,4 +407,12 @@ pub mod tests {
407
407
let value = feature. get_value ( & entity) . unwrap ( ) ;
408
408
assert ! ( matches!( value, Value :: Int64 ( ref v) if v == & 2 ) ) ;
409
409
}
410
+
411
+ /// This test ensures that the rust client is using the same hashing algorithm as to other clients.
412
+ /// See same test for Node client:
413
+ /// https://github.com/IBM/appconfiguration-node-sdk/blob/master/test/unit/configurations/internal/utils.test.js#L25
414
+ #[ test]
415
+ fn test_normalized_hash ( ) {
416
+ assert_eq ! ( FeatureSnapshot :: normalized_hash( "entityId:featureId" ) , 41 )
417
+ }
410
418
}
0 commit comments