Example #1
0
def testSubscribe():
    topicName = 'testSubscribe'
    def proto(a, b, c=None): pass
    topicMgr.getOrCreateTopic(topicName, proto)

    def listener(a, b, c=None): pass
    # verify that pub.isValid() works too
    pub.validate(listener, topicName)
    assert pub.isValid(listener, topicName)

    assert topicMgr.getTopic(topicName).getNumListeners() == 0
    assert topicMgr.getTopicsSubscribed(listener) == []
    assert not pub.isSubscribed(listener, topicName)
    assert pub.subscribe(listener, topicName)
    assert pub.isSubscribed(listener, topicName)
    def topicNames(listener):
        return [t.getName() for t in topicMgr.getTopicsSubscribed(listener)]
    assert topicNames(listener) == [topicName]
    # should do nothing if already subscribed:
    assert not pub.subscribe(listener, topicName)[1]
    assert topicMgr.getTopic(topicName).getNumListeners() == 1

    # test topicMgr.getTopicsSubscribed()
    pub.subscribe(listener, 'lt2', )
    assert set(topicNames(listener)) == set([topicName,'lt2'])
    pub.subscribe(listener, 'lt1.lst1')
    assert set(topicNames(listener)) == set([topicName,'lt2','lt1.lst1'])

    # test ALL_TOPICS
    def listenToAll(): pass
    pub.subscribe(listenToAll, pub.ALL_TOPICS)
    assert topicNames(listenToAll) == [pub.ALL_TOPICS]
Example #2
0
def testDOAListenerPubsub():
    # Verify that a 'temporary' listener (one that will be garbage collected
    # as soon as subscribe() returns because there are no strong references to
    # it) gets immediately unregistered

    def listener(): pass
    class Wrapper:
        def __init__(self, func):
            self.func = func
        def __call__(self):
            pass

    pub.subscribe( Wrapper(listener), 'testDOAListenerPubsub')
    gc.collect() # for pypy: the gc doesn't work the same as cpython's
    assert not topicMgr.getTopic('testDOAListenerPubsub').hasListeners()
    assert pub.isValid(listener, 'testDOAListenerPubsub')
Example #3
0
def test_string_prov_export():
    clear_topic_tree()

    importStr = '''
        """Tree docs, can be anything you want."""

        class root_topic_1:
            """Root topic 1."""

            class subtopic_1:
                """
                Sub topic 1 of root topic. Docs rely on one
                blank line for topic doc, and indentation for
                each argument doc.
                """

                def msgDataSpec(arg1, arg2=None):
                    """
                    - arg1: some multiline doc
                        for arg1
                    - arg2: some multiline doc
                        for arg2
                    """
                    pass

        class root_topic_2:
            """Root topic 2."""

        '''
    pub.clearTopicDefnProviders()
    provider = pub.addTopicDefnProvider(importStr, pub.TOPIC_TREE_FROM_STRING)
    treeDoc = provider.getTreeDoc()
    assert treeDoc == """Tree docs, can be anything you want."""
    root = topicMgr.getOrCreateTopic('root_topic_1.subtopic_1')
    assert root is not None
    assert topicMgr.getOrCreateTopic('root_topic_2').hasMDS()

    # few sanity checks
    def sub_1(arg1, arg2=None):
        pass

    assert root.hasMDS()
    assert pub.isValid(sub_1, 'root_topic_1.subtopic_1')

    # export tree
    exported = pub.exportTopicTreeSpec(rootTopic='root_topic_1',
                                       moduleDoc=treeDoc)
    # print(exported)

    expectExport = '''\
        # Automatically generated by TopicTreeSpecPrinter(**kwargs).
        # The kwargs were:
        # - fileObj: StringIO
        # - footer: '# End of topic tree definition. Note that application may l...'
        # - indentStep: 4
        # - treeDoc: 'Tree docs, can be anything you want....'
        # - width: 70


        """
        Tree docs, can be anything you want.
        """


        class root_topic_1:
            """
            Root topic 1.
            """

            class subtopic_1:
                """
                Sub topic 1 of root topic. Docs rely on one
                blank line for topic doc, and indentation for
                each argument doc.
                """

                def msgDataSpec(arg1, arg2=None):
                    """
                    - arg1: some multiline doc
                        for arg1
                    - arg2: some multiline doc
                        for arg2
                    """


        # End of topic tree definition. Note that application may load
        # more than one definitions provider.
        '''

    # check there are no differences
    from difflib import context_diff, ndiff
    diffs = ndiff(dedent(expectExport).splitlines(), exported.splitlines())
    diffs = [d for d in diffs if not d.startswith(' ')]
    assert diffs == ['- ', '+         ']

    # now for module:
    provider = pub.addTopicDefnProvider('test4_prov_module_expect')
    pub.instantiateAllDefinedTopics(provider)
    modDoc = provider.getTreeDoc()
    assert modDoc.startswith('\nTree docs, can be anything you')
    pub.exportTopicTreeSpec('test4_prov_module_actual',
                            rootTopic='root_topic_1b',
                            moduleDoc=treeDoc)
    lines1 = open('test4_prov_module_actual.py', 'r').readlines()
    lines2 = open('test4_prov_module_expect.py', 'r').readlines()
    diffs = ndiff(lines1, lines2)
    diffs = [d for d in diffs if not d.startswith(' ')]
    assert not list(diffs) or list(diffs) == [
        '- # - fileObj: TextIOWrapper\n', '+ # - fileObj: file\n'
    ]
    Path('test4_prov_module_actual.py').unlink()
Example #4
0
def test_string_prov_export():
    clear_topic_tree()

    importStr = '''
        """Tree docs, can be anything you want."""

        class root_topic_1:
            """Root topic 1."""

            class subtopic_1:
                """
                Sub topic 1 of root topic. Docs rely on one
                blank line for topic doc, and indentation for
                each argument doc.
                """

                def msgDataSpec(arg1, arg2=None):
                    """
                    - arg1: some multiline doc
                        for arg1
                    - arg2: some multiline doc
                        for arg2
                    """
                    pass

        class root_topic_2:
            """Root topic 2."""

        '''
    pub.clearTopicDefnProviders()
    provider = pub.addTopicDefnProvider(importStr, pub.TOPIC_TREE_FROM_STRING)
    treeDoc = provider.getTreeDoc()
    assert treeDoc == """Tree docs, can be anything you want."""
    root = topicMgr.getOrCreateTopic('root_topic_1.subtopic_1')
    assert root is not None
    assert topicMgr.getOrCreateTopic('root_topic_2').hasMDS()

    # few sanity checks
    def sub_1(arg1, arg2=None): pass

    assert root.hasMDS()
    assert pub.isValid(sub_1, 'root_topic_1.subtopic_1')

    # export tree
    exported = pub.exportTopicTreeSpec(rootTopic='root_topic_1', moduleDoc=treeDoc)
    # print(exported)

    expectExport = '''\
        # Automatically generated by TopicTreeSpecPrinter(**kwargs).
        # The kwargs were:
        # - fileObj: StringIO
        # - footer: '# End of topic tree definition. Note that application may l...'
        # - indentStep: 4
        # - treeDoc: 'Tree docs, can be anything you want....'
        # - width: 70


        """
        Tree docs, can be anything you want.
        """


        class root_topic_1:
            """
            Root topic 1.
            """

            class subtopic_1:
                """
                Sub topic 1 of root topic. Docs rely on one
                blank line for topic doc, and indentation for
                each argument doc.
                """

                def msgDataSpec(arg1, arg2=None):
                    """
                    - arg1: some multiline doc
                        for arg1
                    - arg2: some multiline doc
                        for arg2
                    """


        # End of topic tree definition. Note that application may load
        # more than one definitions provider.
        '''

    # check there are no differences
    from difflib import context_diff, ndiff
    diffs = ndiff(dedent(expectExport).splitlines(), exported.splitlines())
    diffs = [d for d in diffs if not d.startswith(' ')]
    assert diffs == ['- ', '+         ']

    # now for module:
    provider = pub.addTopicDefnProvider('test4_prov_module_expect')
    pub.instantiateAllDefinedTopics(provider)
    modDoc = provider.getTreeDoc()
    assert modDoc.startswith('\nTree docs, can be anything you')
    pub.exportTopicTreeSpec('test4_prov_module_actual',
                            rootTopic='root_topic_1b', moduleDoc=treeDoc)
    lines1 = open('test4_prov_module_actual.py', 'r').readlines()
    lines2 = open('test4_prov_module_expect.py', 'r').readlines()
    diffs = ndiff(lines1, lines2)
    diffs = [d for d in diffs if not d.startswith(' ')]
    assert not list(diffs) or list(diffs) == ['- # - fileObj: TextIOWrapper\n', '+ # - fileObj: file\n']
    Path('test4_prov_module_actual.py').unlink()