def test_call_service_fail(self): proto = Protocol("test_call_service_fail") s = CallService(proto) send_msg = loads( dumps({ "op": "call_service", "service": "/rosout/set_logger_level", "args": '["ros", "invalid"]' })) received = {"msg": None, "arrived": False} def cb(msg, cid=None): received["msg"] = msg received["arrived"] = True proto.send = cb s.call_service(send_msg) timeout = 5.0 start = rospy.Time.now() while rospy.Time.now() - start < rospy.Duration(timeout): if received["arrived"]: break time.sleep(0.1) self.assertFalse(received["msg"]["result"])
def test_call_service_works(self): # First, call the service the 'proper' way p = rospy.ServiceProxy("/rosout/get_loggers", GetLoggers) ret = p() proto = Protocol("test_call_service_works") s = CallService(proto) msg = loads(dumps({"op": "call_service", "service": "/rosout/get_loggers"})) received = {"msg": None} def cb(msg, cid=None): received["msg"] = msg proto.send = cb s.call_service(msg) time.sleep(0.5) self.assertTrue(received["msg"]["result"]) for x, y in zip(ret.loggers, received["msg"]["values"]["loggers"]): self.assertEqual(x.name, y["name"]) self.assertEqual(x.level, y["level"])
def test_call_service_works(self): # First, call the service the 'proper' way p = rospy.ServiceProxy("/rosout/get_loggers", GetLoggers) ret = p() proto = Protocol("test_call_service_works") s = CallService(proto) msg = loads( dumps({ "op": "call_service", "service": "/rosout/get_loggers" })) received = {"msg": None} def cb(msg, cid=None): received["msg"] = msg proto.send = cb s.call_service(msg) time.sleep(0.5) for x, y in zip(ret.loggers, received["msg"]["values"]["loggers"]): self.assertEqual(x.name, y["name"]) self.assertEqual(x.level, y["level"])
def test_call_service_fail(self): # Dummy service that instantly fails service_server = rospy.Service("set_bool_fail", SetBool, lambda req: None) proto = Protocol("test_call_service_fail") s = CallService(proto) send_msg = loads( dumps({ "op": "call_service", "service": rospy.get_name() + "/set_bool_fail", "args": '[ true ]' })) received = {"msg": None, "arrived": False} def cb(msg, cid=None): received["msg"] = msg received["arrived"] = True proto.send = cb s.call_service(send_msg) timeout = 5.0 start = rospy.Time.now() while rospy.Time.now() - start < rospy.Duration(timeout): if received["arrived"]: break time.sleep(0.1) self.assertFalse(received["msg"]["result"])
def test_subscribe_works(self): proto = Protocol("test_subscribe_works") sub = subscribe.Subscribe(proto) topic = "/test_subscribe_works" msg = String() msg.data = "test test_subscribe_works works" msg_type = "std_msgs/String" received = {"msg": None} def send(outgoing): received["msg"] = outgoing proto.send = send sub.subscribe( loads(dumps({ "op": "subscribe", "topic": topic, "type": msg_type }))) p = rospy.Publisher(topic, String) time.sleep(0.25) p.publish(msg) time.sleep(0.25) self.assertEqual(received["msg"]["msg"]["data"], msg.data)
def test_invalid_arguments(self): proto = Protocol("test_invalid_arguments") sub = subscribe.Subscribe(proto) msg = {"op": "subscribe", "topic": 3} self.assertRaises(InvalidArgumentException, sub.subscribe, msg) msg = {"op": "subscribe", "topic": "/jon", "type": 3} self.assertRaises(InvalidArgumentException, sub.subscribe, msg) msg = {"op": "subscribe", "topic": "/jon", "throttle_rate": "fast"} self.assertRaises(InvalidArgumentException, sub.subscribe, msg) msg = { "op": "subscribe", "topic": "/jon", "fragment_size": "five cubits" } self.assertRaises(InvalidArgumentException, sub.subscribe, msg) msg = {"op": "subscribe", "topic": "/jon", "queue_length": "long"} self.assertRaises(InvalidArgumentException, sub.subscribe, msg) msg = {"op": "subscribe", "topic": "/jon", "compression": 9000} self.assertRaises(InvalidArgumentException, sub.subscribe, msg)
def test_call_service_fail(self): proto = Protocol("test_call_service_fail") s = CallService(proto) send_msg = loads(dumps({"op": "call_service", "service": "/rosout/set_logger_level", "args": '["ros", "invalid"]'})) received = {"msg": None} def cb(msg, cid=None): received["msg"] = msg proto.send = cb s.call_service(send_msg) time.sleep(0.5) self.assertFalse(received["msg"]["result"])
def test_missing_arguments(self): proto = Protocol("hello") pub = Publish(proto) msg = {"op": "publish"} self.assertRaises(MissingArgumentException, pub.publish, msg) msg = {"op": "publish", "msg": {}} self.assertRaises(MissingArgumentException, pub.publish, msg)
def test_do_advertise(self): proto = Protocol("hello") adv = Advertise(proto) topic = "/test_do_advertise" type = "std_msgs/String" msg = {"op": "advertise", "topic": topic, "type": type} adv.advertise(loads(dumps(msg))) self.assertTrue(self.is_topic_published(topic)) adv.unadvertise(loads(dumps(msg))) self.assertFalse(self.is_topic_published(topic))
def test_invalid_arguments(self): proto = Protocol("hello") adv = Advertise(proto) msg = {"op": "advertise", "topic": 3, "type": "std_msgs/String"} self.assertRaises(InvalidArgumentException, adv.advertise, loads(dumps(msg))) msg = {"op": "advertise", "topic": "/jon", "type": 3} self.assertRaises(InvalidArgumentException, adv.advertise, loads(dumps(msg)))
def setUp(self): self.proto = Protocol(self._testMethodName) # change the log function so we can verify errors are logged self.proto.log = self.mock_log # change the send callback so we can access the rosbridge messages # being sent self.proto.send = self.local_send_cb self.advertise = AdvertiseService(self.proto) self.unadvertise = UnadvertiseService(self.proto) self.response = ServiceResponse(self.proto) self.received_message = None self.log_entries = []
def test_call_service_works(self): # Prepare to call the service the 'proper' way p = rospy.ServiceProxy(rospy.get_name() + "/get_loggers", GetLoggers) p.wait_for_service() time.sleep(1.0) proto = Protocol("test_call_service_works") s = CallService(proto) msg = loads( dumps({ "op": "call_service", "service": rospy.get_name() + "/get_loggers" })) received = {"msg": None, "arrived": False} def cb(msg, cid=None): received["msg"] = msg received["arrived"] = True proto.send = cb s.call_service(msg) timeout = 5.0 start = rospy.Time.now() while rospy.Time.now() - start < rospy.Duration(timeout): if received["arrived"]: break time.sleep(0.1) # The rosbridge service call actually causes another logger to appear, # so do the "regular" service call after that. ret = p() self.assertTrue(received["msg"]["result"]) for x, y in zip(ret.loggers, received["msg"]["values"]["loggers"]): self.assertEqual(x.name, y["name"]) self.assertEqual(x.level, y["level"])
def test_subscribe_works(self): proto = Protocol("test_subscribe_works") sub = subscribe.Subscribe(proto) topic = "/test_subscribe_works" msg = String() msg.data = "test test_subscribe_works works" msg_type = "std_msgs/String" received = {"msg": None} def send(outgoing): received["msg"] = outgoing proto.send = send sub.subscribe(loads(dumps({"op": "subscribe", "topic": topic, "type": msg_type}))) p = rospy.Publisher(topic, String) time.sleep(0.25) p.publish(msg) time.sleep(0.25) self.assertEqual(received["msg"]["msg"]["data"], msg.data)
def test_call_service_works(self): # First, call the service the 'proper' way p = rospy.ServiceProxy("/rosout/get_loggers", GetLoggers) p.wait_for_service() time.sleep(1.0) ret = p() proto = Protocol("test_call_service_works") s = CallService(proto) msg = loads( dumps({ "op": "call_service", "service": "/rosout/get_loggers" })) received = {"msg": None, "arrived": False} def cb(msg, cid=None): received["msg"] = msg received["arrived"] = True proto.send = cb s.call_service(msg) timeout = 5.0 start = rospy.Time.now() while rospy.Time.now() - start < rospy.Duration(timeout): if received["arrived"]: break time.sleep(0.1) self.assertTrue(received["msg"]["result"]) for x, y in zip(ret.loggers, received["msg"]["values"]["loggers"]): self.assertEqual(x.name, y["name"]) self.assertEqual(x.level, y["level"])
def test_invalid_msg_module(self): no_msgs = [ "roslib/Time", "roslib/Duration", "roslib/Header", "std_srvs/ConflictedMsg", "topic_tools/MessageMessage" ] proto = Protocol("hello") adv = Advertise(proto) for invalid_type in no_msgs: msg = { "op": "advertise", "topic": "/test_invalid_msg_module", "type": invalid_type } self.assertRaises(ros_loader.InvalidModuleException, adv.advertise, loads(dumps(msg)))
def test_invalid_msg_package(self): nonexistent = [ "wangle_msgs/Jam", "whistleblower_msgs/Document", "sexual_harrassment_msgs/UnwantedAdvance", "coercion_msgs/Bribe", "airconditioning_msgs/Cold", "pr2thoughts_msgs/Escape" ] proto = Protocol("hello") adv = Advertise(proto) for invalid_type in nonexistent: msg = { "op": "advertise", "topic": "/test_invalid_msg_package", "type": invalid_type } self.assertRaises(ros_loader.InvalidPackageException, adv.advertise, loads(dumps(msg)))
def test_invalid_msg_classes(self): nonexistent = [ "roscpp/Time", "roscpp/Duration", "roscpp/Header", "rospy/Time", "rospy/Duration", "rospy/Header", "std_msgs/Spool", "geometry_msgs/Tetrahedron", "sensor_msgs/TelepathyUnit" ] proto = Protocol("hello") adv = Advertise(proto) for invalid_type in nonexistent: msg = { "op": "advertise", "topic": "/test_invalid_msg_classes", "type": invalid_type } self.assertRaises(ros_loader.InvalidClassException, adv.advertise, loads(dumps(msg)))
def test_publish_works(self): proto = Protocol("hello") pub = Publish(proto) topic = "/test_publish_works" msg = {"data": "test publish works"} received = {"msg": None} def cb(msg): received["msg"] = msg rospy.Subscriber(topic, String, cb) pub_msg = loads(dumps({"op": "publish", "topic": topic, "msg": msg})) pub.publish(pub_msg) sleep(0.5) self.assertEqual(received["msg"].data, msg["data"])
def test_invalid_msg_typestrings(self): invalid = [ "", "/", "//", "///", "////", "/////", "bad", "stillbad", "not/better/still", "not//better//still", "not///better///still", "better/", "better//", "better///", "/better", "//better", "///better", "this\isbad", "\\" ] proto = Protocol("hello") adv = Advertise(proto) for invalid_type in invalid: msg = { "op": "advertise", "topic": "/test_invalid_msg_typestrings", "type": invalid_type } self.assertRaises(ros_loader.InvalidTypeStringException, adv.advertise, loads(dumps(msg)))
def test_valid_msg_classes(self): assortedmsgs = [ "geometry_msgs/Pose", "actionlib_msgs/GoalStatus", "geometry_msgs/WrenchStamped", "stereo_msgs/DisparityImage", "nav_msgs/OccupancyGrid", "geometry_msgs/Point32", "std_msgs/String", "trajectory_msgs/JointTrajectoryPoint", "diagnostic_msgs/KeyValue", "visualization_msgs/InteractiveMarkerUpdate", "nav_msgs/GridCells", "sensor_msgs/PointCloud2" ] proto = Protocol("hello") adv = Advertise(proto) for valid_type in assortedmsgs: msg = { "op": "advertise", "topic": "/" + valid_type, "type": valid_type } adv.advertise(loads(dumps(msg))) adv.unadvertise(loads(dumps(msg)))
def __init__(self, client_id): Protocol.__init__(self, client_id) for capability_class in self.rosbridge_capabilities: self.add_capability(capability_class)
def test_invalid_arguments(self): proto = Protocol("hello") pub = Publish(proto) msg = {"op": "publish", "topic": 3} self.assertRaises(InvalidArgumentException, pub.publish, msg)
def test_invalid_arguments(self): proto = Protocol("test_invalid_arguments") s = CallService(proto) msg = loads(dumps({"op": "call_service", "service": 3})) self.assertRaises(InvalidArgumentException, s.call_service, msg)
def test_missing_arguments(self): proto = Protocol("test_missing_arguments") s = CallService(proto) msg = loads(dumps({"op": "call_service"})) self.assertRaises(MissingArgumentException, s.call_service, msg)
def __init__(self, client_id, parameters = None): self.parameters = parameters Protocol.__init__(self, client_id) for capability_class in self.rosbridge_capabilities: self.add_capability(capability_class)
def __init__(self, client_id, parameters=None): self.parameters = parameters Protocol.__init__(self, client_id) for capability_class in self.rosbridge_capabilities: self.add_capability(capability_class)
def test_missing_arguments(self): proto = Protocol("test_missing_arguments") sub = subscribe.Subscribe(proto) msg = {"op": "subscribe"} self.assertRaises(MissingArgumentException, sub.subscribe, msg)