def test_timed(): @timelimit(1.0) def sleepy_function(x): sleep(x) with assert_throws(Timeout): # should timeout sleepy_function(3.0) sleepy_function(0.2) # should succeed @timelimit(1) def raises_errors(): return 1 / 0 with assert_throws(ZeroDivisionError): raises_errors() with timelimit(.2): sleep(.01) print('finished') tic = time() lim = .2 with assert_throws(Timeout): with timelimit(lim): sleep(lim + 1) toc = time() took = toc - tic # make sure we wait at least `lim`. assert took > lim # make sure that there isn't too much overhead assert abs(lim - took) < 0.001, abs(lim - took) print('decorator tests: pass')
def tests_basics(): m = MultiMap() m["a","b","c"] = 10 m["a","b'","c"] = 12 m["a","b","d"] = 13 m["a", 14,frozenset(),None,()] = 13 # ragged dimensions allowed (YMMV), dims can have mixed types from arsenal.assertions import assert_throws with assert_throws(AssertionError): m["a",:,"d"] = 13 with assert_throws(AssertionError): print(m["a",:10,"d"]) assert m["a",:,"c"] == MultiMap({('a', 'b', 'c'): 10, ('a', "b'", 'c'): 12}) assert m[:,:,"d"] == MultiMap({('a', 'b', 'd'): 13}) print(m) # Note: This does not grab prefixes, # print(m["a",:,:]) # Annoying corner cases for when we don't pass a tuple to get/set items m = MultiMap() m[1] = 2 assert m[1,] == m[1] == m m[1,] = 2 assert m[1,] == m[1] == m assert list(m) == [(1,)]
def test_retry(): class NotCalledEnough(Exception): pass class TroublsomeFunction(object): "Function-like object which must be called >=4 times before succeeding." def __init__(self): self.tries = 0 def __call__(self, *args): self.tries += 1 if self.tries > 4: return True else: raise NotCalledEnough f = TroublsomeFunction() assert retry_apply(f, (1, 2, 3), tries=5) assert f.tries == 5 with assert_throws(NotCalledEnough): f = TroublsomeFunction() print(retry_apply(f, (10, ), tries=2)) def create_trouble(tries_needs, attempts): calls = [] @retry(tries=attempts, pause=0.0) def troublesome(a, kw=None): "I'm a troublesome function!" assert a == 'A' and kw == 'KW' calls.append(1) if len(calls) < tries_needs: raise NotCalledEnough else: return 'the secret' assert troublesome.__doc__ == "I'm a troublesome function!" assert troublesome.__name__ == 'troublesome' troublesome('A', kw='KW') create_trouble(4, 4) with assert_throws(NotCalledEnough): create_trouble(4, 2) @retry(tries=4, pause=0.0) def broken_function(): raise NotImplementedError with assert_throws(NotImplementedError): broken_function() print('retry tests: pass')
def test_timed(): @timelimit(1.0) def sleepy_function(x): time.sleep(x) with assert_throws(TimeoutError): sleepy_function(3.0) sleepy_function(0.2) @timelimit(1) def raises_errors(): return 1/0 with assert_throws(ZeroDivisionError): raises_errors()
def test_retry(): class NotCalledEnough(Exception): pass class TroublsomeFunction(object): "Function-like object which must be called >=4 times before succeeding." def __init__(self): self.tries = 0 def __call__(self, *args): self.tries += 1 if self.tries > 4: return True else: raise NotCalledEnough f = TroublsomeFunction() assert retry_apply(f, (1,2,3), tries=5) assert f.tries == 5 with assert_throws(NotCalledEnough): f = TroublsomeFunction() print(retry_apply(f, (10,), tries=2)) def create_trouble(tries_needs, attempts): calls = [] @retry(tries=attempts, pause=0.0) def troublesome(a, kw=None): "I'm a troublesome function!" assert a == 'A' and kw == 'KW' calls.append(1) if len(calls) < tries_needs: raise NotCalledEnough else: return 'the secret' assert troublesome.__doc__ == "I'm a troublesome function!" assert troublesome.__name__ == 'troublesome' troublesome('A', kw='KW') create_trouble(4, 4) with assert_throws(NotCalledEnough): create_trouble(4, 2) @retry(tries=4, pause=0.0) def broken_function(): raise NotImplementedError with assert_throws(NotImplementedError): broken_function()
def test(): agg = PlusEquals() agg.increment(3, +1) assert agg.value == 3 agg.increment(3, -1) assert agg.value is None agg += agg assert agg.value is None agg2 = PlusEquals() agg2.increment(2, 5) assert agg2.value == 10 agg2 += agg assert agg2.value == 10 agg2.increment(4, 5) assert agg2.value == 10 + 20 agg2.increment(4, -3) assert agg2.value == 10 + 20 - 12 from arsenal.assertions import assert_throws with assert_throws(AggregatorValueError): deref_value('test', MaxEquals({1: -1})) print('aggregator tests: pass')
def tests(): from arsenal.assertions import assert_throws from time import sleep # Check for error with assert_throws(ValueError): list(iterview((None for _ in range(5)))) # won't throw an error because length is passed in list(iterview((None for _ in range(5)), length=5)) for _ in iterview(range(10000), msg='foo', mintime=0.25): sleep(0.0001) # Print time elapsed if we terminate earlier than expected. for i in iterview(range(10000), msg='foo', mintime=0.25): if i == 2000: break sleep(0.001) from arsenal import terminal print('should disappear', terminal.arrow.down) # Print time elapsed if we terminate earlier than expected. for i in iterview(range(10000), msg='foo', mintime=0.25, transient=True): if i == 2000: break sleep(0.001) print('should disappear', terminal.arrow.up)
def tests(): from arsenal.assertions import assert_throws from time import sleep # Check for error with assert_throws(AssertionError): list(iterview((None for _ in range(5)))) # won't throw an error because length is passed in list(iterview((None for _ in range(5)), length=5)) for _ in iterview(range(400), every=20): sleep(0.005) for _ in iterview(range(10000), msg='foo', mintime=0.25): sleep(0.0001) # Print time elapsed if we terminate earlier than expected. for i in iterview(range(10000), msg='foo', mintime=0.25): if i == 2000: break sleep(0.001)