def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c')
def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c')
def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f)
def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2]
def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c')
def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3]
def test_signal_weakref(): """ Test that signals handlers of methods are deleted when their objects get collected """ import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c')
def test_async(): from threading import get_ident main = get_ident() @on_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_test.register() def a2(): assert get_ident() == main on_test() on_test.asynchronous = True @on_test.register() # follows the current setting def a3(): assert get_ident() != main on_test() on_test.asynchronous = False