@@ -33,13 +33,17 @@ where
3333 /// if change state fails.
3434 pub ( crate ) fn ready ( & self ) -> std:: io:: Result < ( ) > {
3535 let current = self . state ( ) ;
36- if let CoroutineState :: Suspend ( _, timestamp) = current {
37- if timestamp <= now ( ) {
38- let new_state = CoroutineState :: Ready ;
39- let old_state = self . change_state ( new_state) ;
40- self . on_ready ( self , old_state) ;
41- return Ok ( ( ) ) ;
36+ match current {
37+ CoroutineState :: Ready => return Ok ( ( ) ) ,
38+ CoroutineState :: Suspend ( _, timestamp) => {
39+ if timestamp <= now ( ) {
40+ let new_state = CoroutineState :: Ready ;
41+ let old_state = self . change_state ( new_state) ;
42+ self . on_ready ( self , old_state) ;
43+ return Ok ( ( ) ) ;
44+ }
4245 }
46+ _ => { }
4347 }
4448 Err ( Error :: new (
4549 ErrorKind :: Other ,
@@ -198,3 +202,84 @@ where
198202 ) )
199203 }
200204}
205+
206+ #[ cfg( test) ]
207+ mod tests {
208+ use super :: * ;
209+ use crate :: coroutine:: suspender:: Suspender ;
210+
211+ #[ test]
212+ fn test_ready ( ) -> std:: io:: Result < ( ) > {
213+ let co = co ! ( |_: & Suspender <( ) , ( ) >, ( ) | { } ) ?;
214+ assert_eq ! ( CoroutineState :: Ready , co. state( ) ) ;
215+ co. ready ( ) ?;
216+ assert_eq ! ( CoroutineState :: Ready , co. state( ) ) ;
217+ co. running ( ) ?;
218+ co. suspend ( ( ) , u64:: MAX ) ?;
219+ assert_eq ! ( CoroutineState :: Suspend ( ( ) , u64 :: MAX ) , co. state( ) ) ;
220+ assert ! ( co. ready( ) . is_err( ) ) ;
221+ Ok ( ( ) )
222+ }
223+
224+ #[ test]
225+ fn test_running ( ) -> std:: io:: Result < ( ) > {
226+ let co = co ! ( |_: & Suspender <( ) , ( ) >, ( ) | { } ) ?;
227+ assert_eq ! ( CoroutineState :: Ready , co. state( ) ) ;
228+ co. running ( ) ?;
229+ co. running ( ) ?;
230+ co. complete ( ( ) ) ?;
231+ assert_eq ! ( CoroutineState :: Complete ( ( ) ) , co. state( ) ) ;
232+ assert ! ( co. running( ) . is_err( ) ) ;
233+ Ok ( ( ) )
234+ }
235+
236+ #[ test]
237+ fn test_suspend ( ) -> std:: io:: Result < ( ) > {
238+ let mut co = co ! ( |_: & Suspender <( ) , ( ) >, ( ) | { } ) ?;
239+ assert_eq ! ( CoroutineState :: Ready , co. state( ) ) ;
240+ co. running ( ) ?;
241+ co. suspend ( ( ) , u64:: MAX ) ?;
242+ assert_eq ! ( CoroutineState :: Suspend ( ( ) , u64 :: MAX ) , co. state( ) ) ;
243+ assert ! ( co. resume( ) . is_err( ) ) ;
244+ assert ! ( co. suspend( ( ) , u64 :: MAX ) . is_err( ) ) ;
245+ Ok ( ( ) )
246+ }
247+
248+ #[ test]
249+ fn test_syscall ( ) -> std:: io:: Result < ( ) > {
250+ let co = co ! ( |_: & Suspender <( ) , ( ) >, ( ) | { } ) ?;
251+ assert_eq ! ( CoroutineState :: Ready , co. state( ) ) ;
252+ co. running ( ) ?;
253+ co. syscall ( ( ) , Syscall :: nanosleep, SyscallState :: Executing ) ?;
254+ assert_eq ! (
255+ CoroutineState :: SystemCall ( ( ) , Syscall :: nanosleep, SyscallState :: Executing ) ,
256+ co. state( )
257+ ) ;
258+ assert ! ( co
259+ . syscall( ( ) , Syscall :: sleep, SyscallState :: Executing )
260+ . is_err( ) ) ;
261+ Ok ( ( ) )
262+ }
263+
264+ #[ test]
265+ fn test_complete ( ) -> std:: io:: Result < ( ) > {
266+ let co = co ! ( |_: & Suspender <( ) , ( ) >, ( ) | { } ) ?;
267+ assert_eq ! ( CoroutineState :: Ready , co. state( ) ) ;
268+ co. running ( ) ?;
269+ co. complete ( ( ) ) ?;
270+ assert_eq ! ( CoroutineState :: Complete ( ( ) ) , co. state( ) ) ;
271+ assert ! ( co. complete( ( ) ) . is_err( ) ) ;
272+ Ok ( ( ) )
273+ }
274+
275+ #[ test]
276+ fn test_error ( ) -> std:: io:: Result < ( ) > {
277+ let co = co ! ( |_: & Suspender <( ) , ( ) >, ( ) | { } ) ?;
278+ assert_eq ! ( CoroutineState :: Ready , co. state( ) ) ;
279+ co. running ( ) ?;
280+ co. error ( "test error, ignore it" ) ?;
281+ assert_eq ! ( CoroutineState :: Error ( "test error, ignore it" ) , co. state( ) ) ;
282+ assert ! ( co. error( "abc" ) . is_err( ) ) ;
283+ Ok ( ( ) )
284+ }
285+ }
0 commit comments