From 0ccca87e2cb679b694a9e7bea03c33802bcae12d Mon Sep 17 00:00:00 2001 From: Tim Savannah Date: Sat, 20 May 2017 16:22:14 -0400 Subject: [PATCH] Some tests --- tests/FuncTimeoutTests/TestUtils.py | 10 +- tests/FuncTimeoutTests/test_Basic.py | 125 ++++++++++++++++++ tests/FuncTimeoutTests/test_TestUtils.py | 4 +- tests/FuncTimeoutTests/test_TestUtilsSleep.py | 18 ++- .../test_TestUtilsSleepWithArgs.py | 17 ++- 5 files changed, 163 insertions(+), 11 deletions(-) create mode 100755 tests/FuncTimeoutTests/test_Basic.py diff --git a/tests/FuncTimeoutTests/TestUtils.py b/tests/FuncTimeoutTests/TestUtils.py index 8d720c0..238a0e8 100644 --- a/tests/FuncTimeoutTests/TestUtils.py +++ b/tests/FuncTimeoutTests/TestUtils.py @@ -70,6 +70,10 @@ class ARG_NO_DEFAULT_TYPE(object): ARG_NO_DEFAULT = ARG_NO_DEFAULT_TYPE() +def getUniqueID(prefix): + uniqueName = prefix + '_' + str(uuid.uuid4().hex) + return uniqueName + def getSleepLambda(sleepTime): ''' getSleepLambda - Get a lambda that takes two integer arguments (a, b) @@ -87,7 +91,9 @@ def getSleepLambda(sleepTime): # Ensure we don't get a strange reference override on somne versions of python _sleepTime = copy.copy(sleepTime) - return lambda a, b : int(bool(time.sleep(_sleepTime))) + a + b + + + return eval('''lambda a, b : int(bool(time.sleep(%f))) + a + b''' %(_sleepTime,)) def getSleepLambdaWithArgs(sleepTime, args): @@ -138,8 +144,6 @@ def getSleepLambdaWithArgs(sleepTime, args): sumStr = ' + '.join(argNames) -# lambdaName = 'tmplambda_' + str(uuid.uuid4().hex) - # print ( 'Function is: %s' %('''lambda %s : int(bool(time.sleep(%f))) + %s''' %(argStr, sleepTime, sumStr, ) ) ) return eval('''lambda %s : int(bool(time.sleep(%f))) + %s''' % (argStr, sleepTime, sumStr, ) ) diff --git a/tests/FuncTimeoutTests/test_Basic.py b/tests/FuncTimeoutTests/test_Basic.py new file mode 100755 index 0000000..390a6bb --- /dev/null +++ b/tests/FuncTimeoutTests/test_Basic.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python +# vim: set ts=4 sw=4 expandtab : + +''' + Copyright (c) 2017 Tim Savannah All Rights Reserved. + + Licensed under the Lesser GNU Public License Version 3, LGPLv3. You should have recieved a copy of this with the source distribution as + LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE +''' + +import copy +import gc +import sys +import time +import threading +import subprocess + +from func_timeout import func_timeout, FunctionTimedOut, func_set_timeout + +from TestUtils import ARG_NO_DEFAULT, getSleepLambda, getSleepLambdaWithArgs, compareTimes + +class TestBasic(object): + ''' + TestBasic - Perform tests using the basic func_timeout function + ''' + + def test_funcTimeout(self): + sleepFunction = getSleepLambda(1.25) + + expectedResult = 5 + 13 + + startTime = time.time() + result = sleepFunction(5, 13) + endTime = time.time() + + assert result == expectedResult , 'Did not get return from sleepFunction' + + try: + result = func_timeout(1.5, sleepFunction, args=(5, 13)) + except FunctionTimedOut as te: + raise AssertionError('Got unexpected timeout at 1.5 second timeout for 1.25 second function: %s' %(str(te),)) + + assert result == expectedResult , 'Got wrong return from func_timeout.\nGot: %s\nExpected: %s\n' %(repr(result), repr(expectedResult)) + + gotException = False + try: + result = func_timeout(1, sleepFunction, args=(5, 13)) + except FunctionTimedOut as te: + gotException = True + + assert gotException , 'Expected to get FunctionTimedOut exception for 1.25 sec function at 1s timeout' + + try: + result = func_timeout(1.5, sleepFunction, args=(5,), kwargs={ 'b' : 13}) + except FunctionTimedOut as te: + raise AssertionError('Got unexpected timeout at 1.5 second timeout for 1.25 second function: %s' %(str(te), )) + except Exception as e: + raise AssertionError('Got unknown exception mixing args and kwargs: < %s > %s' %(e.__class__.__name__, str(e))) + + assert result == expectedResult , 'Got wrong result when mixing args and kwargs' + + def test_retry(self): + sleepFunction = getSleepLambda(.5) + + expectedResult = 5 + 19 + + gotException = False + functionTimedOut = None + + startTime = time.time() + try: + result = func_timeout(.3, sleepFunction, args=(5, 19)) + except FunctionTimedOut as fte: + functionTimedOut = fte + gotException = True + endTime = time.time() + + assert gotException , 'Expected to get exception' + assert compareTimes(endTime, startTime, .3, 3, None, .10) == 0 , 'Expected to wait .3 seconds. Was: %f - %f = %f' %(endTime, startTime, round(endTime - startTime, 3)) + + gotException = False + startTime = time.time() + try: + result = functionTimedOut.retry() + except FunctionTimedOut: + gotException = True + endTime = time.time() + + assert gotException , 'Expected to get exception on retry.' + assert compareTimes(endTime, startTime, .3, 3, None, .10) == 0 , 'Expected retry with no arguments to use same timeout of .3' + + gotException = False + startTime = time.time() + try: + result = functionTimedOut.retry(None) + except FunctionTimedOut: + gotException = True + endTime = time.time() + + assert not gotException , 'Did NOT to get exception with no timeout' + assert compareTimes(endTime, startTime, .5, 3, None, .10) == 0 , 'Expected retry with None as timeout to last full length of function' + + gotException = False + startTime = time.time() + try: + result = functionTimedOut.retry(.4) + except FunctionTimedOut: + gotException = True + finally: + endTime = time.time() + + assert gotException , 'Expected to time out after .4 seconds when providing .4' + assert compareTimes(endTime, startTime, .4, 3, .05, None) == 0 , 'Expected providing .4 would allow timeout of up to .4 seconds' + + time.sleep(.1) + gc.collect() + + assert threading.active_count() == 1 , 'Expected other threads to get cleaned up after gc collection' + + + +if __name__ == '__main__': + sys.exit(subprocess.Popen('GoodTests.py -n1 "%s" %s' %(sys.argv[0], ' '.join(['"%s"' %(arg.replace('"', '\\"'), ) for arg in sys.argv[1:]]) ), shell=True).wait()) + +# vim: set ts=4 sw=4 expandtab : diff --git a/tests/FuncTimeoutTests/test_TestUtils.py b/tests/FuncTimeoutTests/test_TestUtils.py index ad8a4d8..73ba3f1 100755 --- a/tests/FuncTimeoutTests/test_TestUtils.py +++ b/tests/FuncTimeoutTests/test_TestUtils.py @@ -17,9 +17,9 @@ from func_timeout import func_timeout, FunctionTimedOut, func_set_timeout from TestUtils import ARG_NO_DEFAULT, ARG_NO_DEFAULT_TYPE, getSleepLambda, getSleepLambdaWithArgs, compareTimes -class TestBasic(object): +class TestTestUtils(object): ''' - TestBasic - Perform tests using the basic func_timeout function + TestTestUtils - Perform tests using the basic func_timeout function ''' diff --git a/tests/FuncTimeoutTests/test_TestUtilsSleep.py b/tests/FuncTimeoutTests/test_TestUtilsSleep.py index 48f8719..f178a49 100755 --- a/tests/FuncTimeoutTests/test_TestUtilsSleep.py +++ b/tests/FuncTimeoutTests/test_TestUtilsSleep.py @@ -34,12 +34,24 @@ class TestBasicSleep(object): assert compareTimes(endTime, startTime, 2, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambda(2) to take 2 seconds.' - sleepLambda = getSleepLambda(3.1) + sleepLambda = getSleepLambda(1.75) + + expectedResult = 2 + 3 startTime = time.time() - sleepLambda(2, 3) + result = sleepLambda(2, 3) endTime = time.time() - assert compareTimes(endTime, startTime, 3.1, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambda(3.1) to take 3.1 seconds.' + assert result == expectedResult , 'Got wrong result' + assert compareTimes(endTime, startTime, 1.75, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambda(1.75) to take 1.75 seconds.' + + expectedResult = 5 + 13 + + startTime = time.time() + result = sleepLambda(5, 13) + endTime = time.time() + + assert result == expectedResult , 'Did not get return from sleepFunction' + assert compareTimes(endTime, startTime, 1.75, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambda(1.75) to take 1.75 seconds.' if __name__ == '__main__': sys.exit(subprocess.Popen('GoodTests.py -n1 "%s" %s' %(sys.argv[0], ' '.join(['"%s"' %(arg.replace('"', '\\"'), ) for arg in sys.argv[1:]]) ), shell=True).wait()) diff --git a/tests/FuncTimeoutTests/test_TestUtilsSleepWithArgs.py b/tests/FuncTimeoutTests/test_TestUtilsSleepWithArgs.py index 55fe01a..e1aae29 100755 --- a/tests/FuncTimeoutTests/test_TestUtilsSleepWithArgs.py +++ b/tests/FuncTimeoutTests/test_TestUtilsSleepWithArgs.py @@ -44,15 +44,26 @@ class TestBasicSleepWithArgs(object): - sleepLambda = getSleepLambdaWithArgs(3.1, [ ('a', ), ('xxx', )] ) + expectedResult = 2 + 3 + sleepLambda = getSleepLambdaWithArgs(1.75, [ ('a', ), ('xxx', )] ) startTime = time.time() try: - sleepLambda(xxx=2, a=3) + result = sleepLambda(xxx=2, a=3) except: raise AssertionError('Expected to be able to use provided field names when calling function') endTime = time.time() - assert compareTimes(endTime, startTime, 3.1, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambdaWithArgs(3.1) to take 3.1 seconds.' + assert result == expectedResult , 'Got wrong result' + assert compareTimes(endTime, startTime, 1.75, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambdaWithArgs(1.75) to take 1.75 seconds.' + + expectedResult = 5 + 13 + + startTime = time.time() + result = sleepLambda(5, 13) + endTime = time.time() + + assert result == expectedResult , 'Did not get return from sleepFunction' + assert compareTimes(endTime, startTime, 1.75, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambda(1.75) to take 1.75 seconds.' if __name__ == '__main__': sys.exit(subprocess.Popen('GoodTests.py -n1 "%s" %s' %(sys.argv[0], ' '.join(['"%s"' %(arg.replace('"', '\\"'), ) for arg in sys.argv[1:]]) ), shell=True).wait())