Ejemplo n.º 1
0
def setup_lookups():
    content1 = InstanceContent()
    lookup1 = GenericLookup(content1)
    content2 = InstanceContent()
    lookup2 = GenericLookup(content2)

    proxy_lookup = ProxyLookup(lookup1)

    return content1, lookup1, content2, lookup2, proxy_lookup
Ejemplo n.º 2
0
def setup_lookups():
    content1 = InstanceContent()
    lookup1 = GenericLookup(content1)
    content2 = InstanceContent()
    lookup2 = GenericLookup(content2)

    provider = Provider(lookup1)
    delegated_lookup = DelegatedLookup(provider)
    provider.to_notify = delegated_lookup

    return content1, lookup1, content2, lookup2, provider, delegated_lookup
Ejemplo n.º 3
0
def test_convertor_remove():
    members = [object, parent, child, other, parent]
    content = InstanceContent()
    lookup = GenericLookup(content)

    keys_to_members, members_to_keys = make_convertor_maps(members)
    convertor = MyConvertor(keys_to_members)
    content.set(keys_to_members.keys(), convertor=convertor)

    for key in keys_to_members.keys():
        try:
            content.remove(key, convertor=convertor)
        except KeyError:
            print('Could not remove', key)

    assert lookup._storage
    assert lookup._storage._content == set()

    def check_in_storage(obj):
        for pair in lookup._storage._content:
            if pair.get_instance() == obj:
                return True
        else:
            return False

    for member in members:
        assert not check_in_storage(member)
Ejemplo n.º 4
0
def test_remove_before_attach():
    members = [object, parent, child, other, parent]
    content = InstanceContent()
    content.set(members)

    for member in members:
        content.remove(member)

    lookup = GenericLookup(content)

    assert not lookup._storage
Ejemplo n.º 5
0
def test_set_after_attach():
    members = [object, parent, child, other, parent]
    content = InstanceContent()
    lookup = GenericLookup(content)

    content.set(members)

    assert lookup._storage
    assert lookup._storage._content

    def check_in_storage(obj):
        for pair in lookup._storage._content:
            if pair.get_instance() == obj:
                return True
        else:
            return False

    for member in members:
        assert check_in_storage(member)
Ejemplo n.º 6
0
def test_convertor_set():
    members = [object, parent, child, other, parent]
    content = InstanceContent()
    lookup = GenericLookup(content)

    keys_to_members, members_to_keys = make_convertor_maps(members)
    convertor = MyConvertor(keys_to_members)

    content.set(keys_to_members.keys(), convertor=convertor)

    assert lookup._storage
    assert lookup._storage._content

    def check_in_storage(obj):
        for pair in lookup._storage._content:
            if pair.get_instance() == obj:
                return True
        else:
            return False

    for member in members:
        assert check_in_storage(member)
Ejemplo n.º 7
0
def test_remove_after_attach():
    members = [object, parent, child, other, parent]
    content = InstanceContent()
    lookup = GenericLookup(content)
    content.set(members)

    for member in members:
        try:
            content.remove(member)
        except KeyError:
            print('Could not remove', member)

    assert lookup._storage
    assert lookup._storage._content == set()

    def check_in_storage(obj):
        for pair in lookup._storage._content:
            if pair.get_instance() == obj:
                return True
        else:
            return False

    for member in members:
        assert not check_in_storage(member)
Ejemplo n.º 8
0
def test_multiple_results_with_executor(request):
    executor = ThreadPoolExecutor()
    request.addfinalizer(executor.shutdown)
    content = InstanceContent(notify_in=executor)
    lookup = GenericLookup(content)
    test_thread = threading.current_thread()

    result_object = lookup.lookup_result(object)
    result_parent = lookup.lookup_result(TestParentObject)
    result_child = lookup.lookup_result(TestChildObject)
    result_other = lookup.lookup_result(TestOtherObject)
    called_events = {
        object: threading.Event(),
        TestParentObject: threading.Event(),
        TestChildObject: threading.Event(),
        TestOtherObject: threading.Event(),
    }

    def call_me_back(result):
        called_with[result._cls] = result
        called_in_thread[result._cls] = threading.current_thread()
        print('Got called', result)
        called_events[result._cls].set()

    called_with = {}
    called_in_thread = {}
    result_object.add_lookup_listener(call_me_back)
    result_parent.add_lookup_listener(call_me_back)
    result_child.add_lookup_listener(call_me_back)
    result_other.add_lookup_listener(call_me_back)

    members = [obj, obj, obj2, parent]

    def check_for_a_class(member, added, cls, result_cls):
        if isinstance(member, cls):
            if added:
                assert called_events[cls].wait(
                    THREAD_WAITING_TIME
                ), 'Timeout waiting for callback to be called'
                called_events[cls].clear()
                assert cls in called_with
                assert called_with[cls] == result_cls
                assert called_in_thread[cls]
                assert called_in_thread[cls] != test_thread
                del called_with[cls]
                del called_in_thread[cls]
            else:
                assert not called_events[cls].wait(
                    THREAD_WAITING_TIME
                ), 'Callback got called when it should not'
                assert cls not in called_with
                assert cls not in called_in_thread
        else:
            assert not called_events[cls].wait(
                THREAD_WAITING_TIME), 'Callback got called when it should not'
            assert cls not in called_with
            assert cls not in called_in_thread

    # Adding members

    for member in members:
        print('Adding', member)
        added = content.add(member)
        check_for_a_class(member, added, object, result_object)
        check_for_a_class(member, added, TestParentObject, result_parent)
        check_for_a_class(member, added, TestChildObject, result_child)
        check_for_a_class(member, added, TestOtherObject, result_other)
        assert not called_with
        assert not called_in_thread

    # Removing members

    for member in members:
        print('Removing', member)
        try:
            content.remove(member)
        except KeyError:
            continue
        else:
            check_for_a_class(member, True, object, result_object)
            check_for_a_class(member, True, TestParentObject, result_parent)
            check_for_a_class(member, True, TestChildObject, result_child)
            check_for_a_class(member, True, TestOtherObject, result_other)
            assert not called_with
            assert not called_in_thread

    # Removing listener and adding/removing members

    result_object.remove_lookup_listener(call_me_back)
    result_parent.remove_lookup_listener(call_me_back)
    result_child.remove_lookup_listener(call_me_back)
    result_other.remove_lookup_listener(call_me_back)

    for member in members:
        print('Adding', member)
        content.add(member)
        assert not called_with

        print('Removing', member)
        try:
            content.remove(member)
        except KeyError:
            continue
        else:
            assert not called_events[type(member)].wait(
                THREAD_WAITING_TIME), 'Callback got called when it should not'
            assert not called_with
            assert not called_in_thread
Ejemplo n.º 9
0
def setup_lookup(members, convertor=None):
    content = InstanceContent()
    lookup = GenericLookup(content)
    content.set(members, convertor=convertor)

    return content, lookup
Ejemplo n.º 10
0
def test_listener_with_executor(members, search, expected, request):
    print(members, search, expected)
    expected = list(expected) if expected is not None else []
    executor = ThreadPoolExecutor()
    request.addfinalizer(executor.shutdown)
    content = InstanceContent(notify_in=executor)
    lookup = GenericLookup(content)
    test_thread = threading.current_thread()
    called_event = threading.Event()

    result = lookup.lookup_result(search)
    assert not result.all_items()

    def call_me_back(result):
        nonlocal called_with, called_in_thread
        called_with = result
        called_in_thread = threading.current_thread()
        print('Got called', result)
        called_event.set()

    called_with = None
    called_in_thread = None
    result.add_lookup_listener(call_me_back)

    # Adding members

    for member in members:
        print('Adding', member)
        added = content.add(member)
        if member in expected:
            if added:
                assert called_event.wait(
                    THREAD_WAITING_TIME
                ), 'Timeout waiting for callback to be called'
                called_event.clear()
                assert called_with
                assert member in result.all_instances()
                assert member in called_with.all_instances()
                assert called_in_thread
                assert called_in_thread != test_thread
                called_with = None
                called_in_thread = None
            else:
                assert not called_event.wait(
                    THREAD_WAITING_TIME
                ), 'Callback got called when it should not'
                assert called_with is None
                assert called_in_thread is None
                assert member in result.all_instances()
        else:
            assert not called_event.wait(
                THREAD_WAITING_TIME), 'Callback got called when it should not'
            assert called_with is None
            assert called_in_thread is None

    # Removing members

    for member in members:
        print('Removing', member)
        try:
            content.remove(member)
        except KeyError:
            continue
        else:
            if member in expected:
                assert called_event.wait(
                    THREAD_WAITING_TIME
                ), 'Timeout waiting for callback to be called'
                called_event.clear()
                assert called_with
                assert member not in result.all_instances()
                assert member not in called_with.all_instances()
                assert called_in_thread
                assert called_in_thread != test_thread
                called_with = None
                called_in_thread = None
            else:
                assert not called_event.wait(
                    THREAD_WAITING_TIME
                ), 'Callback got called when it should not'
                assert called_with is None
                assert called_in_thread is None

    # Removing listener and adding/removing members

    result.remove_lookup_listener(call_me_back)

    for member in members:
        print('Adding', member)
        content.add(member)
        assert called_with is None

        print('Removing', member)
        try:
            content.remove(member)
        except KeyError:
            continue
        else:
            assert not called_event.wait(
                THREAD_WAITING_TIME), 'Callback got called when it should not'
            assert called_with is None
            assert called_in_thread is None
Ejemplo n.º 11
0
def test_cannot_reuse_content():
    content, lookup = setup_lookup([])

    with pytest.raises(RuntimeError):
        GenericLookup(content)
Ejemplo n.º 12
0
def test_instantiation():
    assert InstanceContent()
    assert GenericLookup(InstanceContent())