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_exception(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 'timed out after ' in functionTimedOut.msg, 'Expected message to be constructed. Got: %s' % (
            repr(functionTimedOut.msg), )
        assert round(
            functionTimedOut.timedOutAfter, 1
        ) == .3, 'Expected timedOutAfter to equal timeout ( .3 ). Got: %s' % (
            str(round(functionTimedOut.timedOutAfter, 1)), )
        assert functionTimedOut.timedOutFunction == sleepFunction, 'Expected timedOutFunction to equal sleepFunction'
        assert functionTimedOut.timedOutArgs == (
            5, 19), 'Expected args to equal (5, 19)'
        assert functionTimedOut.timedOutKwargs == {}, 'Expected timedOutKwargs to equal {}'
Example #4
0
    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'
Example #5
0
    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'
Example #6
0
    def test_exception(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 'timed out after ' in functionTimedOut.msg  , 'Expected message to be constructed. Got: %s' %(repr(functionTimedOut.msg), )
        assert round(functionTimedOut.timedOutAfter, 1) == .3 , 'Expected timedOutAfter to equal timeout ( .3 ). Got: %s' %(str(round(functionTimedOut.timedOutAfter, 1)), )
        assert functionTimedOut.timedOutFunction == sleepFunction , 'Expected timedOutFunction to equal sleepFunction'
        assert functionTimedOut.timedOutArgs == (5, 19) , 'Expected args to equal (5, 19)'
        assert functionTimedOut.timedOutKwargs == {} , 'Expected timedOutKwargs to equal {}'
Example #7
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 #8
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'