def test_subscriber_first(self): self.assert_(self.callback_data is None, "invalid test fixture") # wait at most 5 seconds for listenerpublisher to be registered timeout_t = time.time() + 5.0 while not rostest.is_subscriber( rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)) and time.time() < timeout_t: time.sleep(0.1) self.assert_( rostest.is_subscriber(rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)), "%s is not up" % LPNODE) print "Publishing to ", PUBTOPIC pub = rospy.Publisher(PUBTOPIC, MSG) rospy.Subscriber(LPTOPIC, MSG, self._test_subscriber_first_callback) # publish about 10 messages for fun import random val = random.randint(0, 109812312) msg = "hi [%s]" % val for i in xrange(0, 10): pub.publish(MSG(msg)) time.sleep(0.1) # listenerpublisher is supposed to repeat our messages back onto /listenerpublisher, # make sure we got it self.assert_(self.callback_data is not None, "no callback data from listenerpublisher") self.assertEquals( msg, self.callback_data.data, "callback data from listenerpublisher does not match")
def test_subscriber_first(self): self.assert_(self.callback_data is None, "invalid test fixture") # wait at most 5 seconds for listenerpublisher to be registered timeout_t = time.time() + 5.0 while not rostest.is_subscriber( rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)) and time.time() < timeout_t: time.sleep(0.1) self.assert_(rostest.is_subscriber( rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)), "%s is not up"%LPNODE) print "Publishing to ", PUBTOPIC pub = rospy.Publisher(PUBTOPIC, MSG) rospy.Subscriber(LPTOPIC, MSG, self._test_subscriber_first_callback) # publish about 10 messages for fun import random val = random.randint(0, 109812312) msg = "hi [%s]"%val for i in xrange(0, 10): pub.publish(MSG(msg)) time.sleep(0.1) # listenerpublisher is supposed to repeat our messages back onto /listenerpublisher, # make sure we got it self.assert_(self.callback_data is not None, "no callback data from listenerpublisher") self.assertEquals(msg, self.callback_data.data, "callback data from listenerpublisher does not match")
def wait_for_subscriber(node_name, topic, timeout=5.0): '''Blocks until $node_name subscribes to $topic Useful mostly in integration tests -- I would counsel against use elsewhere ''' end_time = time.time() + timeout # Wait for time-out or ros-shutdown while (time.time() < end_time) and (not rospy.is_shutdown()): subscribed = rostest.is_subscriber( rospy.resolve_name(topic), rospy.resolve_name(node_name) ) # Success scenario: node subscribes if subscribed: break time.sleep(0.1) # Could do this with a while/else # But chose to explicitly check success = rostest.is_subscriber( rospy.resolve_name(topic), rospy.resolve_name(node_name) ) return success
def wait_for_pubsub(self): # wait at most 5 seconds for listenerpublisher to be registered timeout_t = time.time() + 5.0 while not rostest.is_subscriber( rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)) and time.time() < timeout_t: time.sleep(0.1) self.assert_(rostest.is_subscriber( rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)), "%s is not up" % LPNODE)
def wait_for_pubsub(self): # wait at most 5 seconds for listenerpublisher to be registered timeout_t = time.time() + 5.0 while not rostest.is_subscriber( rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)) and time.time() < timeout_t: time.sleep(0.1) self.assert_( rostest.is_subscriber(rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)), "%s is not up" % LPNODE)
def test_ms_msg(self): self.assertEquals(0, len(self.callback_data), "invalid test fixture") if 0: # wait at most 5 seconds for publisher to be registered timeout_t = time.time() + 5.0 while not rostest.is_subscriber( rospy.resolve_name(PUBTOPIC), rospy.resolve_name(PUBNODE)) and time.time() < timeout_t: time.sleep(0.1) self.assert_( rostest.is_subscriber(rospy.resolve_name(PUBTOPIC), rospy.resolve_name(PUBNODE)), "%s is not up" % PUBNODE) print "Subscribing to ", SUBTOPIC rospy.TopicSub(SUBTOPIC, MSG, self._test_ms_callback) sleep_time = 3.0 print "Waiting %s seconds to collect messages" % sleep_time time.sleep(sleep_time) # make sure we got it self.assert_(len(self.callback_data), "no callback data from %s" % PUBNODE) for d in self.callback_data: errorstr = "callback msg field [%%s] from %s does not match" % PUBNODE self.assertAlmostEqual(3.14, d.time, 2, errorstr % "time") self.assertEquals(250, len(d.joint_states)) self.assertEquals(260, len(d.actuator_states)) for f in d.joint_states: self.assertEquals("jointstate", f.name) self.assertAlmostEqual(1.0, f.position, 1) self.assertAlmostEqual(1.0, f.velocity, 1) self.assertAlmostEqual(1.0, f.applied_effort, 1) self.assertAlmostEqual(1.0, f.commanded_effort, 1) for i in xrange(0, len(d.actuator_states)): f = d.actuator_states[i] self.assertEquals("actuatorstate", f.name) self.assertEquals(1.0, f.motor_voltage) self.assertEquals(i, f.encoder_count) self.assertEquals(0, f.calibration_reading) self.assertEquals(i + 1, f.last_calibration_low_transition) self.assertEquals(i + 2, f.last_calibration_high_transition) self.assertEquals(0, f.num_encoder_errors)
def test_ms_msg(self): self.assertEquals(0, len(self.callback_data), "invalid test fixture") if 0: # wait at most 5 seconds for publisher to be registered timeout_t = time.time() + 5.0 while ( not rostest.is_subscriber(rospy.resolve_name(PUBTOPIC), rospy.resolve_name(PUBNODE)) and time.time() < timeout_t ): time.sleep(0.1) self.assert_( rostest.is_subscriber(rospy.resolve_name(PUBTOPIC), rospy.resolve_name(PUBNODE)), "%s is not up" % PUBNODE, ) print "Subscribing to ", SUBTOPIC rospy.TopicSub(SUBTOPIC, MSG, self._test_ms_callback) sleep_time = 3.0 print "Waiting %s seconds to collect messages" % sleep_time time.sleep(sleep_time) # make sure we got it self.assert_(len(self.callback_data), "no callback data from %s" % PUBNODE) for d in self.callback_data: errorstr = "callback msg field [%%s] from %s does not match" % PUBNODE self.assertAlmostEqual(3.14, d.time, 2, errorstr % "time") self.assertEquals(250, len(d.joint_states)) self.assertEquals(260, len(d.actuator_states)) for f in d.joint_states: self.assertEquals("jointstate", f.name) self.assertAlmostEqual(1.0, f.position, 1) self.assertAlmostEqual(1.0, f.velocity, 1) self.assertAlmostEqual(1.0, f.applied_effort, 1) self.assertAlmostEqual(1.0, f.commanded_effort, 1) for i in xrange(0, len(d.actuator_states)): f = d.actuator_states[i] self.assertEquals("actuatorstate", f.name) self.assertEquals(1.0, f.motor_voltage) self.assertEquals(i, f.encoder_count) self.assertEquals(0, f.calibration_reading) self.assertEquals(i + 1, f.last_calibration_low_transition) self.assertEquals(i + 2, f.last_calibration_high_transition) self.assertEquals(0, f.num_encoder_errors)
def test_unsubscribe(self): global _last_callback uri = rospy.get_node_uri() node_proxy = xmlrpclib.ServerProxy(uri) _, _, subscriptions = node_proxy.getSubscriptions('/foo') self.assert_(not subscriptions, 'subscriptions present: %s' % str(subscriptions)) print "Subscribing to ", SUBTOPIC sub = rospy.Subscriber(SUBTOPIC, String, callback) topic = rospy.resolve_name(SUBTOPIC) _, _, subscriptions = node_proxy.getSubscriptions('/foo') self.assertEquals([[topic, String._type]], subscriptions, "Subscriptions were %s" % subscriptions) # wait for the first message to be received timeout_t = time.time() + TIMEOUT while _last_callback is None and time.time() < timeout_t: time.sleep(0.1) self.assert_(_last_callback is not None, "No messages received from talker") # begin actual test by unsubscribing sub.unregister() # clear last callback data, i.e. last message received _last_callback = None timeout_t = time.time() + 2.0 # make sure no new messages are received in the next 2 seconds while timeout_t < time.time(): time.sleep(1.0) self.assert_(_last_callback is None) # verify that close cleaned up master and node state _, _, subscriptions = node_proxy.getSubscriptions('/foo') self.assert_(not subscriptions, "Node still has subscriptions: %s" % subscriptions) n = rospy.get_caller_id() self.assert_(not rostest.is_subscriber(topic, n), "subscription is still active on master")
def test_subscribe_to_multiple_publishers(self): # wait so that all connections are established time.sleep(1.0) # ensure that publishers are publishing for i in range(1, NUMBER_OF_TALKERS + 1): self.assert_(rostest.is_publisher( rospy.resolve_name(TOPIC), rospy.resolve_name(TALKER_NODE % i)), 'talker node %d is not up' % i) # ensure that subscriber is subscribed self.assert_(rostest.is_subscriber( rospy.resolve_name(TOPIC), rospy.resolve_name(LISTENER_NODE)), 'listener node is not up') # check number of connections from subscriber to the topic connections = 0 master = rosgraph.Master(NAME) node_api = master.lookupNode(LISTENER_NODE) if not node_api: self.assert_(False, 'cannot contact [%s]: unknown node' % LISTENER_NODE) socket.setdefaulttimeout(5.0) node = xmlrpclib.ServerProxy(node_api) code, _, businfo = node.getBusInfo(NAME) if code != 1: self.assert_(False, 'cannot get node information') if businfo: for info in businfo: topic = info[4] if len(info) > 5: connected = info[5] else: connected = True # backwards compatibility if connected: if topic == TOPIC: connections += 1 self.assert_(connections == NUMBER_OF_TALKERS, 'Found only %d connections instead of %d' % (connections, NUMBER_OF_TALKERS))
def test_unsubscribe(self): global _last_callback uri = rospy.get_node_uri() node_proxy = ServerProxy(uri) _, _, subscriptions = node_proxy.getSubscriptions('/foo') self.assert_(not subscriptions, 'subscriptions present: %s'%str(subscriptions)) print("Subscribing to ", SUBTOPIC) sub = rospy.Subscriber(SUBTOPIC, String, callback) topic = rospy.resolve_name(SUBTOPIC) _, _, subscriptions = node_proxy.getSubscriptions('/foo') self.assertEquals([[topic, String._type]], subscriptions, "Subscriptions were %s"%subscriptions) # wait for the first message to be received timeout_t = time.time() + TIMEOUT while _last_callback is None and time.time() < timeout_t: time.sleep(0.1) self.assert_(_last_callback is not None, "No messages received from talker") # begin actual test by unsubscribing sub.unregister() # clear last callback data, i.e. last message received _last_callback = None timeout_t = time.time() + 2.0 # make sure no new messages are received in the next 2 seconds while timeout_t < time.time(): time.sleep(1.0) self.assert_(_last_callback is None) # verify that close cleaned up master and node state _, _, subscriptions = node_proxy.getSubscriptions('/foo') self.assert_(not subscriptions, "Node still has subscriptions: %s"%subscriptions) n = rospy.get_caller_id() self.assert_(not rostest.is_subscriber(topic, n), "subscription is still active on master")
def test_is_keypress_event_subscriber_connected(self): assert rostest.is_subscriber(rospy.resolve_name(_KEYPRESS_EVENT_TOPIC), rospy.resolve_name(_GUI_NODE))
def test_ms_msg(self): self.assert_(self.callback_data is None, "invalid test fixture") # wait at most 5 seconds for listenerpublisher to be registered timeout_t = time.time() + 5.0 while not rostest.is_subscriber( rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)) and time.time() < timeout_t: time.sleep(0.1) self.assert_( rostest.is_subscriber(rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)), "%s is not up" % LPNODE) print "Publishing to ", PUBTOPIC pub = rospy.TopicPub(PUBTOPIC, MSG) print "Subscribing to ", SUBTOPIC rospy.TopicSub(SUBTOPIC, MSG, self._test_ms_callback) # publish about 10 messages for fun import random val = random.randint(0, 109812312) msg = "hi [%s]" % val header = None for i in xrange(0, 10): # The test message could be better in terms of the values # it assigns to leaf fields, but the main focus is trying # to dig up edge conditions in the embeds, especially with # respect to arrays and embeds. actuator_states = [ ActuatorState('name0', 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0), ActuatorState('name1', 1, 1.0, 1.0, 1.0, 1.0, 1, 1, 1, 1, 1, 1.0, 1.0, 1.0, 1.0, 1), ] joint_states = [ JointState('name0', 0.0, 0.0, 0.0, 0.0), JointState('name1', 1.0, 1.0, 1.0, 1.0), ] pub.publish(MSG(header, 2.0, actuator_states, joint_states)) time.sleep(0.1) # listenerpublisher is supposed to repeat our messages back onto /listenerpublisher, # make sure we got it self.assert_(self.callback_data is not None, "no callback data from listenerpublisher") print "Got ", self.callback_data.time errorstr = "callback msg field [%s] from listenerpublisher does not match" self.assertEquals(2.0, self.callback_data.time, errorstr % "time") self.assertEquals(2, len(self.callback_data.joint_states)) self.assertEquals(2, len(self.callback_data.actuator_states)) self.assertEquals("name0", self.callback_data.joint_states[0].name) self.assertEquals("name0", self.callback_data.actuator_states[0].name) self.assertEquals("name1", self.callback_data.joint_states[1].name) self.assertEquals("name1", self.callback_data.actuator_states[1].name) self.assertEquals(0, self.callback_data.actuator_states[0].encoder_count) self.assertEquals( 0, self.callback_data.actuator_states[0].calibration_reading) self.assertEquals( 0, self.callback_data.actuator_states[0].last_calibration_rising_edge) self.assertEquals( 0, self.callback_data.actuator_states[0].num_encoder_errors) self.assertEquals(1, self.callback_data.actuator_states[1].encoder_count) self.assertEquals( 1, self.callback_data.actuator_states[1].calibration_reading) self.assertEquals( 1, self.callback_data.actuator_states[1].last_calibration_rising_edge) self.assertEquals( 1, self.callback_data.actuator_states[1].num_encoder_errors)
def test_embed_msg(self): self.assert_(self.callback_data is None, "invalid test fixture") # wait at most 5 seconds for listenerpublisher to be registered timeout_t = time.time() + 5.0 while not rostest.is_subscriber( rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)) and time.time() < timeout_t: time.sleep(0.1) self.assert_(rostest.is_subscriber( rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)), "%s is not up"%LPNODE) print "Publishing to ", PUBTOPIC pub = rospy.Publisher(PUBTOPIC, MSG) rospy.Subscriber(LPTOPIC, MSG, self._test_embed_msg_callback) # publish about 10 messages for fun import random val = random.randint(0, 109812312) msg = "hi [%s]"%val for i in xrange(0, 10): # The test message could be better in terms of the values # it assigns to leaf fields, but the main focus is trying # to dig up edge conditions in the embeds, especially with # respect to arrays and embeds. pub.publish( MSG(String(msg), Int32(val), [Int32(val+1), Int32(val+2), Int32(val+3)], Val(msg+msg), [Val(msg), Val("two")], [ArrayVal([Val("av1"), Val("av2")]), #[Val("%s"%i) for i in xrange(0, 10)]), ArrayVal([]) #,[Val("%s"%i) for i in xrange(0, 10)]), ] )) time.sleep(0.1) # listenerpublisher is supposed to repeat our messages back onto /listenerpublisher, # make sure we got it self.assert_(self.callback_data is not None, "no callback data from listenerpublisher") print "Got ", self.callback_data.str1.data, self.callback_data.int1.data errorstr = "callback msg field [%s] from listenerpublisher does not match" self.assertEquals(msg, self.callback_data.str1.data, errorstr%"str1.data") self.assertEquals(val, self.callback_data.int1.data, errorstr%"int1.data") for i in xrange(1, 4): self.assertEquals(val+i, self.callback_data.ints[i-1].data, errorstr%"ints[i-1].data") self.assertEquals(msg+msg, self.callback_data.val.val, errorstr%"val.val") self.assertEquals(msg, self.callback_data.vals[0].val, errorstr%"vals[0].val") self.assertEquals("two", self.callback_data.vals[1].val, errorstr%"vals[1].val") # #435: test array of arrays self.assertEquals(2, len(self.callback_data.arrayval), errorstr%"len arrayval") self.assertEquals(2, len(self.callback_data.arrayval[0].vals), errorstr%"len arrayval[0].vals") self.assertEquals("av1", self.callback_data.arrayval[0].vals[0].val, errorstr%"arrayval[0].vals[0].val") self.assertEquals("av2", self.callback_data.arrayval[0].vals[1].val, errorstr%"arrayval[0].vals[1].val") self.assertEquals(0, len(self.callback_data.arrayval[1].vals), errorstr%"len arrayval[1].vals")
def test_is_new_server_event_subscriber_connected(self): assert rostest.is_subscriber( rospy.resolve_name(_NEW_SERVER_EVENT_TOPIC), rospy.resolve_name(_GUI_NODE))
def test_is_say_subscriber_connected(self): assert rostest.is_subscriber(rospy.resolve_name(_SAY_TOPIC), rospy.resolve_name(_MANAGER_NODE))
def test_ms_msg(self): self.assert_(self.callback_data is None, "invalid test fixture") # wait at most 5 seconds for listenerpublisher to be registered timeout_t = time.time() + 5.0 while ( not rostest.is_subscriber(rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)) and time.time() < timeout_t ): time.sleep(0.1) self.assert_( rostest.is_subscriber(rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)), "%s is not up" % LPNODE ) print "Publishing to ", PUBTOPIC pub = rospy.TopicPub(PUBTOPIC, MSG) print "Subscribing to ", SUBTOPIC rospy.TopicSub(SUBTOPIC, MSG, self._test_ms_callback) # publish about 10 messages for fun import random val = random.randint(0, 109812312) msg = "hi [%s]" % val header = None for i in xrange(0, 10): # The test message could be better in terms of the values # it assigns to leaf fields, but the main focus is trying # to dig up edge conditions in the embeds, especially with # respect to arrays and embeds. actuator_states = [ ActuatorState("name0", 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0), ActuatorState("name1", 1, 1.0, 1.0, 1.0, 1.0, 1, 1, 1, 1, 1, 1.0, 1.0, 1.0, 1.0, 1), ] joint_states = [JointState("name0", 0.0, 0.0, 0.0, 0.0), JointState("name1", 1.0, 1.0, 1.0, 1.0)] pub.publish(MSG(header, 2.0, actuator_states, joint_states)) time.sleep(0.1) # listenerpublisher is supposed to repeat our messages back onto /listenerpublisher, # make sure we got it self.assert_(self.callback_data is not None, "no callback data from listenerpublisher") print "Got ", self.callback_data.time errorstr = "callback msg field [%s] from listenerpublisher does not match" self.assertEquals(2.0, self.callback_data.time, errorstr % "time") self.assertEquals(2, len(self.callback_data.joint_states)) self.assertEquals(2, len(self.callback_data.actuator_states)) self.assertEquals("name0", self.callback_data.joint_states[0].name) self.assertEquals("name0", self.callback_data.actuator_states[0].name) self.assertEquals("name1", self.callback_data.joint_states[1].name) self.assertEquals("name1", self.callback_data.actuator_states[1].name) self.assertEquals(0, self.callback_data.actuator_states[0].encoder_count) self.assertEquals(0, self.callback_data.actuator_states[0].calibration_reading) self.assertEquals(0, self.callback_data.actuator_states[0].last_calibration_high_transition) self.assertEquals(0, self.callback_data.actuator_states[0].num_encoder_errors) self.assertEquals(1, self.callback_data.actuator_states[1].encoder_count) self.assertEquals(1, self.callback_data.actuator_states[1].calibration_reading) self.assertEquals(1, self.callback_data.actuator_states[1].last_calibration_high_transition) self.assertEquals(1, self.callback_data.actuator_states[1].num_encoder_errors)
def test_embed_msg(self): self.assert_(self.callback_data is None, "invalid test fixture") # wait at most 5 seconds for listenerpublisher to be registered timeout_t = time.time() + 5.0 while not rostest.is_subscriber( rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)) and time.time() < timeout_t: time.sleep(0.1) self.assert_( rostest.is_subscriber(rospy.resolve_name(PUBTOPIC), rospy.resolve_name(LPNODE)), "%s is not up" % LPNODE) print("Publishing to ", PUBTOPIC) pub = rospy.Publisher(PUBTOPIC, MSG, queue_size=0) rospy.Subscriber(LPTOPIC, MSG, self._test_embed_msg_callback) # publish about 10 messages for fun import random val = random.randint(0, 109812312) msg = "hi [%s]" % val for i in range(0, 10): # The test message could be better in terms of the values # it assigns to leaf fields, but the main focus is trying # to dig up edge conditions in the embeds, especially with # respect to arrays and embeds. pub.publish( MSG( String(msg), Int32(val), [Int32(val + 1), Int32(val + 2), Int32(val + 3)], Val(msg + msg), [Val(msg), Val("two")], [ ArrayVal([Val("av1"), Val("av2") ]), #[Val("%s"%i) for i in range(0, 10)]), ArrayVal([]) #,[Val("%s"%i) for i in range(0, 10)]), ])) time.sleep(0.1) # listenerpublisher is supposed to repeat our messages back onto /listenerpublisher, # make sure we got it self.assert_(self.callback_data is not None, "no callback data from listenerpublisher") print("Got ", self.callback_data.str1.data, self.callback_data.int1.data) errorstr = "callback msg field [%s] from listenerpublisher does not match" self.assertEquals(msg, self.callback_data.str1.data, errorstr % "str1.data") self.assertEquals(val, self.callback_data.int1.data, errorstr % "int1.data") for i in range(1, 4): self.assertEquals(val + i, self.callback_data.ints[i - 1].data, errorstr % "ints[i-1].data") self.assertEquals(msg + msg, self.callback_data.val.val, errorstr % "val.val") self.assertEquals(msg, self.callback_data.vals[0].val, errorstr % "vals[0].val") self.assertEquals("two", self.callback_data.vals[1].val, errorstr % "vals[1].val") # #435: test array of arrays self.assertEquals(2, len(self.callback_data.arrayval), errorstr % "len arrayval") self.assertEquals(2, len(self.callback_data.arrayval[0].vals), errorstr % "len arrayval[0].vals") self.assertEquals("av1", self.callback_data.arrayval[0].vals[0].val, errorstr % "arrayval[0].vals[0].val") self.assertEquals("av2", self.callback_data.arrayval[0].vals[1].val, errorstr % "arrayval[0].vals[1].val") self.assertEquals(0, len(self.callback_data.arrayval[1].vals), errorstr % "len arrayval[1].vals")
def test_is_go_to_sleep_subscriber_connected(self): assert rostest.is_subscriber(rospy.resolve_name(_IS_GO_TO_SLEEP_TOPIC), rospy.resolve_name(_MANAGER_NODE))
def test_is_mouse_event_subscriber_connected(self): assert rostest.is_subscriber(rospy.resolve_name(_MOUSE_EVENT_TOPIC), rospy.resolve_name(_GUI_NODE))