@@ -560,30 +560,34 @@ mod tests {
560560 offchain:: sleep_until ( offchain:: timestamp ( ) . add ( Duration :: from_millis ( 200 ) ) ) ;
561561
562562 // the lock is still active, extend it successfully
563- assert_eq ! ( guard. extend_lock( ) . is_ok( ) , true ) ;
564- guard. forget ( ) ;
563+ assert_eq ! ( guard. extend_lock( ) . is_ok( ) , true ) ;
565564
566- // sleep_until < deadline
567- offchain:: sleep_until ( offchain:: timestamp ( ) . add ( Duration :: from_millis ( 200 ) ) ) ;
565+ // sleep_until < deadline
566+ offchain:: sleep_until ( offchain:: timestamp ( ) . add ( Duration :: from_millis ( 200 ) ) ) ;
568567
569- // the lock is still active, try_lock will fail
570- let mut lock = StorageLock :: < ' _ , Time > :: new ( b"lock_4" ) ;
571- let res = lock. try_lock ( ) ;
572- assert_eq ! ( res. is_ok( ) , false ) ;
568+ // the lock is still active, try_lock will fail
569+ let mut lock = StorageLock :: < ' _ , Time > :: with_deadline ( b"lock_4" , lock_expiration ) ;
570+ let res = lock. try_lock ( ) ;
571+ assert_eq ! ( res. is_ok( ) , false ) ;
573572
574- // sleep_until > deadline
575- offchain:: sleep_until ( offchain:: timestamp ( ) . add ( Duration :: from_millis ( 200 ) ) ) ;
573+ // sleep again untill sleep_until > deadline
574+ offchain:: sleep_until ( offchain:: timestamp ( ) . add ( Duration :: from_millis ( 200 ) ) ) ;
576575
577- // the lock has expired, try_lock will succeed
578- let mut lock = StorageLock :: < ' _ , Time > :: new ( b"lock_4" ) ;
579- let res = lock. try_lock ( ) ;
580- assert ! ( res. is_ok( ) ) ;
581- let guard = res. unwrap ( ) ;
582- guard. forget ( ) ;
583- } ) ;
576+ // the lock has expired, failed to extend it
577+ assert_eq ! ( guard. extend_lock( ) . is_ok( ) , false ) ;
578+ guard. forget ( ) ;
584579
585- // lock must have been cleared at this point
586- let opt = state. read ( ) . persistent_storage . get ( b"" , b"lock_4" ) ;
587- assert ! ( opt. is_some( ) ) ;
580+ // try_lock will succeed
581+ let mut lock = StorageLock :: < ' _ , Time > :: with_deadline ( b"lock_4" , lock_expiration) ;
582+ let res = lock. try_lock ( ) ;
583+ assert ! ( res. is_ok( ) ) ;
584+ let guard = res. unwrap ( ) ;
585+
586+ guard. forget ( ) ;
587+ } ) ;
588+
589+ // lock must have been cleared at this point
590+ let opt = state. read ( ) . persistent_storage . get ( b"" , b"lock_4" ) ;
591+ assert_eq ! ( opt. unwrap( ) , vec![ 132_u8 , 3u8 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ; // 132 + 256 * 3 = 900
588592 }
589593}
0 commit comments