def test_RegManager(self): from rospy.impl.registration import RegManager class MockHandler(object): def __init__(self): self.done = False self.args = [] def _connect_topic(self, topic, uri): self.args.append(['_connect_topic', topic, uri]) # trip to done on connect topic so we can exit loop self.done = True return 1, 'msg', 1 # bad return value for _connect_topic class BadHandler(object): def __init__(self): self.done = False def _connect_topic(self, topic, uri): self.done = True return None # bad code for _connect_topic class BadHandler2(object): def __init__(self): self.done = False def _connect_topic(self, topic, uri): self.done = True return -1, "failed", 1 handler = MockHandler() m = RegManager(handler) self.assertEquals(handler, m.handler) self.assert_(m.logger is not None) self.assertEquals(m.master_uri, None) self.assertEquals(m.uri, None) self.assertEquals([], m.updates) try: m.cond.acquire() finally: m.cond.release() # call twice with topic 2 to test filtering logic m.publisher_update('topic1', ['http://uri:1', 'http://uri:1b']) m.publisher_update('topic2', ['http://old:2', 'http://old:2b']) m.publisher_update('topic1b', ['http://foo:1', 'http://foo:1b']) m.publisher_update('topic2', ['http://uri:2', 'http://uri:2b']) self.assertEquals([('topic1', ['http://uri:1', 'http://uri:1b']), ('topic2', ['http://old:2', 'http://old:2b']), ('topic1b', ['http://foo:1', 'http://foo:1b']), ('topic2', ['http://uri:2', 'http://uri:2b'])], m.updates) # disabling these tests as they are just too dangerous now that registrations multithreads updates if 0: # this should only go through once as MockHandler trips to done m.run() # have to give time for threads to spin up and call handler timeout_t = 10. + time.time() while time.time() < timeout_t and len(m.updates) > 2: time.sleep(0.1) m.handler = BadHandler() # this should only go through once as BadHandler trips to done. this tests the # exception branch m.run() # this will cause an error to be logged in _connect_topic_thread # - reset data in m.updates m.updates= [('topic1', ['http://uri:1', 'http://uri:1b']), ('topic1b', ['http://foo:1', 'http://foo:1b'])] m.handler = BadHandler2() m.run() # m.start should return immediately as m.master_uri is not set self.assertEquals(m.master_uri, None) m.start(None, None) # test that it returns if URIs are equal m.start('http://localhost:1234', 'http://localhost:1234') # - test with is_shutdown overriden so we don't enter loop def foo(): return True sys.modules['rospy.impl.registration'].__dict__['is_shutdown'] = foo m.start('http://localhost:1234', 'http://localhost:4567') handler.done = True # handler done is true, so this should not run m.run() # no master uri, so these should just return m.master_uri = None m.reg_added('n1', 'type1', 'rtype1') m.reg_removed('n1', 'type1', 'rtype1') m.cleanup('reason')
def test_RegManager(self): from rospy.impl.registration import RegManager class MockHandler(object): def __init__(self): self.done = False self.args = [] def _connect_topic(self, topic, uri): self.args.append(['_connect_topic', topic, uri]) # trip to done on connect topic so we can exit loop self.done = True return 1, 'msg', 1 # bad return value for _connect_topic class BadHandler(object): def __init__(self): self.done = False def _connect_topic(self, topic, uri): self.done = True return None # bad code for _connect_topic class BadHandler2(object): def __init__(self): self.done = False def _connect_topic(self, topic, uri): self.done = True return -1, "failed", 1 handler = MockHandler() m = RegManager(handler) self.assertEquals(handler, m.handler) self.assert_(m.logger is not None) self.assertEquals(m.master_uri, None) self.assertEquals(m.uri, None) self.assertEquals([], m.updates) try: m.cond.acquire() finally: m.cond.release() # call twice with topic 2 to test filtering logic m.publisher_update('topic1', ['http://uri:1', 'http://uri:1b']) m.publisher_update('topic2', ['http://old:2', 'http://old:2b']) m.publisher_update('topic1b', ['http://foo:1', 'http://foo:1b']) m.publisher_update('topic2', ['http://uri:2', 'http://uri:2b']) self.assertEquals([('topic1', ['http://uri:1', 'http://uri:1b']), ('topic2', ['http://old:2', 'http://old:2b']), ('topic1b', ['http://foo:1', 'http://foo:1b']), ('topic2', ['http://uri:2', 'http://uri:2b'])], m.updates) # disabling these tests as they are just too dangerous now that registrations multithreads updates if 0: # this should only go through once as MockHandler trips to done m.run() # have to give time for threads to spin up and call handler timeout_t = 10. + time.time() while time.time() < timeout_t and len(m.updates) > 2: time.sleep(0.1) m.handler = BadHandler() # this should only go through once as BadHandler trips to done. this tests the # exception branch m.run() # this will cause an error to be logged in _connect_topic_thread # - reset data in m.updates m.updates = [('topic1', ['http://uri:1', 'http://uri:1b']), ('topic1b', ['http://foo:1', 'http://foo:1b'])] m.handler = BadHandler2() m.run() # m.start should return immediately as m.master_uri is not set self.assertEquals(m.master_uri, None) m.start(None, None) # test that it returns if URIs are equal m.start('http://localhost:1234', 'http://localhost:1234') # - test with is_shutdown overriden so we don't enter loop def foo(): return True sys.modules['rospy.impl.registration'].__dict__['is_shutdown'] = foo m.start('http://localhost:1234', 'http://localhost:4567') handler.done = True # handler done is true, so this should not run m.run() # no master uri, so these should just return m.master_uri = None m.reg_added('n1', 'type1', 'rtype1') m.reg_removed('n1', 'type1', 'rtype1') m.cleanup('reason')