@@ -65,21 +65,19 @@ fn test_posix_gettimeofday() {
65
65
}
66
66
67
67
fn 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 } ;
71
70
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) } ;
73
72
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 ) ) ;
75
74
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 } ;
79
77
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) } ;
81
79
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 ) ) ;
83
81
}
84
82
85
83
/// Helper function to get the current time for testing relative sleeps
@@ -91,70 +89,55 @@ fn timespec_now(clock: libc::clockid_t) -> libc::timespec {
91
89
}
92
90
93
91
fn 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 } ;
96
94
let remainder = ptr:: null_mut :: < libc:: timespec > ( ) ;
97
95
let error = unsafe {
98
96
// 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)
105
98
} ;
106
99
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 ) ) ;
108
101
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
+ } ;
112
108
let remainder = ptr:: null_mut :: < libc:: timespec > ( ) ;
113
109
let error = unsafe {
114
- // this will not sleep since unix time zero is in the past
115
110
libc:: clock_nanosleep (
116
111
libc:: CLOCK_MONOTONIC ,
117
112
libc:: TIMER_ABSTIME ,
118
- & one_second_from_now ,
113
+ & hunderd_millis_after_start ,
119
114
remainder,
120
115
)
121
116
} ;
122
117
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 ) ) ;
124
119
}
125
120
126
121
fn test_clock_nanosleep_relative ( ) {
127
122
const NO_FLAGS : i32 = 0 ;
128
123
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 } ;
131
126
let remainder = ptr:: null_mut :: < libc:: timespec > ( ) ;
132
127
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)
140
129
} ;
141
130
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 ) ) ;
143
132
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 } ;
146
135
let remainder = ptr:: null_mut :: < libc:: timespec > ( ) ;
147
136
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)
155
138
} ;
156
139
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 ) ) ;
158
141
}
159
142
160
143
/// Helper function to create an empty tm struct.
0 commit comments