Example #1
0
def test_weakref_event_emitter_cb(disconnect_and_should_be_none):
    """

    Note that as above but with pure callback, We keep a reference to it, the
    reason is that unlike with bound method, the callback may be a closure and
    may not stick around.

    We thus expect the wekref to be None only if explicitely disconnected

    """
    e = EventEmitter(type='test_weak')

    def cb(self):
        pass

    ref_cb = weakref.ref(cb)

    e.connect(cb)

    with e.blocker(cb):
        e()

    if disconnect_and_should_be_none:
        e.disconnect(cb)
        del cb
        assert ref_cb() is None
    else:
        del cb
        assert ref_cb() is not None
Example #2
0
def test_disconnect_object():
    count_list = []

    def fun1():
        count_list.append(1)

    class TestOb:
        call_list_1 = []
        call_list_2 = []

        def fun1(self):
            self.call_list_1.append(1)

        def fun2(self):
            self.call_list_2.append(1)

    t = TestOb()

    e = EventEmitter(type="test")
    e.connect(t.fun1)
    e.connect(t.fun2)
    e.connect(fun1)
    e()

    assert t.call_list_1 == [1]
    assert t.call_list_2 == [1]
    assert count_list == [1]

    e.disconnect(t)
    e()

    assert t.call_list_1 == [1]
    assert t.call_list_2 == [1]
    assert count_list == [1, 1]
Example #3
0
def test_none_disconnect():
    count_list = []

    def fun1():
        count_list.append(1)

    def fun2(event):
        count_list.append(2)

    e = EventEmitter(type="test")
    e.connect(fun1)
    e()
    assert count_list == [1]
    e.disconnect(None)
    e()
    assert count_list == [1]
    e.connect(fun2)
    e()
    assert count_list == [1, 2]
Example #4
0
def test_weakref_disconnect():
    class TestOb:
        call_list_1 = []

        def fun1(self):
            self.call_list_1.append(1)

        def fun2(self, event):
            self.call_list_1.append(2)

    t = TestOb()

    e = EventEmitter(type="test")
    e.connect(t.fun1)
    e()

    assert t.call_list_1 == [1]
    e.disconnect((weakref.ref(t), "fun1"))
    e()
    assert t.call_list_1 == [1]
    e.connect(t.fun2)
    e()
    assert t.call_list_1 == [1, 2]