<s> from __future__ import absolute_import <EOL> import unittest <EOL> import logging <EOL> import time <EOL> from salt . utils . timeout import wait_for <EOL> log = logging . getLogger ( __name__ ) <EOL> def return_something_after ( seconds , something = True ) : <EOL> start = time . time ( ) <EOL> end = start + seconds <EOL> log . debug ( "<STR_LIT>" , something , end ) <EOL> def actual ( ) : <EOL> t = time . time ( ) <EOL> condition = t >= end <EOL> log . debug ( "<STR_LIT>" , t , condition ) <EOL> if condition : <EOL> return something <EOL> else : <EOL> return False <EOL> return actual <EOL> def return_args_after ( seconds ) : <EOL> start = time . time ( ) <EOL> end = start + seconds <EOL> def actual ( * args ) : <EOL> if time . time ( ) >= end : <EOL> return args <EOL> else : <EOL> return False <EOL> return actual <EOL> def return_kwargs_after ( seconds ) : <EOL> start = time . time ( ) <EOL> end = start + seconds <EOL> def actual ( ** kwargs ) : <EOL> if time . time ( ) >= end : <EOL> return kwargs <EOL> else : <EOL> return False <EOL> return actual <EOL> class WaitForTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . true_after_1s = return_something_after ( <NUM_LIT:1> ) <EOL> self . self_after_1s = return_something_after ( <NUM_LIT:1> , something = self ) <EOL> def test_wait_for_true ( self ) : <EOL> ret = wait_for ( self . true_after_1s , timeout = <NUM_LIT:2> , step = <NUM_LIT:0.5> ) <EOL> self . assertTrue ( ret ) <EOL> def test_wait_for_self ( self ) : <EOL> ret = wait_for ( self . self_after_1s , timeout = <NUM_LIT:2> , step = <NUM_LIT:0.5> ) <EOL> self . assertEqual ( ret , self ) <EOL> def test_wait_for_too_long ( self ) : <EOL> ret = wait_for ( self . true_after_1s , timeout = <NUM_LIT:0.5> , step = <NUM_LIT:0.1> , default = False ) <EOL> self . assertFalse ( ret ) <EOL> def test_wait_for_with_big_step ( self ) : <EOL> ret = wait_for ( self . true_after_1s , timeout = <NUM_LIT> , step = <NUM_LIT:2> ) <EOL> self . assertTrue ( ret ) <EOL> def test_wait_for_custom_args ( self ) : <EOL> args_after_1s = return_args_after ( <NUM_LIT:1> ) <EOL> args = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ret = wait_for ( args_after_1s , timeout = <NUM_LIT:2> , step = <NUM_LIT:0.5> , func_args = args ) <EOL> self . assertEqual ( ret , args ) <EOL> def test_wait_for_custom_kwargs ( self ) : <EOL> kwargs_after_1s = return_kwargs_after ( <NUM_LIT:1> ) <EOL> kwargs = { "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:2> } <EOL> ret = wait_for ( kwargs_after_1s , timeout = <NUM_LIT:2> , step = <NUM_LIT:0.5> , func_kwargs = kwargs ) <EOL> self . assertEqual ( ret , kwargs ) <EOL> def test_return_false ( self ) : <EOL> ret = self . true_after_1s ( ) <EOL> self . assertFalse ( ret ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
