def test_getSleepLambda(self):

        sleepLambda = getSleepLambda(2)
        startTime = time.time()
        sleepLambda(2, 3)
        endTime = time.time()

        assert compareTimes(
            endTime, startTime, 2, 2, deltaFixed=.1, deltaPct=None
        ) == 0, 'Expected getSleepLambda(2) to take 2 seconds.'

        sleepLambda = getSleepLambda(1.75)

        expectedResult = 2 + 3
        startTime = time.time()
        result = sleepLambda(2, 3)
        endTime = time.time()

        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.'
    def test_getSleepLambda(self):
        
        sleepLambda = getSleepLambda(2)
        startTime = time.time()
        sleepLambda(2, 3)
        endTime = time.time()

        assert compareTimes(endTime, startTime, 2, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambda(2) to take 2 seconds.'

        sleepLambda = getSleepLambda(1.75)

        expectedResult = 2 + 3
        startTime = time.time()
        result = sleepLambda(2, 3)
        endTime = time.time()

        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.'
Example #3
0
    def test_funcSetTimeout(self):
        
        @func_set_timeout(SLEEP_TIME)
        def doSleepFunc(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME * 1.3 + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFunc(SLEEP_TIME * 1.3, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert gotException , 'Expected to get exception at sleep time'
        assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep up to sleep time'

        expected = SLEEP_TIME * .8 + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFunc(SLEEP_TIME * .8, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException , 'Expected not to get exception at 80% sleep time'
        assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, .1) == 0 , 'Expected to only sleep for 80% of sleep time'
        assert result == expected , 'Got wrong result'
Example #4
0
    def test_funcSetTimeout(self):
        @func_set_timeout(SLEEP_TIME)
        def doSleepFunc(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME * 1.3 + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFunc(SLEEP_TIME * 1.3, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert gotException, 'Expected to get exception at sleep time'
        assert compareTimes(endTime, startTime, SLEEP_TIME, None,
                            .1) == 0, 'Expected to sleep up to sleep time'

        expected = SLEEP_TIME * .8 + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFunc(SLEEP_TIME * .8, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException, 'Expected not to get exception at 80% sleep time'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME * .8, None,
            .1) == 0, 'Expected to only sleep for 80% of sleep time'
        assert result == expected, 'Got wrong result'
    def test_funcSetTimeoutOverride(self):
        @func_set_timeout(SLEEP_TIME, allowOverride=True)
        def doSleepFunc(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME * 1.3 + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFunc(SLEEP_TIME * 1.3,
                                 4,
                                 forceTimeout=SLEEP_TIME * 1.2)
        except FunctionTimedOut as fte:
            gotException = True
        except Exception as e:
            raise AssertionError(
                'Expected to be able to override forceTimeout when allowOverride=True'
            )
        endTime = time.time()

        assert gotException, 'Expected to get exception at 130% sleep time'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME * 1.2, None,
            deltaFixed=.15) == 0, 'Expected to sleep up to 120% of sleep time'

        @func_set_timeout(SLEEP_TIME, allowOverride=False)
        def doSleepFuncNoOverride(a, b):
            time.sleep(a)
            return a + b

        gotException = False
        try:
            doSleepFuncNoOverride(SLEEP_TIME * 1.3,
                                  4,
                                  forceTimeout=SLEEP_TIME * 1.2)
        except Exception as e:
            gotException = True
        except FunctionTimedOut as fte:
            raise AssertionError(
                'Expected to NOT be able to pass forceTimeout when allowOverride=False, but got FunctionTimedOut exception'
            )

        assert gotException, 'Expected to NOT be able to pass forceTimeout when allowOverride=False, but did not get exception'

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFunc(SLEEP_TIME, 4, forceTimeout=SLEEP_TIME * 1.15)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException, 'Expected not to get exception with forced 115% sleep time'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME, None,
            deltaFixed=.15) == 0, 'Expected to sleep for SLEEP_TIME'
        assert result == expected, 'Got wrong result'
Example #6
0
    def test_compareTimes(self):

        startTime = 50.00
        endTime   = 52.03

        assert compareTimes(endTime, startTime, 2, roundTo=2, deltaFixed=.05, deltaPct=None) == 0 , 'Expected deltaFixed to be > abs(delta) to show times equal'

        assert compareTimes(endTime, startTime, 2, roundTo=2, deltaFixed=.01, deltaPct=None) == .03 , 'Expected when deltaFixed is less than the abs delta, actual diff to be returned.'

        assert compareTimes(endTime, startTime, 2, roundTo=2, deltaFixed=None, deltaPct=.2) == 0 , 'Expected deltaPct * cmpTime when greater than abs delta to be equal'

        assert compareTimes(endTime, startTime, 2, roundTo=2, deltaFixed=None, deltaPct=.0002) == .03 , 'Expected deltaPct * cmpTime when less than abs delta to be actual diff'
Example #7
0
    def test_funcSetTimeoutOverride(self):
        @func_set_timeout(SLEEP_TIME, allowOverride=True)
        def doSleepFunc(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME * 1.3 + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFunc(SLEEP_TIME * 1.3, 4, forceTimeout=SLEEP_TIME * 1.2)
        except FunctionTimedOut as fte:
            gotException = True
        except Exception as e:
            raise AssertionError('Expected to be able to override forceTimeout when allowOverride=True')
        endTime = time.time()

        assert gotException , 'Expected to get exception at 130% sleep time'
        assert compareTimes(endTime, startTime, SLEEP_TIME * 1.2, None, .1) == 0 , 'Expected to sleep up to 120% of sleep time'

        @func_set_timeout(SLEEP_TIME, allowOverride=False)
        def doSleepFuncNoOverride(a, b):
            time.sleep(a)
            return a + b

        gotException = False
        try:
            doSleepFuncNoOverride(SLEEP_TIME * 1.3, 4, forceTimeout=SLEEP_TIME * 1.2)
        except Exception as e:
            gotException = True
        except FunctionTimedOut as fte:
            raise AssertionError('Expected to NOT be able to pass forceTimeout when allowOverride=False, but got FunctionTimedOut exception')

        assert gotException , 'Expected to NOT be able to pass forceTimeout when allowOverride=False, but did not get exception'
            

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFunc(SLEEP_TIME, 4, forceTimeout=SLEEP_TIME * 1.15)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException , 'Expected not to get exception with forced 115% sleep time'
        assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep for SLEEP_TIME'
        assert result == expected , 'Got wrong result'
Example #8
0
    def test_getSleepLambdaWithArgs(self):

        sleepLambda = getSleepLambdaWithArgs(2, [('a', ), ('b', ), ('c', 4)])
        startTime = time.time()
        try:
            sleepLambda(1, 2)
        except:
            raise AssertionError(
                'Expected to have 1 default arg and 2 standard. Tried 3 args')
        endTime = time.time()

        assert compareTimes(
            endTime, startTime, 2, 2, deltaFixed=.1, deltaPct=None
        ) == 0, 'Expected getSleepLambdaWithArgs(2) to take 2 seconds.'

        try:
            sleepLambda(4, 7, 12)
        except:
            raise AssertionError(
                'Expected to have 1 default arg and 2 standard. Tried 3 args.')

        expectedResult = 2 + 3
        sleepLambda = getSleepLambdaWithArgs(1.75, [('a', ), ('xxx', )])
        startTime = time.time()
        try:
            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 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.'
    def test_compareTimes(self):

        startTime = 50.00
        endTime = 52.03

        assert compareTimes(
            endTime, startTime, 2, roundTo=2, deltaFixed=.05, deltaPct=None
        ) == 0, 'Expected deltaFixed to be > abs(delta) to show times equal'

        assert compareTimes(
            endTime, startTime, 2, roundTo=2, deltaFixed=.01, deltaPct=None
        ) == .03, 'Expected when deltaFixed is less than the abs delta, actual diff to be returned.'

        assert compareTimes(
            endTime, startTime, 2, roundTo=2, deltaFixed=None, deltaPct=.2
        ) == 0, 'Expected deltaPct * cmpTime when greater than abs delta to be equal'

        assert compareTimes(
            endTime, startTime, 2, roundTo=2, deltaFixed=None, deltaPct=.0002
        ) == .03, 'Expected deltaPct * cmpTime when less than abs delta to be actual diff'
    def test_getSleepLambdaWithArgs(self):
        
        sleepLambda = getSleepLambdaWithArgs(2, [ ('a', ), ('b', ), ('c', 4) ] )
        startTime = time.time()
        try:
            sleepLambda(1, 2)
        except:
            raise AssertionError('Expected to have 1 default arg and 2 standard. Tried 3 args')
        endTime = time.time()

        assert compareTimes(endTime, startTime, 2, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambdaWithArgs(2) to take 2 seconds.'

        try:
            sleepLambda(4, 7, 12)
        except:
            raise AssertionError('Expected to have 1 default arg and 2 standard. Tried 3 args.')



        expectedResult = 2 + 3
        sleepLambda = getSleepLambdaWithArgs(1.75, [ ('a', ), ('xxx', )] )
        startTime = time.time()
        try:
            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 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.'
Example #11
0
    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'

        threadsCleanedUp = False

        for i in range(5):
            time.sleep(1)
            gc.collect()

            if threading.active_count() == 1:
                threadsCleanedUp = True
                break

                
        assert threadsCleanedUp , 'Expected other threads to get cleaned up after gc collection'
Example #12
0
    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'

        threadsCleanedUp = False

        for i in range(5):
            time.sleep(1)
            gc.collect()

            if threading.active_count() == 1:
                threadsCleanedUp = True
                break

        assert threadsCleanedUp, 'Expected other threads to get cleaned up after gc collection'
Example #13
0
    def test_setFuncTimeoutetry(self):
        def calculateSleepOver(a, b):
            return a * 1.2

        def calculateSleepUnder(a, b):
            return a * .8

        def calculateSleepOverArgs(*args, **kwargs):
            return args[0] * 1.2

        def calculateSleepUnderArgs(*args, **kwargs):
            return args[0] * .8

        @func_set_timeout(calculateSleepUnder, allowOverride=True)
        def doSleepFuncUnder(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME + 4
        gotException = False
        functionTimedOut = None
        startTime = time.time()
        try:
            result = doSleepFuncUnder(SLEEP_TIME, 4)
        except FunctionTimedOut as fte:
            gotException = True
            functionTimedOut = fte
        endTime = time.time()

        assert gotException, 'Expected to get exception with calculated 80% timeout'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME * .8, None,
            .1) == 0, 'Expected to sleep for 80% SLEEP_TIME with 80% timeout'

        gotException = False
        startTime = time.time()
        try:
            functionTimedOut.retry()
        except FunctionTimedOut as fte2:
            gotException = True
        except Exception as e:
            raise AssertionError(
                'Got exception trying to retry with same timeout:  < %s > : %s'
                % (e.__name__, str(e)))
        endTime = time.time()

        assert gotException, 'Expected to get exception with calculated same 80% timeout on retry'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME * .8, None, .1
        ) == 0, 'Expected to sleep for 80% SLEEP_TIME with same 80% timeout on retry'

        result = None
        gotException = False
        startTime = time.time()
        try:
            result = functionTimedOut.retry(None)
        except FunctionTimedOut as fte2:
            gotException = True
        except Exception as e:
            raise AssertionError(
                'Got exception trying to retry with same timeout:  < %s > : %s'
                % (e.__name__, str(e)))
        endTime = time.time()

        assert not gotException, 'Expected to get exception with calculated 80% timeout on retry ( None ) [ No timeout ]'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME, None, .1
        ) == 0, 'Expected to sleep for 100% SLEEP_TIME with 80% timeout overriden on retry ( None ) [ No timeout ]'
        assert result == expected, 'Got wrong result'

        gotException = False
        startTime = time.time()
        try:
            functionTimedOut.retry(SLEEP_TIME * .6)
        except FunctionTimedOut as fte2:
            gotException = True
        except Exception as e:
            raise AssertionError(
                'Got exception trying to retry with same timeout:  < %s > : %s'
                % (e.__name__, str(e)))
        endTime = time.time()

        assert gotException, 'Expected to get exception with calculated 80% timeout overriden by 60% timeout on retry'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME * .6, None, .1
        ) == 0, 'Expected to sleep for 60% SLEEP_TIME with 80% timeout overriden on retry ( SLEEP_TIME * .6 ) [ 60% timeout ]'

        result = None
        gotException = False
        startTime = time.time()
        try:
            result = functionTimedOut.retry(SLEEP_TIME * 1.5)
        except FunctionTimedOut as fte2:
            gotException = True
        except Exception as e:
            raise AssertionError(
                'Got exception trying to retry with same timeout:  < %s > : %s'
                % (e.__name__, str(e)))
        endTime = time.time()

        assert not gotException, 'Expected to get exception with calculated 80% timeout overriden by 150% timeout on retry'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME, None, .1
        ) == 0, 'Expected to sleep for 100% SLEEP_TIME with 80% timeout overriden on retry ( SLEEP_TIME * 1.5 ) [ 150% timeout ]'
        assert result == expected

        threadsCleanedUp = False

        for i in range(5):
            time.sleep(1)
            gc.collect()

            if threading.active_count() == 1:
                threadsCleanedUp = True
                break

        assert threadsCleanedUp, 'Expected other threads to get cleaned up after gc collection'
Example #14
0
    def test_funcSetTimeCalculateWithOverride(self):
        def calculateSleepOver(a, b):
            return a * 1.2

        def calculateSleepUnder(a, b):
            return a * .8

        def calculateSleepOverArgs(*args, **kwargs):
            return args[0] * 1.2

        def calculateSleepUnderArgs(*args, **kwargs):
            return args[0] * .8

        @func_set_timeout(calculateSleepOver, allowOverride=True)
        def doSleepFuncOver(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncOver(SLEEP_TIME, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException, 'Expected not to get exception with calculated 120% timeout on sleep time'
        assert compareTimes(endTime, startTime, SLEEP_TIME, None,
                            .1) == 0, 'Expected to sleep for SLEEP_TIME'
        assert result == expected, 'Got wrong result'

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncOver(SLEEP_TIME,
                                     4,
                                     forceTimeout=SLEEP_TIME * 1.5)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException, 'Expected not to get exception with calculated 120% timeout on sleep time but 150% timeout on override'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME, None, .1
        ) == 0, 'Expected to sleep for SLEEP_TIME with 150% timeout on override'
        assert result == expected, 'Got wrong result'

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncOver(SLEEP_TIME,
                                     4,
                                     forceTimeout=SLEEP_TIME * .7)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert gotException, 'Expected to get exception with calculated 120% timeout on sleep time but 70% timeout on override'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME * .7, None, .1
        ) == 0, 'Expected to sleep for 70% SLEEP_TIME with 70% timeout on override'

        @func_set_timeout(calculateSleepUnder, allowOverride=True)
        def doSleepFuncUnder(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncUnder(SLEEP_TIME, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert gotException, 'Expected to get exception with calculated 80% timeout on sleep time'
        assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None,
                            .1) == 0, 'Expected to sleep for 80% SLEEP_TIME'

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncUnder(SLEEP_TIME,
                                      4,
                                      forceTimeout=SLEEP_TIME * 1.2)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException, 'Expected not to get exception with calculated 80% timeout on sleep time but 120% timeout on override'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME, None, .1
        ) == 0, 'Expected to sleep for SLEEP_TIME with 120% timeout on override'
Example #15
0
    def test_funcSetTimeCalculateWithOverride(self):

        def calculateSleepOver(a, b):
            return a * 1.2

        def calculateSleepUnder(a, b):
            return a * .8

        def calculateSleepOverArgs(*args, **kwargs):
            return args[0] * 1.2

        def calculateSleepUnderArgs(*args, **kwargs):
            return args[0] * .8

        @func_set_timeout(calculateSleepOver, allowOverride=True)
        def doSleepFuncOver(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncOver(SLEEP_TIME, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException , 'Expected not to get exception with calculated 120% timeout on sleep time'
        assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep for SLEEP_TIME'
        assert result == expected , 'Got wrong result'

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncOver(SLEEP_TIME, 4, forceTimeout= SLEEP_TIME * 1.5)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException , 'Expected not to get exception with calculated 120% timeout on sleep time but 150% timeout on override'
        assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep for SLEEP_TIME with 150% timeout on override'
        assert result == expected , 'Got wrong result'

        
        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncOver(SLEEP_TIME, 4, forceTimeout=SLEEP_TIME * .7)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert gotException , 'Expected to get exception with calculated 120% timeout on sleep time but 70% timeout on override'
        assert compareTimes(endTime, startTime, SLEEP_TIME * .7, None, .1) == 0 , 'Expected to sleep for 70% SLEEP_TIME with 70% timeout on override'
        
        
        @func_set_timeout(calculateSleepUnder, allowOverride=True)
        def doSleepFuncUnder(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncUnder(SLEEP_TIME, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert gotException , 'Expected to get exception with calculated 80% timeout on sleep time'
        assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, .1) == 0 , 'Expected to sleep for 80% SLEEP_TIME'

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncUnder(SLEEP_TIME, 4, forceTimeout=SLEEP_TIME * 1.2)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException , 'Expected not to get exception with calculated 80% timeout on sleep time but 120% timeout on override'
        assert compareTimes(endTime, startTime, SLEEP_TIME , None, .1) == 0 , 'Expected to sleep for SLEEP_TIME with 120% timeout on override'
    def test_funcSetTimeCalculate(self):
        def calculateSleepOver(a, b):
            return a * 1.2

        def calculateSleepUnder(a, b):
            return a * .8

        def calculateSleepOverArgs(*args, **kwargs):
            return args[0] * 1.2

        def calculateSleepUnderArgs(*args, **kwargs):
            return args[0] * .8

        @func_set_timeout(calculateSleepOver, allowOverride=False)
        def doSleepFuncOver(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncOver(SLEEP_TIME, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException, 'Expected not to get exception with calculated 120% timeout on sleep time'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME, None,
            deltaFixed=.15) == 0, 'Expected to sleep for SLEEP_TIME'
        assert result == expected, 'Got wrong result'

        @func_set_timeout(calculateSleepUnder, allowOverride=False)
        def doSleepFuncUnder(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncUnder(SLEEP_TIME, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert gotException, 'Expected to get exception with calculated 80% timeout on sleep time'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME * .8, None,
            deltaFixed=.15) == 0, 'Expected to sleep for 80% SLEEP_TIME'

        @func_set_timeout(calculateSleepOverArgs, allowOverride=False)
        def doSleepFuncOverArgs(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncOverArgs(SLEEP_TIME, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert not gotException, 'Expected not to get exception with calculated 120% timeout on sleep time using *args'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME, None, deltaFixed=.15
        ) == 0, 'Expected to sleep for SLEEP_TIME using *args'
        assert result == expected, 'Got wrong result'

        @func_set_timeout(calculateSleepUnderArgs, allowOverride=False)
        def doSleepFuncUnderArgs(a, b):
            time.sleep(a)
            return a + b

        expected = SLEEP_TIME + 4
        gotException = False
        startTime = time.time()
        try:
            result = doSleepFuncUnderArgs(SLEEP_TIME, 4)
        except FunctionTimedOut as fte:
            gotException = True
        endTime = time.time()

        assert gotException, 'Expected to get exception with calculated 80% timeout on sleep time using *args'
        assert compareTimes(
            endTime, startTime, SLEEP_TIME * .8, None, deltaFixed=.15
        ) == 0, 'Expected to sleep for 80% SLEEP_TIME using *args'
Example #17
0
    def test_setFuncTimeoutetry(self):
        def calculateSleepOver(a, b):
            return a * 1.2

        def calculateSleepUnder(a, b):
            return a * .8

        def calculateSleepOverArgs(*args, **kwargs):
            return args[0] * 1.2

        def calculateSleepUnderArgs(*args, **kwargs):
            return args[0] * .8

        @func_set_timeout(calculateSleepUnder, allowOverride=True)
        def doSleepFuncUnder(a, b):
            time.sleep(a)
            return a + b


        expected = SLEEP_TIME + 4
        gotException = False
        functionTimedOut = None
        startTime = time.time()
        try:
            result = doSleepFuncUnder(SLEEP_TIME, 4)
        except FunctionTimedOut as fte:
            gotException = True
            functionTimedOut = fte
        endTime = time.time()

        assert gotException , 'Expected to get exception with calculated 80% timeout'
        assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, .1) == 0 , 'Expected to sleep for 80% SLEEP_TIME with 80% timeout'

        gotException = False
        startTime = time.time()
        try:
            functionTimedOut.retry()
        except FunctionTimedOut as fte2:
            gotException = True
        except Exception as e:
            raise AssertionError('Got exception trying to retry with same timeout:  < %s > : %s' %(e.__name__, str(e)))
        endTime = time.time()

        assert gotException , 'Expected to get exception with calculated same 80% timeout on retry'
        assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, .1) == 0 , 'Expected to sleep for 80% SLEEP_TIME with same 80% timeout on retry'

        result = None
        gotException = False
        startTime = time.time()
        try:
            result = functionTimedOut.retry(None)
        except FunctionTimedOut as fte2:
            gotException = True
        except Exception as e:
            raise AssertionError('Got exception trying to retry with same timeout:  < %s > : %s' %(e.__name__, str(e)))
        endTime = time.time()

        assert not gotException , 'Expected to get exception with calculated 80% timeout on retry ( None ) [ No timeout ]'
        assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep for 100% SLEEP_TIME with 80% timeout overriden on retry ( None ) [ No timeout ]'
        assert result == expected , 'Got wrong result'



        gotException = False
        startTime = time.time()
        try:
            functionTimedOut.retry(SLEEP_TIME * .6)
        except FunctionTimedOut as fte2:
            gotException = True
        except Exception as e:
            raise AssertionError('Got exception trying to retry with same timeout:  < %s > : %s' %(e.__name__, str(e)))
        endTime = time.time()

        assert gotException , 'Expected to get exception with calculated 80% timeout overriden by 60% timeout on retry'
        assert compareTimes(endTime, startTime, SLEEP_TIME * .6, None, .1) == 0 , 'Expected to sleep for 60% SLEEP_TIME with 80% timeout overriden on retry ( SLEEP_TIME * .6 ) [ 60% timeout ]'
        
        result = None
        gotException = False
        startTime = time.time()
        try:
            result = functionTimedOut.retry(SLEEP_TIME * 1.5)
        except FunctionTimedOut as fte2:
            gotException = True
        except Exception as e:
            raise AssertionError('Got exception trying to retry with same timeout:  < %s > : %s' %(e.__name__, str(e)))
        endTime = time.time()

        assert not gotException , 'Expected to get exception with calculated 80% timeout overriden by 150% timeout on retry'
        assert compareTimes(endTime, startTime, SLEEP_TIME , None, .1) == 0 , 'Expected to sleep for 100% SLEEP_TIME with 80% timeout overriden on retry ( SLEEP_TIME * 1.5 ) [ 150% timeout ]'
        assert result == expected
        
        threadsCleanedUp = False

        for i in range(5):
            time.sleep(1)
            gc.collect()

            if threading.active_count() == 1:
                threadsCleanedUp = True
                break

                
        assert threadsCleanedUp , 'Expected other threads to get cleaned up after gc collection'