Exemplo n.º 1
0
    def test_Registrations(self):
        import rosmaster.exceptions
        from rosmaster.registrations import Registrations
        types = [Registrations.TOPIC_SUBSCRIPTIONS,
                 Registrations.TOPIC_PUBLICATIONS,
                 Registrations.SERVICE,
                 Registrations.PARAM_SUBSCRIPTIONS]
        # test enums
        self.assertEquals(4, len(set(types)))
        try:
            r = Registrations(-1)
            self.fail("Registrations accepted invalid type")
        except rosmaster.exceptions.InternalException: pass
        
        for t in types:
            r = Registrations(t)
            self.assertEquals(t, r.type)
            self.assert_(not r) #test nonzero
            self.failIf('topic1' in r) #test contains            
            self.failIf(r.has_key('topic1')) #test has_key
            self.failIf([k for k in r.iterkeys()]) #no keys
            self.assertEquals(None, r.get_service_api('non-existent'))

        # Test topic subs
        r = Registrations(Registrations.TOPIC_SUBSCRIPTIONS)
        self._subtest_Registrations_basic(r)
        r = Registrations(Registrations.TOPIC_PUBLICATIONS)        
        self._subtest_Registrations_basic(r)
        r = Registrations(Registrations.PARAM_SUBSCRIPTIONS)        
        self._subtest_Registrations_basic(r)

        r = Registrations(Registrations.SERVICE)        
        self._subtest_Registrations_services(r)
Exemplo n.º 2
0
    def test_compute_param_updates(self):
        from rosmaster.registrations import Registrations
        from rosmaster.paramserver import compute_param_updates
        # spec requires that subscriptions always have a trailing slash
        tests = [
            # [correct val], (subscribers, param_key, param_value)
            ([],({}, '/foo', 1)),
            ([],({'/bar': 'barapi'}, '/foo/', 1)),
            ([],({'/bar/': 'barapi'}, '/foo/', 1)),
            
            # make sure that it's robust to aliases
            ([('fooapi', '/foo/', 1)], ({'/foo/': 'fooapi'}, '/foo', 1)),
            ([('fooapi', '/foo/', 1)], ({'/foo/': 'fooapi'}, '/foo/', 1)),
            
            # check namespace subscription
            ([('fooapi', '/foo/val/', 1)], ({'/foo/': 'fooapi'}, '/foo/val', 1)),

            # check against dictionary param values
            ([],({'/bar/': 'barapi'}, '/foo/', {'bar': 2})),
            ([('fooapi', '/foo/val/', 1)], ({'/foo/val/': 'fooapi'}, '/foo', {'val' : 1})),

            ([('fooapi', '/foo/bar/val/', 1)], ({'/foo/bar/val/': 'fooapi'}, '/foo', {'bar' : {'val' : 1}})),            
            ([('fooapi', '/foo/bar/', {'val': 1})], ({'/foo/bar/': 'fooapi'}, '/foo', {'bar' : {'val' : 1}})),
            ([('fooapi', '/foo/', {'bar':{'val': 1}})], ({'/foo/': 'fooapi'}, '/foo', {'bar' : {'val' : 1}})),

            ([('fooapi', '/foo/', {'bar': 1, 'baz': 2}), ('foobazapi', '/foo/baz/', 2)],
             ({'/foo/': 'fooapi', '/foo/baz/': 'foobazapi'}, '/foo', {'bar' : 1, 'baz': 2})),

            ([('foobarapi', '/foo/bar/', 1), ('foobazapi', '/foo/baz/', 2)],
             ({'/foo/bar/': 'foobarapi', '/foo/baz/': 'foobazapi'}, '/foo', {'bar' : 1, 'baz': 2})),

            # deletion of higher level tree
            ([('delapi', '/del/bar/', {})],
             ({'/del/bar/': 'delapi'}, '/del', {})),

            ]
        for correct, args in tests:
            reg = Registrations(Registrations.PARAM_SUBSCRIPTIONS)
            reg.map = args[0]
            param_key = args[1]
            param_val = args[2]

            val = compute_param_updates(reg, param_key, param_val)
            self.assertEquals(len(correct), len(val), "Failed: \n%s \nreturned \n%s\nvs correct\n%s"%(str(args), str(val), str(correct)))
            for c in correct:
                self.assert_(c in val, "Failed: \n%s \ndid not include \n%s. \nIt returned \n%s"%(str(args), c, val))
 def test_Registrations(self):
     import rosmaster.exceptions
     from rosmaster.registrations import Registrations
     types = [
         Registrations.TOPIC_SUBSCRIPTIONS,
         Registrations.TOPIC_PUBLICATIONS, Registrations.SERVICE,
         Registrations.PARAM_SUBSCRIPTIONS
     ]
     # test enums
     self.assertEquals(4, len(set(types)))
     try:
         r = Registrations(-1)
         self.fail("Registrations accepted invalid type")
     except rosmaster.exceptions.InternalException, e:
         pass
Exemplo n.º 4
0
    def test_Registrations_unregister_all(self):
        import rosmaster.exceptions
        from rosmaster.registrations import Registrations

        r = Registrations(Registrations.TOPIC_SUBSCRIPTIONS)        
        for k in ['topic1', 'topic1b', 'topic1c', 'topic1d']:        
            r.register(k, 'node1', 'http://node1:5678')
        r.register('topic2', 'node2', 'http://node2:5678')
        r.unregister_all('node1')
        self.failIf(not r)
        for k in ['topic1', 'topic1b', 'topic1c', 'topic1d']:        
            self.failIf(r.has_key(k))
        self.assertEquals(['topic2'], [k for k in r.iterkeys()])
        
        r = Registrations(Registrations.TOPIC_PUBLICATIONS)        
        for k in ['topic1', 'topic1b', 'topic1c', 'topic1d']:        
            r.register(k, 'node1', 'http://node1:5678')
        r.register('topic2', 'node2', 'http://node2:5678')
        r.unregister_all('node1')
        self.failIf(not r)
        for k in ['topic1', 'topic1b', 'topic1c', 'topic1d']:        
            self.failIf(r.has_key(k))
        self.assertEquals(['topic2'], [k for k in r.iterkeys()])

        r = Registrations(Registrations.PARAM_SUBSCRIPTIONS)        
        r.register('param2', 'node2', 'http://node2:5678')
        for k in ['param1', 'param1b', 'param1c', 'param1d']:
            r.register(k, 'node1', 'http://node1:5678')
        r.unregister_all('node1')
        self.failIf(not r)
        for k in ['param1', 'param1b', 'param1c', 'param1d']:
            self.failIf(r.has_key(k))
        self.assertEquals(['param2'], [k for k in r.iterkeys()])
        
        r = Registrations(Registrations.SERVICE)        
        for k in ['service1', 'service1b', 'service1c', 'service1d']:
            r.register(k, 'node1', 'http://node1:5678', 'rosrpc://node1:1234')
        r.register('service2', 'node2', 'http://node2:5678', 'rosrpc://node2:1234')
        r.unregister_all('node1')
        self.failIf(not r)
        for k in ['service1', 'service1b', 'service1c', 'service1d']:
            self.failIf(r.has_key(k))
            self.assertEquals(None, r.get_service_api(k))
        self.assertEquals(['service2'], [k for k in r.iterkeys()])
        self.assertEquals('rosrpc://node2:1234', r.get_service_api('service2'))
    def test_Registrations_unregister_all(self):
        import rosmaster.exceptions
        from rosmaster.registrations import Registrations

        r = Registrations(Registrations.TOPIC_SUBSCRIPTIONS)
        for k in ['topic1', 'topic1b', 'topic1c', 'topic1d']:
            r.register(k, 'node1', 'http://node1:5678')
        r.register('topic2', 'node2', 'http://node2:5678')
        r.unregister_all('node1')
        self.failIf(not r)
        for k in ['topic1', 'topic1b', 'topic1c', 'topic1d']:
            self.failIf(r.has_key(k))
        self.assertEquals(['topic2'], [k for k in r.iterkeys()])

        r = Registrations(Registrations.TOPIC_PUBLICATIONS)
        for k in ['topic1', 'topic1b', 'topic1c', 'topic1d']:
            r.register(k, 'node1', 'http://node1:5678')
        r.register('topic2', 'node2', 'http://node2:5678')
        r.unregister_all('node1')
        self.failIf(not r)
        for k in ['topic1', 'topic1b', 'topic1c', 'topic1d']:
            self.failIf(r.has_key(k))
        self.assertEquals(['topic2'], [k for k in r.iterkeys()])

        r = Registrations(Registrations.PARAM_SUBSCRIPTIONS)
        r.register('param2', 'node2', 'http://node2:5678')
        for k in ['param1', 'param1b', 'param1c', 'param1d']:
            r.register(k, 'node1', 'http://node1:5678')
        r.unregister_all('node1')
        self.failIf(not r)
        for k in ['param1', 'param1b', 'param1c', 'param1d']:
            self.failIf(r.has_key(k))
        self.assertEquals(['param2'], [k for k in r.iterkeys()])

        r = Registrations(Registrations.SERVICE)
        for k in ['service1', 'service1b', 'service1c', 'service1d']:
            r.register(k, 'node1', 'http://node1:5678', 'rosrpc://node1:1234')
        r.register('service2', 'node2', 'http://node2:5678',
                   'rosrpc://node2:1234')
        r.unregister_all('node1')
        self.failIf(not r)
        for k in ['service1', 'service1b', 'service1c', 'service1d']:
            self.failIf(r.has_key(k))
            self.assertEquals(None, r.get_service_api(k))
        self.assertEquals(['service2'], [k for k in r.iterkeys()])
        self.assertEquals('rosrpc://node2:1234', r.get_service_api('service2'))
class TestRosmasterRegistrations(unittest.TestCase):
    def test_NodeRef_services(self):
        from rosmaster.registrations import NodeRef, Registrations
        n = NodeRef('n1', 'http://localhost:1234')
        # test services
        n.add(Registrations.SERVICE, 'add_two_ints')
        self.failIf(n.is_empty())
        self.assert_('add_two_ints' in n.services)
        self.assertEquals(['add_two_ints'], n.services)

        n.add(Registrations.SERVICE, 'add_three_ints')
        self.failIf(n.is_empty())
        self.assert_('add_three_ints' in n.services)
        self.assert_('add_two_ints' in n.services)

        n.remove(Registrations.SERVICE, 'add_two_ints')
        self.assert_('add_three_ints' in n.services)
        self.assertEquals(['add_three_ints'], n.services)
        self.failIf('add_two_ints' in n.services)
        self.failIf(n.is_empty())

        n.remove(Registrations.SERVICE, 'add_three_ints')
        self.failIf('add_three_ints' in n.services)
        self.failIf('add_two_ints' in n.services)
        self.assertEquals([], n.services)
        self.assert_(n.is_empty())

    def test_NodeRef_subs(self):
        from rosmaster.registrations import NodeRef, Registrations
        n = NodeRef('n1', 'http://localhost:1234')
        # test topic suscriptions
        n.add(Registrations.TOPIC_SUBSCRIPTIONS, 'topic1')
        self.failIf(n.is_empty())
        self.assert_('topic1' in n.topic_subscriptions)
        self.assertEquals(['topic1'], n.topic_subscriptions)

        n.add(Registrations.TOPIC_SUBSCRIPTIONS, 'topic2')
        self.failIf(n.is_empty())
        self.assert_('topic2' in n.topic_subscriptions)
        self.assert_('topic1' in n.topic_subscriptions)

        n.remove(Registrations.TOPIC_SUBSCRIPTIONS, 'topic1')
        self.assert_('topic2' in n.topic_subscriptions)
        self.assertEquals(['topic2'], n.topic_subscriptions)
        self.failIf('topic1' in n.topic_subscriptions)
        self.failIf(n.is_empty())

        n.remove(Registrations.TOPIC_SUBSCRIPTIONS, 'topic2')
        self.failIf('topic2' in n.topic_subscriptions)
        self.failIf('topic1' in n.topic_subscriptions)
        self.assertEquals([], n.topic_subscriptions)
        self.assert_(n.is_empty())

    def test_NodeRef_pubs(self):
        from rosmaster.registrations import NodeRef, Registrations
        n = NodeRef('n1', 'http://localhost:1234')
        # test topic publications
        n.add(Registrations.TOPIC_PUBLICATIONS, 'topic1')
        self.failIf(n.is_empty())
        self.assert_('topic1' in n.topic_publications)
        self.assertEquals(['topic1'], n.topic_publications)

        n.add(Registrations.TOPIC_PUBLICATIONS, 'topic2')
        self.failIf(n.is_empty())
        self.assert_('topic2' in n.topic_publications)
        self.assert_('topic1' in n.topic_publications)

        n.remove(Registrations.TOPIC_PUBLICATIONS, 'topic1')
        self.assert_('topic2' in n.topic_publications)
        self.assertEquals(['topic2'], n.topic_publications)
        self.failIf('topic1' in n.topic_publications)
        self.failIf(n.is_empty())

        n.remove(Registrations.TOPIC_PUBLICATIONS, 'topic2')
        self.failIf('topic2' in n.topic_publications)
        self.failIf('topic1' in n.topic_publications)
        self.assertEquals([], n.topic_publications)
        self.assert_(n.is_empty())

    def test_NodeRef_base(self):
        import rosmaster.exceptions
        from rosmaster.registrations import NodeRef, Registrations
        n = NodeRef('n1', 'http://localhost:1234')
        self.assertEquals('http://localhost:1234', n.api)
        self.assertEquals([], n.param_subscriptions)
        self.assertEquals([], n.topic_subscriptions)
        self.assertEquals([], n.topic_publications)
        self.assertEquals([], n.services)
        self.assert_(n.is_empty())

        try:
            n.add(12345, 'topic')
            self.fail("should have failed with invalid type")
        except rosmaster.exceptions.InternalException:
            pass
        try:
            n.remove(12345, 'topic')
            self.fail("should have failed with invalid type")
        except rosmaster.exceptions.InternalException:
            pass

        n.add(Registrations.TOPIC_PUBLICATIONS, 'topic1')
        n.add(Registrations.TOPIC_PUBLICATIONS, 'topic2')
        n.add(Registrations.TOPIC_SUBSCRIPTIONS, 'topic2')
        n.add(Registrations.TOPIC_SUBSCRIPTIONS, 'topic3')
        n.add(Registrations.PARAM_SUBSCRIPTIONS, 'topic4')
        n.add(Registrations.SERVICE, 'serv')
        self.failIf(n.is_empty())

        n.clear()
        self.assert_(n.is_empty())

    def test_NodeRef_param_subs(self):
        from rosmaster.registrations import NodeRef, Registrations
        n = NodeRef('n1', 'http://localhost:1234')
        # test param suscriptions
        n.add(Registrations.PARAM_SUBSCRIPTIONS, 'param1')
        self.failIf(n.is_empty())
        self.assert_('param1' in n.param_subscriptions)
        self.assertEquals(['param1'], n.param_subscriptions)

        n.add(Registrations.PARAM_SUBSCRIPTIONS, 'param2')
        self.failIf(n.is_empty())
        self.assert_('param2' in n.param_subscriptions)
        self.assert_('param1' in n.param_subscriptions)

        n.remove(Registrations.PARAM_SUBSCRIPTIONS, 'param1')
        self.assert_('param2' in n.param_subscriptions)
        self.assertEquals(['param2'], n.param_subscriptions)
        self.failIf('param1' in n.param_subscriptions)
        self.failIf(n.is_empty())

        n.remove(Registrations.PARAM_SUBSCRIPTIONS, 'param2')
        self.failIf('param2' in n.param_subscriptions)
        self.failIf('param1' in n.param_subscriptions)
        self.assertEquals([], n.param_subscriptions)
        self.assert_(n.is_empty())

    ## subroutine of registration tests that test topic/param type Reg objects
    ## @param r Registrations: initialized registrations object to test
    def _subtest_Registrations_basic(self, r):
        #NOTE: no real difference between topic and param names, so tests are reusable

        # - note that we've updated node1's API
        r.register('topic1', 'node1', 'http://node1:5678')
        self.assert_('topic1' in r)  # test contains
        self.assert_(r.has_key('topic1'))  # test contains
        self.assertEquals(['topic1'], [k for k in r.iterkeys()])
        self.assertEquals(['http://node1:5678'], r.get_apis('topic1'))
        self.assertEquals([('node1', 'http://node1:5678')], r['topic1'])
        self.failIf(not r)  #test nonzero
        self.assertEquals(
            None, r.get_service_api('topic1'))  #make sure no contamination
        self.assertEquals([['topic1', ['node1']]], r.get_state())

        r.register('topic1', 'node2', 'http://node2:5678')
        self.assertEquals(['topic1'], [k for k in r.iterkeys()])
        self.assertEquals(['topic1'], [k for k in r.iterkeys()])
        self.assertEquals(2, len(r.get_apis('topic1')))
        self.assert_('http://node1:5678' in r.get_apis('topic1'))
        self.assert_('http://node2:5678' in r.get_apis('topic1'))
        self.assertEquals(2, len(r['topic1']))
        self.assert_(('node1', 'http://node1:5678') in r['topic1'],
                     r['topic1'])
        self.assert_(('node2', 'http://node2:5678') in r['topic1'])
        self.assertEquals([['topic1', ['node1', 'node2']]], r.get_state())

        # TODO: register second topic
        r.register('topic2', 'node3', 'http://node3:5678')
        self.assert_('topic2' in r)  # test contains
        self.assert_(r.has_key('topic2'))  # test contains
        self.assert_('topic1' in [k for k in r.iterkeys()])
        self.assert_('topic2' in [k for k in r.iterkeys()])
        self.assertEquals(['http://node3:5678'], r.get_apis('topic2'))
        self.assertEquals([('node3', 'http://node3:5678')], r['topic2'])
        self.failIf(not r)  #test nonzero
        self.assert_(['topic1', ['node1', 'node2']] in r.get_state(),
                     r.get_state())
        self.assert_(['topic2', ['node3']] in r.get_state(), r.get_state())

        # Unregister

        # - fail if node is not registered
        code, _, val = r.unregister('topic1', 'node3', 'http://node3:5678')
        self.assertEquals(0, val)
        # - fail if topic is not registered by that node
        code, _, val = r.unregister('topic2', 'node2', 'http://node2:5678')
        self.assertEquals(0, val)
        # - fail if URI does not match
        code, _, val = r.unregister('topic2', 'node2', 'http://fakenode2:5678')
        self.assertEquals(0, val)

        # - unregister node2
        code, _, val = r.unregister('topic1', 'node1', 'http://node1:5678')
        self.assertEquals(1, code)
        self.assertEquals(1, val)
        self.assert_('topic1' in r)  # test contains
        self.assert_(r.has_key('topic1'))
        self.assert_('topic1' in [k for k in r.iterkeys()])
        self.assert_('topic2' in [k for k in r.iterkeys()])
        self.assertEquals(['http://node2:5678'], r.get_apis('topic1'))
        self.assertEquals([('node2', 'http://node2:5678')], r['topic1'])
        self.failIf(not r)  #test nonzero
        self.assert_(['topic1', ['node2']] in r.get_state())
        self.assert_(['topic2', ['node3']] in r.get_state())

        code, _, val = r.unregister('topic1', 'node2', 'http://node2:5678')
        self.assertEquals(1, code)
        self.assertEquals(1, val)
        self.failIf('topic1' in r)  # test contains
        self.failIf(r.has_key('topic1'))
        self.assertEquals(['topic2'], [k for k in r.iterkeys()])
        self.assertEquals([], r.get_apis('topic1'))
        self.assertEquals([], r['topic1'])
        self.failIf(not r)  #test nonzero
        self.assertEquals([['topic2', ['node3']]], r.get_state())

        # clear out last reg
        code, _, val = r.unregister('topic2', 'node3', 'http://node3:5678')
        self.assertEquals(1, code)
        self.assertEquals(1, val)
        self.failIf('topic2' in r)  # test contains
        self.assert_(not r)
        self.assertEquals([], r.get_state())

    def test_Registrations(self):
        import rosmaster.exceptions
        from rosmaster.registrations import Registrations
        types = [
            Registrations.TOPIC_SUBSCRIPTIONS,
            Registrations.TOPIC_PUBLICATIONS, Registrations.SERVICE,
            Registrations.PARAM_SUBSCRIPTIONS
        ]
        # test enums
        self.assertEquals(4, len(set(types)))
        try:
            r = Registrations(-1)
            self.fail("Registrations accepted invalid type")
        except rosmaster.exceptions.InternalException, e:
            pass

        for t in types:
            r = Registrations(t)
            self.assertEquals(t, r.type)
            self.assert_(not r)  #test nonzero
            self.failIf('topic1' in r)  #test contains
            self.failIf(r.has_key('topic1'))  #test has_key
            self.failIf([k for k in r.iterkeys()])  #no keys
            self.assertEquals(None, r.get_service_api('non-existent'))

        # Test topic subs
        r = Registrations(Registrations.TOPIC_SUBSCRIPTIONS)
        self._subtest_Registrations_basic(r)
        r = Registrations(Registrations.TOPIC_PUBLICATIONS)
        self._subtest_Registrations_basic(r)
        r = Registrations(Registrations.PARAM_SUBSCRIPTIONS)
        self._subtest_Registrations_basic(r)

        r = Registrations(Registrations.SERVICE)
        self._subtest_Registrations_services(r)
    def test_Registrations(self):
        import rosmaster.exceptions
        from rosmaster.registrations import Registrations
        types = [
            Registrations.TOPIC_SUBSCRIPTIONS,
            Registrations.TOPIC_PUBLICATIONS, Registrations.SERVICE,
            Registrations.PARAM_SUBSCRIPTIONS
        ]
        # test enums
        self.assertEquals(4, len(set(types)))
        try:
            r = Registrations(-1)
            self.fail("Registrations accepted invalid type")
        except rosmaster.exceptions.InternalException:
            pass

        for t in types:
            r = Registrations(t)
            self.assertEquals(t, r.type)
            self.assert_(not r)  #test nonzero
            self.failIf('topic1' in r)  #test contains
            self.failIf(r.has_key('topic1'))  #test has_key
            self.failIf([k for k in r.iterkeys()])  #no keys
            self.assertEquals(None, r.get_service_api('non-existent'))

        # Test topic subs
        r = Registrations(Registrations.TOPIC_SUBSCRIPTIONS)
        self._subtest_Registrations_basic(r)
        r = Registrations(Registrations.TOPIC_PUBLICATIONS)
        self._subtest_Registrations_basic(r)
        r = Registrations(Registrations.PARAM_SUBSCRIPTIONS)
        self._subtest_Registrations_basic(r)

        r = Registrations(Registrations.SERVICE)
        self._subtest_Registrations_services(r)