Exemple #1
0
class TestRun(unittest.TestCase):

    def setUp(self):
        self.tout = TestTimeout('1s', variables=VariableMock())
        self.tout.start()

    def test_passing(self):
        assert_equal(self.tout.run(passing), None)

    def test_returning(self):
        for arg in [ 10, 'hello', ['l','i','s','t'], unittest]:
            ret = self.tout.run(returning, args=(arg,))
            assert_equal(ret, arg)

    def test_failing(self):
        assert_raises_with_msg(MyException, 'hello world',
                               self.tout.run, failing, ('hello world',))

    if JYTHON:

        def test_java_failing(self):
            from java.lang import Error
            from thread_resources import java_failing
            assert_raises_with_msg(Error, 'java.lang.Error: hi tellus',
                                   self.tout.run, java_failing, ('hi tellus',))

    def test_sleeping(self):
        assert_equal(self.tout.run(sleeping, args=(0.01,)), 0.01)

    def test_method_executed_normally_if_no_timeout(self):
        os.environ['ROBOT_THREAD_TESTING'] = 'initial value'
        self.tout.run(sleeping, (0.05,))
        assert_equal(os.environ['ROBOT_THREAD_TESTING'], '0.05')

    def test_method_stopped_if_timeout(self):
        os.environ['ROBOT_THREAD_TESTING'] = 'initial value'
        self.tout.secs = 0.001
        # PyThreadState_SetAsyncExc thrown exceptions are not guaranteed
        # to occur in a specific timeframe ,, thus the actual Timeout exception
        # maybe thrown too late in Windows.
        # This is why we need to have an action that really will take some time (sleep 5 secs)
        # to (almost) ensure that the 'ROBOT_THREAD_TESTING' setting is not executed before
        # timeout exception occurs
        assert_raises_with_msg(TimeoutError, 'Test timeout 1 second exceeded.',
                               self.tout.run, sleeping, (5,))
        assert_equal(os.environ['ROBOT_THREAD_TESTING'], 'initial value')

    def test_zero_and_negative_timeout(self):
        for tout in [0, 0.0, -0.01, -1, -1000]:
            self.tout.time_left = lambda: tout
            assert_raises(TimeoutError, self.tout.run, sleeping, (10,))

    def test_customized_message(self):
        tout = KeywordTimeout('1s', 'My message', VariableMock())
        tout.start()
        tout.run(passing)
        tout.secs = 0.001
        assert_raises_with_msg(TimeoutError, 'My message',
                               tout.run, sleeping, (10,))
class TestRun(unittest.TestCase):

    def setUp(self):
        self.tout = TestTimeout('1s', variables=VariableMock())
        self.tout.start()

    def test_passing(self):
        assert_equals(self.tout.run(passing), None)

    def test_returning(self):
        for arg in [ 10, 'hello', ['l','i','s','t'], unittest]:
            ret = self.tout.run(returning, args=(arg,))
            assert_equals(ret, arg)

    def test_failing(self):
        assert_raises_with_msg(MyException, 'hello world',
                               self.tout.run, failing, ('hello world',))

    if JYTHON:

        def test_java_failing(self):
            from java.lang import Error
            from thread_resources import java_failing
            assert_raises_with_msg(Error, 'java.lang.Error: hi tellus',
                                   self.tout.run, java_failing, ('hi tellus',))

    def test_sleeping(self):
        assert_equals(self.tout.run(sleeping, args=(0.01,)), 0.01)

    def test_method_executed_normally_if_no_timeout(self):
        os.environ['ROBOT_THREAD_TESTING'] = 'initial value'
        self.tout.run(sleeping, (0.05,))
        assert_equals(os.environ['ROBOT_THREAD_TESTING'], '0.05')

    def test_method_stopped_if_timeout(self):
        os.environ['ROBOT_THREAD_TESTING'] = 'initial value'
        self.tout.secs = 0.001
        # PyThreadState_SetAsyncExc thrown exceptions are not guaranteed
        # to occur in a specific timeframe ,, thus the actual Timeout exception
        # maybe thrown too late in Windows.
        # This is why we need to have an action that really will take some time (sleep 5 secs)
        # to (almost) ensure that the 'ROBOT_THREAD_TESTING' setting is not executed before
        # timeout exception occurs
        assert_raises_with_msg(TimeoutError, 'Test timeout 1 second exceeded.',
                               self.tout.run, sleeping, (5,))
        assert_equals(os.environ['ROBOT_THREAD_TESTING'], 'initial value')

    def test_zero_and_negative_timeout(self):
        for tout in [0, 0.0, -0.01, -1, -1000]:
            self.tout.time_left = lambda: tout
            assert_raises(TimeoutError, self.tout.run, sleeping, (10,))

    def test_customized_message(self):
        tout = KeywordTimeout('1s', 'My message', VariableMock())
        tout.start()
        tout.run(passing)
        tout.secs = 0.001
        assert_raises_with_msg(TimeoutError, 'My message',
                               tout.run, sleeping, (10,))
class TestRun(unittest.TestCase):
    def setUp(self):
        self.tout = TestTimeout('1s', variables=VariableMock())
        self.tout.start()

    def test_passing(self):
        assert_none(self.tout.run(passing))

    def test_returning(self):
        for arg in [10, 'hello', ['l', 'i', 's', 't'], unittest]:
            ret = self.tout.run(returning, args=(arg, ))
            assert_equals(ret, arg)

    def test_failing(self):
        assert_raises_with_msg(MyException, 'hello world', self.tout.run,
                               failing, ('hello world', ))

    if sys.platform.startswith('java'):

        def test_java_failing(self):
            from java.lang import Error
            from thread_resources import java_failing
            assert_raises_with_msg(Error, 'java.lang.Error: hi tellus',
                                   self.tout.run, java_failing,
                                   ('hi tellus', ))

    def test_sleeping(self):
        assert_equals(self.tout.run(sleeping, args=(0.01, )), 0.01)

    def test_method_executed_normally_if_no_timeout(self):
        os.environ['ROBOT_THREAD_TESTING'] = 'initial value'
        self.tout.run(sleeping, (0.05, ))
        assert_equals(os.environ['ROBOT_THREAD_TESTING'], '0.05')

    def test_method_stopped_if_timeout(self):
        os.environ['ROBOT_THREAD_TESTING'] = 'initial value'
        self.tout.secs = 0.001
        assert_raises_with_msg(TimeoutError, 'Test timeout 1 second exceeded.',
                               self.tout.run, sleeping, (0.05, ))
        time.sleep(0.1)
        assert_equals(os.environ['ROBOT_THREAD_TESTING'], 'initial value')

    def test_zero_and_negative_timeout(self):
        for tout in [0, 0.0, -0.01, -1, -1000]:
            self.tout.time_left = lambda: tout
            assert_raises(TimeoutError, self.tout.run, sleeping, (10, ))

    def test_customized_message(self):
        tout = KeywordTimeout('1s', 'My message', VariableMock())
        tout.start()
        tout.run(passing)
        tout.secs = 0.001
        time.sleep(0.01)
        assert_raises_with_msg(TimeoutError, 'My message', tout.run, sleeping,
                               (1, ))