Skip to content

Commit

Permalink
Merge pull request #13 from tecnalia-advancedmanufacturing-robotics/f…
Browse files Browse the repository at this point in the history
…ix_service_call

Fix service call
  • Loading branch information
aremazeilles authored Aug 21, 2024
2 parents 5d1e771 + 67bd981 commit fa9ece4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[![license - apache 2.0](https://img.shields.io/:license-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

# node_test

[![license - apache 2.0](https://img.shields.io/:license-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

The package `node_test` extends `rostest` to add more testing functionalities at node level.

**Author & Maintainer**: [Anthony Remazeilles](https://github.com/aremazeilles), [email protected]
**Author & Maintainer**: [Anthony Remazeilles](https://github.com/aremazeilles), <mailto:[email protected]>

**Affiliation** : Tecnalia Research and Innovation, Spain

Expand All @@ -17,6 +17,7 @@ See [LICENSE.md](LICENSE.md) for more details.

We assume [`ROS`][ros] is installed on the machine.
Code developed and tested so far under `ROS kinetic`.
Currently working under `ROS noetic`

[ros]: http://www.ros.org/

Expand Down Expand Up @@ -93,7 +94,6 @@ rostest node_test example_srv.test

### test_filter


`test_filter` enables testing a filter-like node, that is supposed to publish a message after having processed a received message.

An example is provided in [msg_filter.test](node_test/test/msg_filter.test):
Expand Down
6 changes: 5 additions & 1 deletion node_test/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<depend>rospy</depend>
<depend>rospy_message_converter</depend>
<test_depend>roslint</test_depend>
<build_depend>rostest</build_depend>
<depend>rosservice</depend>
<depend>rostest</depend>
<depend>rostopic</depend>
<test_depend>std_srvs</test_depend>
<test_depend>std_msgs</test_depend>

</package>
18 changes: 8 additions & 10 deletions node_test/src/node_test/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<rosparam>
filters:
- topic_in: topic to which the filter node is subscribed to
topic_out: topic to which the filter node will pubish filter output
topic_out: topic to which the filter node will publish filter output
msg_in: message to publish on topic_in, in python dictionary format
msg_out: message to be received on topic_out, in python dictionary format
timeout: limit time [s] for receiving the filter output
Expand All @@ -25,16 +25,14 @@
__author__ = "Anthony Remazeilles"
__email__ = "[email protected]"
__copyright__ = "Copyright (C) 2020 Tecnalia Research and Innovation"
__licence__ = "Apache 2.0"
__license__ = "Apache 2.0"

import sys
import time
import unittest
import rospy
import rostopic
import rosmsg
import rostest
import genpy
from rospy_message_converter import message_converter

CLASSNAME = 'filterTest'
Expand Down Expand Up @@ -73,8 +71,8 @@ def test_filter(self):
keys = ['topic_in', 'topic_out', 'msg_in', 'msg_out', 'timeout']

for item in keys:
if item not in keys:
self.fail("{} field required, but not specified in {}".format(item, call))
if item not in one_filter:
self.fail("{} field required, but not specified in {}".format(item, one_filter))

if one_filter['topic_in'] == 'None':
rospy.logwarn('None input converted to empty input')
Expand Down Expand Up @@ -123,10 +121,10 @@ def _test_filter(self, topic_in, topic_out, msg_in, msg_out, timeout):
ros_msg_in = message_converter.convert_dictionary_to_ros_message(msg_in_type, msg_in)
rospy.loginfo("Generated message: [%s]" % (ros_msg_in))
except ValueError as err:
msg_err = "Prb in message in contruction \n"
msg_err = "Prb in message in construction \n"
msg_err += "Expected type: [%s]\n" % (msg_in_type)
msg_err += "dictionary: [%s]\n" % (msg_in)
msg_err += "Erorr: [%s]\n" % (str(err))
msg_err += "Error: [%s]\n" % (str(err))
self.fail(msg_err)

# getting message to receive
Expand All @@ -139,10 +137,10 @@ def _test_filter(self, topic_in, topic_out, msg_in, msg_out, timeout):
ros_msg_out = message_converter.convert_dictionary_to_ros_message(msg_out_type, msg_out)
rospy.loginfo("Generated message: [%s]" % (ros_msg_out))
except ValueError as err:
msg_err = "Prb in message in contruction \n"
msg_err = "Prb in message construction \n"
msg_err += "Expected type: [%s]\n" % (msg_out_type)
msg_err += "dictionary: [%s]\n" % (msg_out)
msg_err += "Erorr: [%s]\n" % (str(err))
msg_err += "Errorr: [%s]\n" % (str(err))
self.fail(msg_err)

# subscription
Expand Down
11 changes: 8 additions & 3 deletions node_test/src/node_test/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
__author__ = "Anthony Remazeilles"
__email__ = "[email protected]"
__copyright__ = "Copyright (C) 2020 Tecnalia Research and Innovation"
__licence__ = "Apache 2.0"
__license__ = "Apache 2.0"

import sys
import time
Expand Down Expand Up @@ -92,7 +92,7 @@ def setUp(self):
def test_advertise_service(self):
"""Test services are advertised"""
t_start = self.t_start
s_name_set = set([ item['name'] for item in self.calls])
s_name_set = set([item['name'] for item in self.calls])
t_timeout_max = 10.0

while not rospy.is_shutdown():
Expand Down Expand Up @@ -133,11 +133,16 @@ def _test_service(self, srv_name, srv_input, srv_output):
srv_proxy = rospy.ServiceProxy(srv_name, srv_class)
except KeyError as err:
msg_err = "Service proxy could not be created"
msg_err += "Error: [%s]\n" % (str(err))
self.fail(msg_err)

try:
if srv_input:
srv_resp = srv_proxy(**srv_input)
srv_type = rosservice.get_service_type(srv_name)
rospy.loginfo(f"Service type: {srv_type}")

req = message_converter.convert_dictionary_to_ros_message(srv_type, srv_input, kind='request')
srv_resp = srv_proxy(req)
else:
srv_resp = srv_proxy()

Expand Down
4 changes: 4 additions & 0 deletions node_test/test_nodes/service_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@
def empty_cb(req):
return EmptyResponse()


def set_bool_cb(req):
response = SetBoolResponse()
response.success = True
response.message = str(req.data)
return response


def trigger_cb(req):
return TriggerResponse()


def trigger2_cb(req):
response = TriggerResponse()
response.success = True
response.message = 'well done!'
return response


def main():
rospy.init_node('service_server')

Expand Down

0 comments on commit fa9ece4

Please sign in to comment.