test_pubsub_order.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_pubsub_order'
41 
42 import sys
43 import time
44 import unittest
45 
46 import rospy
47 import rostest
48 from std_msgs.msg import String
49 
50 PUBTOPIC = "chatter"
51 LPNODE = 'listenerpublisher'
52 LPTOPIC = 'listenerpublisher'
53 MSG = String
54 
55 TIMEOUT = 10.0 #seconds
56 
57 class TestPubSubOrder(unittest.TestCase):
58 
59  def setUp(self):
60  self.callback_data = None
61 
63  self.callback_data = data
64 
65  ## Test subscriber first makes sure that if a subscriber is up first
66  ## that it is able to successfully receive messages from a new publisher
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_subscriber_first_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  pub.publish(MSG(msg))
91  time.sleep(0.1)
92 
93  # listenerpublisher is supposed to repeat our messages back onto /listenerpublisher,
94  # make sure we got it
95  self.assert_(self.callback_data is not None, "no callback data from listenerpublisher")
96  self.assertEquals(msg, self.callback_data.data, "callback data from listenerpublisher does not match")
97 
98 if __name__ == '__main__':
99  rospy.init_node(NAME)
100  rostest.run(PKG, NAME, TestPubSubOrder, sys.argv)
def _test_subscriber_first_callback(self, data)
def test_subscriber_first(self)
Test subscriber first makes sure that if a subscriber is up first that it is able to successfully rec...


test_rospy
Author(s): Ken Conley, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:56