<s> import unittest <EOL> import sys <EOL> import time <EOL> import os <EOL> from robot . errors import TimeoutError <EOL> from robot . running . timeouts import TestTimeout , KeywordTimeout <EOL> from robot . utils . asserts import ( assert_equal , assert_false , assert_true , <EOL> assert_raises , assert_raises_with_msg ) <EOL> from robot . utils import JYTHON <EOL> sys . path . append ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' , '<STR_LIT>' ) ) <EOL> from thread_resources import passing , failing , sleeping , returning , MyException <EOL> class VariableMock ( object ) : <EOL> def replace_string ( self , string ) : <EOL> return string <EOL> class TestInit ( unittest . TestCase ) : <EOL> def test_no_params ( self ) : <EOL> self . _verify_tout ( TestTimeout ( ) ) <EOL> def test_timeout_string ( self ) : <EOL> for tout_str , exp_str , exp_secs in [ ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:10> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT> ) ] : <EOL> self . _verify_tout ( TestTimeout ( tout_str ) , exp_str , exp_secs ) <EOL> def test_invalid_timeout_string ( self ) : <EOL> for inv in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> for params in [ [ inv ] , [ inv , '<STR_LIT>' ] ] : <EOL> tout = TestTimeout ( * params ) <EOL> err = "<STR_LIT>" <EOL> self . _verify_tout ( tout , str = inv , secs = <NUM_LIT> , err = err % inv ) <EOL> def _verify_tout ( self , tout , str = '<STR_LIT>' , secs = - <NUM_LIT:1> , err = None ) : <EOL> tout . replace_variables ( VariableMock ( ) ) <EOL> assert_equal ( tout . string , str ) <EOL> assert_equal ( tout . secs , secs ) <EOL> assert_equal ( tout . error , err ) <EOL> class TestTimer ( unittest . TestCase ) : <EOL> def test_time_left ( self ) : <EOL> tout = TestTimeout ( '<STR_LIT>' , variables = VariableMock ( ) ) <EOL> tout . start ( ) <EOL> assert_true ( tout . time_left ( ) > <NUM_LIT> ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> assert_true ( tout . time_left ( ) < <NUM_LIT> ) <EOL> def test_timed_out_with_no_timeout ( self ) : <EOL> tout = TestTimeout ( variables = VariableMock ( ) ) <EOL> tout . start ( ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> assert_false ( tout . timed_out ( ) ) <EOL> def test_timed_out_with_non_exceeded_timeout ( self ) : <EOL> tout = TestTimeout ( '<STR_LIT>' , variables = VariableMock ( ) ) <EOL> tout . start ( ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> assert_false ( tout . timed_out ( ) ) <EOL> def test_timed_out_with_exceeded_timeout ( self ) : <EOL> tout = TestTimeout ( '<STR_LIT>' , variables = VariableMock ( ) ) <EOL> tout . start ( ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> assert_true ( tout . timed_out ( ) ) <EOL> class TestComparisons ( unittest . TestCase ) : <EOL> def test_compare_when_none_timeouted ( self ) : <EOL> touts = self . _create_timeouts ( [ '<STR_LIT>' ] * <NUM_LIT:10> ) <EOL> assert_equal ( min ( touts ) . string , '<STR_LIT>' ) <EOL> assert_equal ( max ( touts ) . string , '<STR_LIT>' ) <EOL> def test_compare_when_all_timeouted ( self ) : <EOL> touts = self . _create_timeouts ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert_equal ( min ( touts ) . string , '<STR_LIT>' ) <EOL> assert_equal ( max ( touts ) . string , '<STR_LIT>' ) <EOL> def test_compare_with_timeouted_and_non_timeouted ( self ) : <EOL> touts = self . _create_timeouts ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert_equal ( min ( touts ) . string , '<STR_LIT>' ) <EOL> assert_equal ( max ( touts ) . string , '<STR_LIT>' ) <EOL> def test_that_compare_uses_starttime ( self ) : <EOL> touts = self . _create_timeouts ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> touts [ <NUM_LIT:2> ] . starttime -= <NUM_LIT:2> <EOL> assert_equal ( min ( touts ) . string , '<STR_LIT>' ) <EOL> assert_equal ( max ( touts ) . string , '<STR_LIT>' ) <EOL> def _create_timeouts ( self , tout_strs ) : <EOL> touts = [ ] <EOL> for tout_str in tout_strs : <EOL> touts . append ( TestTimeout ( tout_str , variables = VariableMock ( ) ) ) <EOL> touts [ - <NUM_LIT:1> ] . start ( ) <EOL> return touts <EOL> class TestRun ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . tout = TestTimeout ( '<STR_LIT>' , variables = VariableMock ( ) ) <EOL> self . tout . start ( ) <EOL> def test_passing ( self ) : <EOL> assert_equal ( self . tout . run ( passing ) , None ) <EOL> def test_returning ( self ) : <EOL> for arg in [ <NUM_LIT:10> , '<STR_LIT:hello>' , [ '<STR_LIT:l>' , '<STR_LIT:i>' , '<STR_LIT:s>' , '<STR_LIT:t>' ] , unittest ] : <EOL> ret = self . tout . run ( returning , args = ( arg , ) ) <EOL> assert_equal ( ret , arg ) <EOL> def test_failing ( self ) : <EOL> assert_raises_with_msg ( MyException , '<STR_LIT>' , <EOL> self . tout . run , failing , ( '<STR_LIT>' , ) ) <EOL> if JYTHON : <EOL> def test_java_failing ( self ) : <EOL> from java . lang import Error <EOL> from thread_resources import java_failing <EOL> assert_raises_with_msg ( Error , '<STR_LIT>' , <EOL> self . tout . run , java_failing , ( '<STR_LIT>' , ) ) <EOL> def test_sleeping ( self ) : <EOL> assert_equal ( self . tout . run ( sleeping , args = ( <NUM_LIT> , ) ) , <NUM_LIT> ) <EOL> def test_method_executed_normally_if_no_timeout ( self ) : <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . tout . run ( sleeping , ( <NUM_LIT> , ) ) <EOL> assert_equal ( os . environ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_method_stopped_if_timeout ( self ) : <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . tout . secs = <NUM_LIT> <EOL> assert_raises_with_msg ( TimeoutError , '<STR_LIT>' , <EOL> self . tout . run , sleeping , ( <NUM_LIT:5> , ) ) <EOL> assert_equal ( os . environ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_zero_and_negative_timeout ( self ) : <EOL> for tout in [ <NUM_LIT:0> , <NUM_LIT:0.0> , - <NUM_LIT> , - <NUM_LIT:1> , - <NUM_LIT:1000> ] : <EOL> self . tout . time_left = lambda : tout <EOL> assert_raises ( TimeoutError , self . tout . run , sleeping , ( <NUM_LIT:10> , ) ) <EOL> def test_customized_message ( self ) : <EOL> tout = KeywordTimeout ( '<STR_LIT>' , '<STR_LIT>' , VariableMock ( ) ) <EOL> tout . start ( ) <EOL> tout . run ( passing ) <EOL> tout . secs = <NUM_LIT> <EOL> assert_raises_with_msg ( TimeoutError , '<STR_LIT>' , <EOL> tout . run , sleeping , ( <NUM_LIT:10> , ) ) <EOL> class TestMessage ( unittest . TestCase ) : <EOL> def test_non_active ( self ) : <EOL> assert_equal ( TestTimeout ( ) . get_message ( ) , '<STR_LIT>' ) <EOL> def test_active ( self ) : <EOL> tout = KeywordTimeout ( '<STR_LIT>' , variables = VariableMock ( ) ) <EOL> tout . start ( ) <EOL> msg = tout . get_message ( ) <EOL> assert_true ( msg . startswith ( '<STR_LIT>' ) , msg ) <EOL> assert_true ( msg . endswith ( '<STR_LIT>' ) , msg ) <EOL> def test_failed_default ( self ) : <EOL> tout = TestTimeout ( '<STR_LIT>' , variables = VariableMock ( ) ) <EOL> tout . starttime = time . time ( ) - <NUM_LIT:2> <EOL> assert_equal ( tout . get_message ( ) , '<STR_LIT>' ) <EOL> def test_failed_custom ( self ) : <EOL> tout = KeywordTimeout ( '<STR_LIT>' , '<STR_LIT>' , VariableMock ( ) ) <EOL> tout . starttime = time . time ( ) - <NUM_LIT:2> <EOL> assert_equal ( tout . get_message ( ) , '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
