@@ -65,21 +65,19 @@ fn test_posix_gettimeofday() {
6565}
6666
6767fn test_nanosleep ( ) {
68- // sleep zero seconds
69- let start_zero_second_sleep = Instant :: now ( ) ;
70- let timespec = libc:: timespec { tv_sec : 0 , tv_nsec : 0 } ;
68+ let start_test_sleep = Instant :: now ( ) ;
69+ let duration_zero = libc:: timespec { tv_sec : 0 , tv_nsec : 0 } ;
7170 let remainder = ptr:: null_mut :: < libc:: timespec > ( ) ;
72- let is_error = unsafe { libc:: nanosleep ( & timespec , remainder) } ;
71+ let is_error = unsafe { libc:: nanosleep ( & duration_zero , remainder) } ;
7372 assert_eq ! ( is_error, 0 ) ;
74- assert ! ( start_zero_second_sleep . elapsed( ) < Duration :: from_millis( 100 ) ) ;
73+ assert ! ( start_test_sleep . elapsed( ) < Duration :: from_millis( 10 ) ) ;
7574
76- // sleep one second
77- let start_one_second_sleep = Instant :: now ( ) ;
78- let timespec = libc:: timespec { tv_sec : 1 , tv_nsec : 0 } ;
75+ let start_test_sleep = Instant :: now ( ) ;
76+ let duration_100_millis = libc:: timespec { tv_sec : 0 , tv_nsec : 1_000_000_000 / 10 } ;
7977 let remainder = ptr:: null_mut :: < libc:: timespec > ( ) ;
80- let is_error = unsafe { libc:: nanosleep ( & timespec , remainder) } ;
78+ let is_error = unsafe { libc:: nanosleep ( & duration_100_millis , remainder) } ;
8179 assert_eq ! ( is_error, 0 ) ;
82- assert ! ( start_one_second_sleep . elapsed( ) > Duration :: from_secs ( 1 ) ) ;
80+ assert ! ( start_test_sleep . elapsed( ) > Duration :: from_millis ( 100 ) ) ;
8381}
8482
8583/// Helper function to get the current time for testing relative sleeps
@@ -91,70 +89,55 @@ fn timespec_now(clock: libc::clockid_t) -> libc::timespec {
9189}
9290
9391fn test_clock_nanosleep_absolute ( ) {
94- let start_zero_second_sleep = Instant :: now ( ) ;
95- let unix_time_zero = libc:: timespec { tv_sec : 0 , tv_nsec : 0 } ;
92+ let start_test_sleep = Instant :: now ( ) ;
93+ let before_start = libc:: timespec { tv_sec : 0 , tv_nsec : 0 } ;
9694 let remainder = ptr:: null_mut :: < libc:: timespec > ( ) ;
9795 let error = unsafe {
9896 // this will not sleep since unix time zero is in the past
99- libc:: clock_nanosleep (
100- libc:: CLOCK_MONOTONIC ,
101- libc:: TIMER_ABSTIME ,
102- & unix_time_zero,
103- remainder,
104- )
97+ libc:: clock_nanosleep ( libc:: CLOCK_MONOTONIC , libc:: TIMER_ABSTIME , & before_start, remainder)
10598 } ;
10699 assert_eq ! ( error, 0 ) ;
107- assert ! ( start_zero_second_sleep . elapsed( ) < Duration :: from_millis( 100 ) ) ;
100+ assert ! ( start_test_sleep . elapsed( ) < Duration :: from_millis( 10 ) ) ;
108101
109- let start_one_second_sleep = Instant :: now ( ) ;
110- let mut one_second_from_now = timespec_now ( libc:: CLOCK_MONOTONIC ) ;
111- one_second_from_now. tv_sec += 1 ;
102+ let start_test_sleep = Instant :: now ( ) ;
103+ let hunderd_millis_after_start = {
104+ let mut ts = timespec_now ( libc:: CLOCK_MONOTONIC ) ;
105+ ts. tv_nsec += 1_000_000_000 / 10 ;
106+ ts
107+ } ;
112108 let remainder = ptr:: null_mut :: < libc:: timespec > ( ) ;
113109 let error = unsafe {
114- // this will not sleep since unix time zero is in the past
115110 libc:: clock_nanosleep (
116111 libc:: CLOCK_MONOTONIC ,
117112 libc:: TIMER_ABSTIME ,
118- & one_second_from_now ,
113+ & hunderd_millis_after_start ,
119114 remainder,
120115 )
121116 } ;
122117 assert_eq ! ( error, 0 ) ;
123- assert ! ( start_one_second_sleep . elapsed( ) > Duration :: from_secs ( 1 ) ) ;
118+ assert ! ( start_test_sleep . elapsed( ) > Duration :: from_millis ( 100 ) ) ;
124119}
125120
126121fn test_clock_nanosleep_relative ( ) {
127122 const NO_FLAGS : i32 = 0 ;
128123
129- let start_zero_second_sleep = Instant :: now ( ) ;
130- let zero_seconds = libc:: timespec { tv_sec : 0 , tv_nsec : 0 } ;
124+ let start_test_sleep = Instant :: now ( ) ;
125+ let duration_zero = libc:: timespec { tv_sec : 0 , tv_nsec : 0 } ;
131126 let remainder = ptr:: null_mut :: < libc:: timespec > ( ) ;
132127 let error = unsafe {
133- // this will not sleep since unix time zero is in the past
134- libc:: clock_nanosleep (
135- libc:: CLOCK_MONOTONIC ,
136- NO_FLAGS ,
137- & zero_seconds,
138- remainder,
139- )
128+ libc:: clock_nanosleep ( libc:: CLOCK_MONOTONIC , NO_FLAGS , & duration_zero, remainder)
140129 } ;
141130 assert_eq ! ( error, 0 ) ;
142- assert ! ( start_zero_second_sleep . elapsed( ) < Duration :: from_millis( 100 ) ) ;
131+ assert ! ( start_test_sleep . elapsed( ) < Duration :: from_millis( 10 ) ) ;
143132
144- let start_one_second_sleep = Instant :: now ( ) ;
145- let one_second = libc:: timespec { tv_sec : 1 , tv_nsec : 0 } ;
133+ let start_test_sleep = Instant :: now ( ) ;
134+ let duration_100_millis = libc:: timespec { tv_sec : 0 , tv_nsec : 1_000_000_000 / 10 } ;
146135 let remainder = ptr:: null_mut :: < libc:: timespec > ( ) ;
147136 let error = unsafe {
148- // this will not sleep since unix time zero is in the past
149- libc:: clock_nanosleep (
150- libc:: CLOCK_MONOTONIC ,
151- NO_FLAGS ,
152- & one_second,
153- remainder,
154- )
137+ libc:: clock_nanosleep ( libc:: CLOCK_MONOTONIC , NO_FLAGS , & duration_100_millis, remainder)
155138 } ;
156139 assert_eq ! ( error, 0 ) ;
157- assert ! ( start_one_second_sleep . elapsed( ) > Duration :: from_secs ( 1 ) ) ;
140+ assert ! ( start_test_sleep . elapsed( ) > Duration :: from_millis ( 100 ) ) ;
158141}
159142
160143/// Helper function to create an empty tm struct.
0 commit comments