@@ -224,3 +224,126 @@ struct PriorityPair {
224224 keep : SamplingPriority ,
225225 reject : SamplingPriority ,
226226}
227+
228+ #[ cfg( test) ]
229+ mod tests {
230+ use super :: * ;
231+
232+ // --- SamplingPriority ---
233+
234+ #[ test]
235+ fn test_priority_into_i8 ( ) {
236+ assert_eq ! ( priority:: AUTO_KEEP . into_i8( ) , 1 ) ;
237+ assert_eq ! ( priority:: AUTO_REJECT . into_i8( ) , 0 ) ;
238+ assert_eq ! ( priority:: USER_KEEP . into_i8( ) , 2 ) ;
239+ assert_eq ! ( priority:: USER_REJECT . into_i8( ) , -1 ) ;
240+ }
241+
242+ #[ test]
243+ fn test_priority_is_keep ( ) {
244+ assert ! ( priority:: AUTO_KEEP . is_keep( ) ) ;
245+ assert ! ( priority:: USER_KEEP . is_keep( ) ) ;
246+ assert ! ( !priority:: AUTO_REJECT . is_keep( ) ) ;
247+ assert ! ( !priority:: USER_REJECT . is_keep( ) ) ;
248+ }
249+
250+ #[ test]
251+ fn test_priority_display ( ) {
252+ assert_eq ! ( priority:: AUTO_KEEP . to_string( ) , "1" ) ;
253+ assert_eq ! ( priority:: AUTO_REJECT . to_string( ) , "0" ) ;
254+ assert_eq ! ( priority:: USER_KEEP . to_string( ) , "2" ) ;
255+ assert_eq ! ( priority:: USER_REJECT . to_string( ) , "-1" ) ;
256+ }
257+
258+ #[ test]
259+ fn test_priority_from_str ( ) {
260+ assert_eq ! ( "1" . parse:: <SamplingPriority >( ) . unwrap( ) , priority:: AUTO_KEEP ) ;
261+ assert_eq ! ( "0" . parse:: <SamplingPriority >( ) . unwrap( ) , priority:: AUTO_REJECT ) ;
262+ assert_eq ! ( "2" . parse:: <SamplingPriority >( ) . unwrap( ) , priority:: USER_KEEP ) ;
263+ assert_eq ! ( "-1" . parse:: <SamplingPriority >( ) . unwrap( ) , priority:: USER_REJECT ) ;
264+ assert ! ( "not_a_number" . parse:: <SamplingPriority >( ) . is_err( ) ) ;
265+ assert ! ( "999" . parse:: <SamplingPriority >( ) . is_err( ) ) ; // overflows i8
266+ }
267+
268+ // --- SamplingMechanism ---
269+
270+ #[ test]
271+ fn test_mechanism_into_u8 ( ) {
272+ assert_eq ! ( mechanism:: DEFAULT . into_u8( ) , 0 ) ;
273+ assert_eq ! ( mechanism:: AGENT_RATE_BY_SERVICE . into_u8( ) , 1 ) ;
274+ assert_eq ! ( mechanism:: LOCAL_USER_TRACE_SAMPLING_RULE . into_u8( ) , 3 ) ;
275+ assert_eq ! ( mechanism:: REMOTE_USER_TRACE_SAMPLING_RULE . into_u8( ) , 11 ) ;
276+ assert_eq ! ( mechanism:: REMOTE_DYNAMIC_TRACE_SAMPLING_RULE . into_u8( ) , 12 ) ;
277+ }
278+
279+ #[ test]
280+ fn test_mechanism_to_priority_auto_pair ( ) {
281+ // DEFAULT and AGENT_RATE_BY_SERVICE use AUTO priority
282+ assert_eq ! ( mechanism:: DEFAULT . to_priority( true ) , priority:: AUTO_KEEP ) ;
283+ assert_eq ! ( mechanism:: DEFAULT . to_priority( false ) , priority:: AUTO_REJECT ) ;
284+ assert_eq ! ( mechanism:: AGENT_RATE_BY_SERVICE . to_priority( true ) , priority:: AUTO_KEEP ) ;
285+ assert_eq ! ( mechanism:: AGENT_RATE_BY_SERVICE . to_priority( false ) , priority:: AUTO_REJECT ) ;
286+ assert_eq ! ( mechanism:: APPSEC . to_priority( true ) , priority:: AUTO_KEEP ) ;
287+ assert_eq ! ( mechanism:: APPSEC . to_priority( false ) , priority:: AUTO_REJECT ) ;
288+ }
289+
290+ #[ test]
291+ fn test_mechanism_to_priority_user_pair ( ) {
292+ // Rule-based mechanisms use USER priority
293+ assert_eq ! ( mechanism:: LOCAL_USER_TRACE_SAMPLING_RULE . to_priority( true ) , priority:: USER_KEEP ) ;
294+ assert_eq ! ( mechanism:: LOCAL_USER_TRACE_SAMPLING_RULE . to_priority( false ) , priority:: USER_REJECT ) ;
295+ assert_eq ! ( mechanism:: REMOTE_USER_TRACE_SAMPLING_RULE . to_priority( true ) , priority:: USER_KEEP ) ;
296+ assert_eq ! ( mechanism:: REMOTE_DYNAMIC_TRACE_SAMPLING_RULE . to_priority( true ) , priority:: USER_KEEP ) ;
297+ assert_eq ! ( mechanism:: MANUAL . to_priority( true ) , priority:: USER_KEEP ) ;
298+ assert_eq ! ( mechanism:: SPAN_SAMPLING_RULE . to_priority( false ) , priority:: USER_REJECT ) ;
299+ assert_eq ! ( mechanism:: DATA_JOBS_MONITORING . to_priority( true ) , priority:: USER_KEEP ) ;
300+ }
301+
302+ #[ test]
303+ fn test_mechanism_to_priority_unknown_falls_back_to_auto ( ) {
304+ let unknown = SamplingMechanism :: from_u8 ( 99 ) ;
305+ assert_eq ! ( unknown. to_priority( true ) , priority:: AUTO_KEEP ) ;
306+ assert_eq ! ( unknown. to_priority( false ) , priority:: AUTO_REJECT ) ;
307+ }
308+
309+ #[ test]
310+ fn test_mechanism_to_cow ( ) {
311+ assert_eq ! ( mechanism:: DEFAULT . to_cow( ) , "-0" ) ;
312+ assert_eq ! ( mechanism:: AGENT_RATE_BY_SERVICE . to_cow( ) , "-1" ) ;
313+ assert_eq ! ( mechanism:: REMOTE_RATE . to_cow( ) , "-2" ) ;
314+ assert_eq ! ( mechanism:: LOCAL_USER_TRACE_SAMPLING_RULE . to_cow( ) , "-3" ) ;
315+ assert_eq ! ( mechanism:: MANUAL . to_cow( ) , "-4" ) ;
316+ assert_eq ! ( mechanism:: APPSEC . to_cow( ) , "-5" ) ;
317+ assert_eq ! ( mechanism:: REMOTE_RATE_USER . to_cow( ) , "-6" ) ;
318+ assert_eq ! ( mechanism:: REMOTE_RATE_DATADOG . to_cow( ) , "-7" ) ;
319+ assert_eq ! ( mechanism:: SPAN_SAMPLING_RULE . to_cow( ) , "-8" ) ;
320+ assert_eq ! ( mechanism:: OTLP_INGEST_PROBABILISTIC_SAMPLING . to_cow( ) , "-9" ) ;
321+ assert_eq ! ( mechanism:: DATA_JOBS_MONITORING . to_cow( ) , "-10" ) ;
322+ assert_eq ! ( mechanism:: REMOTE_USER_TRACE_SAMPLING_RULE . to_cow( ) , "-11" ) ;
323+ assert_eq ! ( mechanism:: REMOTE_DYNAMIC_TRACE_SAMPLING_RULE . to_cow( ) , "-12" ) ;
324+ // Unknown mechanism falls back to Display
325+ assert_eq ! ( SamplingMechanism :: from_u8( 99 ) . to_cow( ) , "-99" ) ;
326+ }
327+
328+ #[ test]
329+ fn test_mechanism_display ( ) {
330+ assert_eq ! ( mechanism:: DEFAULT . to_string( ) , "-0" ) ;
331+ assert_eq ! ( mechanism:: LOCAL_USER_TRACE_SAMPLING_RULE . to_string( ) , "-3" ) ;
332+ assert_eq ! ( mechanism:: REMOTE_DYNAMIC_TRACE_SAMPLING_RULE . to_string( ) , "-12" ) ;
333+ }
334+
335+ #[ test]
336+ fn test_mechanism_from_str ( ) {
337+ assert_eq ! ( "-0" . parse:: <SamplingMechanism >( ) . unwrap( ) , mechanism:: DEFAULT ) ;
338+ assert_eq ! ( "-1" . parse:: <SamplingMechanism >( ) . unwrap( ) , mechanism:: AGENT_RATE_BY_SERVICE ) ;
339+ assert_eq ! ( "-3" . parse:: <SamplingMechanism >( ) . unwrap( ) , mechanism:: LOCAL_USER_TRACE_SAMPLING_RULE ) ;
340+ assert_eq ! ( "-12" . parse:: <SamplingMechanism >( ) . unwrap( ) , mechanism:: REMOTE_DYNAMIC_TRACE_SAMPLING_RULE ) ;
341+ }
342+
343+ #[ test]
344+ fn test_mechanism_from_str_errors ( ) {
345+ assert ! ( "not_a_number" . parse:: <SamplingMechanism >( ) . is_err( ) ) ;
346+ assert ! ( "1" . parse:: <SamplingMechanism >( ) . is_err( ) ) ; // positive not allowed
347+ assert ! ( "-99999" . parse:: <SamplingMechanism >( ) . is_err( ) ) ; // overflows u8
348+ }
349+ }
0 commit comments