1+ use std:: time:: UNIX_EPOCH ;
2+
13use assert2:: check;
2- use metrique:: { OnParentDrop , Slot } ;
4+ use metrique:: {
5+ OnParentDrop , Slot ,
6+ timers:: { EpochMicros , Timestamp , TimestampOnClose } ,
7+ } ;
38use metrique_macro:: metrics;
9+ use metrique_timesource:: { TimeSource , set_time_source} ;
410use metrique_writer:: sink:: DevNullSink ;
511
612#[ metrics( rename_all = "PascalCase" ) ]
@@ -13,10 +19,13 @@ struct Metrics {
1319 c : Slot < AnotherNested > ,
1420}
1521
16- #[ metrics( subfield ) ]
17- #[ derive( Default , Clone , Debug ) ]
22+ #[ metrics( subfield_owned ) ]
23+ #[ derive( Default , Debug ) ]
1824struct Nested {
1925 inner_value : usize ,
26+ timer : TimestampOnClose ,
27+ #[ metrics( format = EpochMicros ) ]
28+ formatted : Timestamp ,
2029}
2130
2231#[ metrics( subfield) ]
@@ -25,11 +34,16 @@ struct AnotherNested {
2534 another_value : usize ,
2635}
2736
28- #[ tokio:: test]
37+ #[ tokio:: test( start_paused = true ) ]
2938async fn debug_slot_closed ( ) {
39+ let _mock_time = set_time_source ( TimeSource :: tokio ( UNIX_EPOCH ) ) ;
3040 let mut metrics = Metrics {
3141 a : 42 ,
32- b : Slot :: new ( Nested { inner_value : 123 } ) ,
42+ b : Slot :: new ( Nested {
43+ inner_value : 123 ,
44+ timer : TimestampOnClose :: default ( ) ,
45+ formatted : Timestamp :: default ( ) ,
46+ } ) ,
3347 c : Slot :: new ( AnotherNested { another_value : 456 } ) ,
3448 } ;
3549
@@ -42,15 +56,20 @@ async fn debug_slot_closed() {
4256 metrics. b . wait_for_data ( ) . await ;
4357 check ! (
4458 format!( "{:?}" , metrics. b)
45- == "Slot { open: true, has_data: true, data: Some(NestedEntry { inner_value: 1000 }) }"
59+ == "Slot { open: true, has_data: true, data: Some(NestedEntry { inner_value: 1000, timer: TimestampValue { duration_since_epoch: 0ns }, formatted: TimestampValue { duration_since_epoch: 0ns } }) }"
4660 ) ;
4761}
4862
4963#[ test]
5064fn debug_slot_open ( ) {
65+ let _mock_time = set_time_source ( TimeSource :: tokio ( UNIX_EPOCH ) ) ;
5166 let mut metrics = Metrics {
5267 a : 99 ,
53- b : Slot :: new ( Nested { inner_value : 200 } ) ,
68+ b : Slot :: new ( Nested {
69+ inner_value : 200 ,
70+ timer : TimestampOnClose :: default ( ) ,
71+ formatted : Timestamp :: default ( ) ,
72+ } ) ,
5473 c : Slot :: new ( AnotherNested { another_value : 300 } ) ,
5574 } ;
5675
@@ -71,23 +90,28 @@ fn debug_slot_open() {
7190 ) ;
7291}
7392
74- #[ test]
75- fn debug_slot_guard ( ) {
93+ #[ tokio:: test( start_paused = true ) ]
94+ async fn debug_slot_guard ( ) {
95+ let _mock_time = set_time_source ( TimeSource :: tokio ( UNIX_EPOCH ) ) ;
7696 let mut metrics = Metrics {
7797 a : 1 ,
78- b : Slot :: new ( Nested { inner_value : 777 } ) ,
98+ b : Slot :: new ( Nested {
99+ inner_value : 777 ,
100+ timer : TimestampOnClose :: default ( ) ,
101+ formatted : Timestamp :: default ( ) ,
102+ } ) ,
79103 c : Slot :: new ( AnotherNested { another_value : 888 } ) ,
80104 } ;
81105
82106 let guard_b = metrics. b . open ( OnParentDrop :: Discard ) . unwrap ( ) ;
83107
84108 check ! (
85109 format!( "{:?}" , guard_b)
86- == "SlotGuard { value: Nested { inner_value: 777 }, parent_is_closed: false, parent_drop_mode: Discard }"
110+ == "SlotGuard { value: Nested { inner_value: 777, timer: TimestampOnClose { time_source: TimeSource::Custom(...) }, formatted: Timestamp { time: SystemTime { tv_sec: 0, tv_nsec: 0 } } }, parent_is_closed: false, parent_drop_mode: Discard }"
87111 ) ;
88112 drop ( metrics) ;
89113 check ! (
90114 format!( "{:?}" , guard_b)
91- == "SlotGuard { value: Nested { inner_value: 777 }, parent_is_closed: true, parent_drop_mode: Discard }"
115+ == "SlotGuard { value: Nested { inner_value: 777, timer: TimestampOnClose { time_source: TimeSource::Custom(...) }, formatted: Timestamp { time: SystemTime { tv_sec: 0, tv_nsec: 0 } } }, parent_is_closed: true, parent_drop_mode: Discard }"
92116 ) ;
93117}
0 commit comments