def test_ROSLaunchParentNode(self): from roslaunch.server import ROSLaunchParentNode from roslaunch.server import ChildROSLaunchProcess from roslaunch.config import ROSLaunchConfig from roslaunch.pmon import ProcessListener rosconfig = ROSLaunchConfig() try: ROSLaunchParentNode(rosconfig, None) self.fail("should not allow pm as None") except: pass pmon = self.pmon n = ROSLaunchParentNode(rosconfig, pmon) self.assertEquals(rosconfig, n.rosconfig) self.assertEquals([], n.listeners) self.assertEquals({}, n.child_processes) self.assertEquals(n.handler.listeners, n.listeners) self.assertEquals(n.handler.child_processes, n.child_processes) # test add listener self.assertEquals(n.handler.listeners, n.listeners) l = ProcessListener() n.add_process_listener(l) self.assertEquals([l], n.listeners) self.assertEquals(n.handler.listeners, n.listeners) # now, lets make some xmlrpc calls against it import roslaunch.config server = roslaunch.server.ROSLaunchParentNode( roslaunch.config.ROSLaunchConfig(), self.pmon) # it's really dangerous for logging when both a parent and a # child are in the same process, so out of paranoia, clear the # logging handlers roslaunch.core.clear_printlog_handlers() roslaunch.core.clear_printerrlog_handlers() # - register a fake child with the server so that it accepts registration from ROSLaunchChild child_name = 'child-%s' % time.time() child_proc = ChildROSLaunchProcess('foo', [], {}) server.add_child(child_name, child_proc) try: server.start() self.assert_(server.uri, "server URI did not initialize") s = ServerProxy(server.uri) child_uri = 'http://fake-unroutable:1324' # - list children should be empty val = self._succeed(s.list_children()) self.assertEquals([], val) # - register val = self._succeed(s.register(child_name, child_uri)) self.assertEquals(1, val) # - list children val = self._succeed(s.list_children()) self.assertEquals([child_uri], val) finally: server.shutdown('test done')
def test_ROSLaunchParentNode(self): from roslaunch.server import ROSLaunchParentNode from roslaunch.server import ChildROSLaunchProcess from roslaunch.config import ROSLaunchConfig from roslaunch.pmon import ProcessListener rosconfig = ROSLaunchConfig() try: ROSLaunchParentNode(rosconfig, None) self.fail("should not allow pm as None") except: pass pmon = self.pmon n = ROSLaunchParentNode(rosconfig, pmon) self.assertEquals(rosconfig, n.rosconfig) self.assertEquals([], n.listeners) self.assertEquals({}, n.child_processes) self.assertEquals(n.handler.listeners, n.listeners) self.assertEquals(n.handler.child_processes, n.child_processes) # test add listener self.assertEquals(n.handler.listeners, n.listeners) l = ProcessListener() n.add_process_listener(l) self.assertEquals([l], n.listeners) self.assertEquals(n.handler.listeners, n.listeners) # now, lets make some xmlrpc calls against it import roslaunch.config server = roslaunch.server.ROSLaunchParentNode(roslaunch.config.ROSLaunchConfig(), self.pmon) # it's really dangerous for logging when both a parent and a # child are in the same process, so out of paranoia, clear the # logging handlers roslaunch.core.clear_printlog_handlers() roslaunch.core.clear_printerrlog_handlers() # - register a fake child with the server so that it accepts registration from ROSLaunchChild child_name = 'child-%s'%time.time() child_proc = ChildROSLaunchProcess('foo', [], {}) server.add_child(child_name, child_proc) try: server.start() self.assert_(server.uri, "server URI did not initialize") s = ServerProxy(server.uri) child_uri = 'http://fake-unroutable:1324' # - list children should be empty val = self._succeed(s.list_children()) self.assertEquals([], val) # - register val = self._succeed(s.register(child_name, child_uri)) self.assertEquals(1, val) # - list children val = self._succeed(s.list_children()) self.assertEquals([child_uri], val) finally: server.shutdown('test done')
def _subtestTimeouts(self): from roslaunch.parent import ROSLaunchParent from roslaunch.server import ROSLaunchParentNode import signal import tempfile from threading import Thread pmon = self.pmon pmon.start() try: # if there is a core up, we have to use its run id run_id = get_param('/run_id') except: run_id = 'test-rl-parent-timeout-%s' % time.time() rl_dir = rospkg.RosPack().get_path('roslaunch') rl_file = os.path.join(rl_dir, 'resources', 'timeouts.launch') sigint_timeout = 2 sigterm_timeout = 3 p = ROSLaunchParent(run_id, [rl_file], is_core=False, port=11312, local_only=True, sigint_timeout=sigint_timeout, sigterm_timeout=sigterm_timeout) p._load_config() p.pm = pmon p.server = ROSLaunchParentNode(p.config, pmon) signal_log_file = os.path.join(tempfile.gettempdir(), "signal.log") try: os.remove(signal_log_file) except OSError: pass def kill_launch(times): time.sleep(3) # give it time to start times.append(time.time()) p.shutdown() times.append(time.time()) p.start() times = [] t = Thread(target=kill_launch, args=(times, )) t.start() p.spin() t.join() before_stop_call_time, after_stop_call_time = times signals = dict() try: with open(signal_log_file, 'r') as f: lines = f.readlines() for line in lines: sig, timestamp = line.split(" ") sig = int(sig) timestamp = float(timestamp) signals[sig] = timestamp except IOError: self.fail("Could not open %s" % signal_log_file) self.assertSetEqual({signal.SIGINT, signal.SIGTERM}, set(signals.keys())) self.assertAlmostEqual(before_stop_call_time, signals[signal.SIGINT], delta=1.0) self.assertAlmostEqual(before_stop_call_time, signals[signal.SIGTERM] - sigint_timeout, delta=1) self.assertAlmostEqual(before_stop_call_time, after_stop_call_time - sigint_timeout - sigterm_timeout, delta=1)
def _subroslaunchParent(self): from roslaunch.parent import ROSLaunchParent pmon = self.pmon try: # if there is a core up, we have to use its run id run_id = get_param('/run_id') except: run_id = 'test-rl-parent-%s' % time.time() name = 'foo-bob' server_uri = 'http://localhost:12345' p = ROSLaunchParent(run_id, [], is_core=True, port=None, local_only=False) self.assertEquals(run_id, p.run_id) self.assertEquals(True, p.is_core) self.assertEquals(False, p.local_only) rl_dir = rospkg.RosPack().get_path('roslaunch') rl_file = os.path.join(rl_dir, 'resources', 'example.launch') self.assert_(os.path.isfile(rl_file)) # validate load_config logic p = ROSLaunchParent(run_id, [rl_file], is_core=False, port=None, local_only=True) self.assertEquals(run_id, p.run_id) self.assertEquals(False, p.is_core) self.assertEquals(True, p.local_only) self.assert_(p.config is None) p._load_config() self.assert_(p.config is not None) self.assert_(p.config.nodes) # try again with port override p = ROSLaunchParent(run_id, [rl_file], is_core=False, port=11312, local_only=True) self.assertEquals(11312, p.port) self.assert_(p.config is None) p._load_config() # - make sure port got passed into master _, port = rosgraph.network.parse_http_host_and_port( p.config.master.uri) self.assertEquals(11312, port) # try again with bad file p = ROSLaunchParent(run_id, ['non-existent-fake.launch']) self.assert_(p.config is None) try: p._load_config() self.fail("load config should have failed due to bad rl file") except roslaunch.core.RLException: pass # try again with bad xml rl_dir = rospkg.RosPack().get_path('roslaunch') rl_file = os.path.join(rl_dir, 'test', 'xml', 'test-params-invalid-1.xml') self.assert_(os.path.isfile(rl_file)) p = ROSLaunchParent(run_id, [rl_file]) self.assert_(p.config is None) try: p._load_config() self.fail("load config should have failed due to bad rl file") except roslaunch.core.RLException: pass # Mess around with internal repr to get code coverage on _init_runner/_init_remote p = ROSLaunchParent(run_id, [], is_core=False, port=None, local_only=True) # no config, _init_runner/_init_remote/_start_server should fail for m in ['_init_runner', '_init_remote', '_start_server']: try: getattr(p, m)() self.fail('should have raised') except roslaunch.core.RLException: pass # - initialize p.config p.config = roslaunch.config.ROSLaunchConfig() # no pm, _init_runner/_init_remote/_start_server should fail for m in ['_init_runner', '_init_remote', '_start_server']: try: getattr(p, m)() self.fail('should have raised') except roslaunch.core.RLException: pass # - initialize p.pm p.pm = pmon for m in ['_init_runner', '_init_remote']: try: getattr(p, m)() self.fail('should have raised') except roslaunch.core.RLException: pass from roslaunch.server import ROSLaunchParentNode p.server = ROSLaunchParentNode(p.config, pmon) p._init_runner() # roslaunch runner should be initialized self.assert_(p.runner is not None) # test _init_remote p.local_only = True p._init_remote() p.local_only = False # - this violates many abstractions to do this def ftrue(): return True p.config.has_remote_nodes = ftrue p._init_remote() self.assert_(p.remote_runner is not None) self.failIf(pmon.is_shutdown) p.shutdown() self.assert_(pmon.is_shutdown)