test_embed_msg.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 ## Integration test for empty services to test serializers
35 ## and transport
36 
37 from __future__ import print_function
38 
39 PKG = 'test_rospy'
40 NAME = 'test_embed_msg'
41 
42 import sys
43 import time
44 import unittest
45 
46 import rospy
47 import rostest
48 from std_msgs.msg import String, Int32
49 from test_rospy.msg import EmbedTest, Val, ArrayVal
50 
51 PUBTOPIC = "chatter"
52 LPNODE = 'listenerpublisher'
53 LPTOPIC = 'listenerpublisher'
54 
55 MSG = EmbedTest
56 
57 TIMEOUT = 10.0 #seconds
58 
59 class TestEmbedMsg(unittest.TestCase):
60 
61  def setUp(self):
62  self.callback_data = None
63 
64  def _test_embed_msg_callback(self, data):
65  self.callback_data = data
66 
67  def test_embed_msg(self):
68  self.assert_(self.callback_data is None, "invalid test fixture")
69 
70  # wait at most 5 seconds for listenerpublisher to be registered
71  timeout_t = time.time() + 5.0
72  while not rostest.is_subscriber(
73  rospy.resolve_name(PUBTOPIC),
74  rospy.resolve_name(LPNODE)) and time.time() < timeout_t:
75  time.sleep(0.1)
76 
77  self.assert_(rostest.is_subscriber(
78  rospy.resolve_name(PUBTOPIC),
79  rospy.resolve_name(LPNODE)), "%s is not up"%LPNODE)
80 
81  print("Publishing to ", PUBTOPIC)
82  pub = rospy.Publisher(PUBTOPIC, MSG)
83  rospy.Subscriber(LPTOPIC, MSG, self._test_embed_msg_callback)
84 
85  # publish about 10 messages for fun
86  import random
87  val = random.randint(0, 109812312)
88  msg = "hi [%s]"%val
89  for i in range(0, 10):
90  # The test message could be better in terms of the values
91  # it assigns to leaf fields, but the main focus is trying
92  # to dig up edge conditions in the embeds, especially with
93  # respect to arrays and embeds.
94  pub.publish(
95  MSG(String(msg), Int32(val),
96  [Int32(val+1), Int32(val+2), Int32(val+3)],
97  Val(msg+msg),
98  [Val(msg), Val("two")],
99  [ArrayVal([Val("av1"), Val("av2")]), #[Val("%s"%i) for i in range(0, 10)]),
100  ArrayVal([]) #,[Val("%s"%i) for i in range(0, 10)]),
101  ]
102  ))
103  time.sleep(0.1)
104 
105  # listenerpublisher is supposed to repeat our messages back onto /listenerpublisher,
106  # make sure we got it
107  self.assert_(self.callback_data is not None, "no callback data from listenerpublisher")
108  print("Got ", self.callback_data.str1.data, self.callback_data.int1.data)
109  errorstr = "callback msg field [%s] from listenerpublisher does not match"
110  self.assertEquals(msg, self.callback_data.str1.data,
111  errorstr%"str1.data")
112  self.assertEquals(val, self.callback_data.int1.data,
113  errorstr%"int1.data")
114  for i in range(1, 4):
115  self.assertEquals(val+i, self.callback_data.ints[i-1].data,
116  errorstr%"ints[i-1].data")
117  self.assertEquals(msg+msg, self.callback_data.val.val,
118  errorstr%"val.val")
119  self.assertEquals(msg, self.callback_data.vals[0].val,
120  errorstr%"vals[0].val")
121  self.assertEquals("two", self.callback_data.vals[1].val,
122  errorstr%"vals[1].val")
123  # #435: test array of arrays
124  self.assertEquals(2, len(self.callback_data.arrayval),
125  errorstr%"len arrayval")
126  self.assertEquals(2, len(self.callback_data.arrayval[0].vals),
127  errorstr%"len arrayval[0].vals")
128  self.assertEquals("av1", self.callback_data.arrayval[0].vals[0].val,
129  errorstr%"arrayval[0].vals[0].val")
130  self.assertEquals("av2", self.callback_data.arrayval[0].vals[1].val,
131  errorstr%"arrayval[0].vals[1].val")
132  self.assertEquals(0, len(self.callback_data.arrayval[1].vals),
133  errorstr%"len arrayval[1].vals")
134 
135 
136 if __name__ == '__main__':
137  rospy.init_node(NAME)
138  rostest.run(PKG, NAME, TestEmbedMsg, sys.argv)
def _test_embed_msg_callback(self, data)


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