$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2008, Willow Garage, Inc. 00005 # All rights reserved. 00006 # 00007 # Redistribution and use in source and binary forms, with or without 00008 # modification, are permitted provided that the following conditions 00009 # are met: 00010 # 00011 # * Redistributions of source code must retain the above copyright 00012 # notice, this list of conditions and the following disclaimer. 00013 # * Redistributions in binary form must reproduce the above 00014 # copyright notice, this list of conditions and the following 00015 # disclaimer in the documentation and/or other materials provided 00016 # with the distribution. 00017 # * Neither the name of Willow Garage, Inc. nor the names of its 00018 # contributors may be used to endorse or promote products derived 00019 # from this software without specific prior written permission. 00020 # 00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 # POSSIBILITY OF SUCH DAMAGE. 00033 # 00034 # Revision $Id: test_empty_service.py 3803 2009-02-11 02:04:39Z rob_wheeler $ 00035 00036 PKG = 'test_rospy' 00037 NAME = 'test_latch' 00038 import roslib; roslib.load_manifest(PKG) 00039 00040 import sys 00041 import time 00042 import unittest 00043 00044 from std_msgs.msg import String 00045 00046 class TestLatch(unittest.TestCase): 00047 00048 def setUp(self): 00049 self.callback_invoked = {} 00050 for i in range(0, 6): 00051 self.callback_invoked[i] = False 00052 00053 def callback_args(self, msg, i): 00054 self.assertEquals('foo', msg.data) 00055 self.callback_invoked[i] = True 00056 00057 def callback(self, msg): 00058 self.assertEquals('foo', msg.data) 00059 self.callback_invoked[0] = True 00060 00061 def test_latch(self): 00062 import rospy 00063 00064 # multi-part test. First check that we get latched message, then check 00065 # that subscribers to same topic also receive latched message 00066 # #1852 00067 rospy.init_node(NAME) 00068 s0 = rospy.Subscriber('s', String, self.callback) 00069 # 20 seconds to receive first latched message 00070 timeout_t = time.time() + 20. 00071 print "waiting for 20 seconds" 00072 while not self.callback_invoked[0] and \ 00073 not rospy.is_shutdown() and \ 00074 timeout_t > time.time(): 00075 time.sleep(0.2) 00076 00077 self.failIf(timeout_t < time.time(), "timeout exceeded") 00078 self.failIf(rospy.is_shutdown(), "node shutdown") 00079 self.assert_(self.callback_invoked[0], "callback not invoked") 00080 00081 # register three more callbacks, make sure they get invoked with message 00082 # - callbacks are actually called inline, but in spirit of test, async callback is allowed 00083 for i in range(1, 5): 00084 self.failIf(self.callback_invoked[i]) 00085 s = rospy.Subscriber('s', String, self.callback_args, i) 00086 timeout_t = time.time() + 0.5 00087 while not self.callback_invoked[i] and \ 00088 not rospy.is_shutdown() and \ 00089 timeout_t > time.time(): 00090 time.sleep(0.1) 00091 self.assert_(self.callback_invoked[i]) 00092 00093 if __name__ == '__main__': 00094 import rostest 00095 rostest.run(PKG, NAME, TestLatch, sys.argv)