test_client_param_server.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 ## Unit test of rospy.client APIs for parameter server access
35 
36 PKG = 'test_rospy'
37 NAME = 'test_rospy_client_param_server'
38 
39 import sys, time
40 import math
41 import unittest
42 
43 import rospy
44 import rostest
45 from test_rospy.srv import *
46 
47 def test_param_filter(params):
48  return [p for p in params if not p.startswith('/roslaunch') and p not in ['/run_id', '/rosdistro', '/rosversion']]
49 
50 ## Test the rospy.client parameter server API:
51 ##- get_param
52 ##- set_param
53 ##- delete_param
54 ##- has_param
55 ##- get_param_names
56 class TestClientParamServer(unittest.TestCase):
57 
58  ## test get/has param against state initialized from rostest script
59  def test_get_has_param(self):
60  #test error conditions
61  try: rospy.get_param(None); self.fail("get_param(None) succeeded")
62  except: pass
63  try: rospy.get_param(''); self.fail("get_param('') succeeded")
64  except: pass
65  # should be keyerror
66  try: rospy.get_param('non-existent'); self.fail("get_param('non-existent') succeeded")
67  except: pass
68  try: rospy.has_param(None, 'foo'); self.fail("has_param(None) succeeded")
69  except: pass
70  try: rospy.has_param(''); self.fail("has_param('') succeeded")
71  except: pass
72 
73  self.failIf(rospy.has_param('non-existent'), "has_param('non-existent') succeeded")
74 
75  #validate get_param against values on the param server
76  rostest_tests = {
77  'string': "string",
78  'int0': 0,
79  'int10': 10,
80  'float0': 0.0,
81  'float10': 10.0,
82  "namespace/string": "namespaced string",
83  }
84  param_names = [rospy.resolve_name(k) for k in rostest_tests.keys()]
85 
86  # test get parameter names
87  diff = set(param_names) ^ set(test_param_filter(rospy.get_param_names()))
88  self.failIf(diff, diff)
89 
90  # test for existing and value
91  for k, v in rostest_tests.items():
92  self.assert_(rospy.has_param(k))
93  self.assert_(rospy.has_param(rospy.resolve_name(k)))
94  if not type(v) == float:
95  self.assertEqual(v, rospy.get_param(k))
96  self.assertEqual(v, rospy.get_param(rospy.resolve_name(k)))
97  else:
98  self.assertAlmostEqual(v, rospy.get_param(k), 1)
99  self.assertAlmostEqual(v, rospy.get_param(rospy.resolve_name(k)), 1)
100 
101  def test_search_param(self):
102  try:
103  orig_caller_id = rospy.names.get_caller_id()
104 
105  rospy.names._set_caller_id('/global_node')
106 
107  self.assertEquals(None, rospy.search_param('search_param'))
108  rospy.set_param('/search_param', 1)
109  self.assertEquals('/search_param', rospy.search_param('search_param'))
110 
111  rospy.names._set_caller_id('/level1/level2/relative_node')
112  self.assertEquals('/search_param', rospy.search_param('search_param'))
113  rospy.set_param('/level1/search_param', 2)
114  self.assertEquals('/level1/search_param', rospy.search_param('search_param'))
115  rospy.set_param('~search_param', 3)
116  # make sure that search starts in our private namespace first
117  self.assertEquals('/level1/level2/relative_node/search_param', rospy.search_param('search_param'))
118 
119  finally:
120  rospy.names._set_caller_id(orig_caller_id)
121 
122  def test_delete_param(self):
123  try: rospy.delete_param(None); self.fail("delete_param(None) succeeded")
124  except: pass
125  try: rospy.delete_param(''); self.fail("delete_param('') succeeded")
126  except: pass
127 
128  rostest_tests = {
129  'dpstring': "string",
130  'dpint0': 0,
131  'dpint10': 10,
132  'dpfloat0': 0.0,
133  'dpfloat10': 10.0,
134  "dpnamespace/string": "namespaced string",
135  }
136  initial_params = rospy.get_param_names()
137  # implicitly depends on set param
138  for k, v in rostest_tests.items():
139  rospy.set_param(k, v)
140 
141  # test delete
142  for k, v in rostest_tests.items():
143  self.assert_(rospy.has_param(k))
144  rospy.delete_param(k)
145  self.failIf(rospy.has_param(k))
146  self.failIf(rospy.has_param(rospy.resolve_name(k)))
147  try:
148  rospy.get_param(k)
149  self.fail("get_param should fail on deleted key")
150  except KeyError: pass
151 
152  # make sure no new state
153  self.failIf(set(initial_params) ^ set(rospy.get_param_names()))
154 
155  ## test set_param separately
156  def test_set_param(self):
157  try: rospy.set_param(None, 'foo'); self.fail("set_param(None) succeeded")
158  except: pass
159  try: rospy.set_param('', 'foo'); self.fail("set_param('') succeeded")
160  except: pass
161 
162  rostest_tests = {
163  'spstring': "string",
164  'spint0': 0,
165  'spint10': 10,
166  'spfloat0': 0.0,
167  'spfloat10': 10.0,
168  "spnamespace/string": "namespaced string",
169  }
170  initial_param_names = rospy.get_param_names()
171  param_names = [rospy.resolve_name(k) for k in rostest_tests.keys()]
172  for k, v in rostest_tests.items():
173  self.failIf(rospy.has_param(k))
174  self.failIf(rospy.has_param(rospy.resolve_name(k)))
175  rospy.set_param(k, v)
176  self.assert_(rospy.has_param(k))
177  self.assert_(rospy.has_param(rospy.resolve_name(k)))
178  self.assertEquals(v, rospy.get_param(k))
179  self.assertEquals(v, rospy.get_param(rospy.resolve_name(k)))
180  correct_state = set(initial_param_names + param_names)
181  self.failIf(correct_state ^ set(rospy.get_param_names()))
182 
183 if __name__ == '__main__':
184  rospy.init_node(NAME)
185  rostest.run(PKG, NAME, TestClientParamServer, sys.argv)
def test_set_param(self)
test set_param separately
def test_get_has_param(self)
test get/has param against state initialized from rostest script
Test the rospy.client parameter server API:


test_rospy
Author(s): Ken Conley
autogenerated on Sun Feb 3 2019 03:30:22