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)
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 _subtest_ROSLaunchParent(self): from roslaunch.parent import ROSLaunchParent pmon = self.pmon import roslib.params try: # if there is a core up, we have to use its run id run_id = roslib.params.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) import roslib.packages rl_dir = roslib.packages.get_pkg_dir('roslaunch') rl_file = os.path.join(rl_dir, '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 = roslib.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 = roslib.packages.get_pkg_dir('test_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 # run an empty launch if 0: p = ROSLaunchParent(run_id, [], 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) thread.start_new_thread(kill_parent, (p,)) p.start() p.spin() # 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)