test_latch_unsubscribe.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer in the documentation and/or other materials provided
13 # with the distribution.
14 # * Neither the name of Willow Garage, Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
30 
31 PKG = 'test_rospy'
32 NAME = 'test_latch_unsubscribe'
33 
34 import os
35 import sys
36 import unittest
37 
38 import psutil
39 
40 from std_msgs.msg import String
41 
42 
43 def _get_connections(process_info):
44  if hasattr(process_info, 'connections'): # new naming
45  return process_info.connections()
46  elif hasattr(process_info, 'get_connections'): # old naming
47  return process_info.get_connections()
48  raise AttributeError('Wrong psutil version?')
49 
50 
51 def _get_connection_statii(process_info):
52  return (conn.status for conn in _get_connections(process_info))
53 
54 
55 class TestLatch(unittest.TestCase):
56 
57  def setUp(self):
58  pass
59 
60  def test_latch(self):
61  import rospy
62  proc_info = psutil.Process(os.getpid())
63  self.assertNotIn('CLOSE_WAIT', _get_connection_statii(proc_info),
64  'CLOSE_WAIT sockets already before the test. This '
65  'should not happen at all.')
66 
67  rospy.init_node(NAME)
68  pub = rospy.Publisher('chatter', String, latch=True)
69  pub.publish(String("hello"))
70  rospy.sleep(0.5)
71  self.assertNotIn('CLOSE_WAIT', _get_connection_statii(proc_info),
72  'CLOSE_WAIT sockets after the subscriber exited. '
73  '(#107)')
74  rospy.sleep(1.5)
75  # also check for a second subscriber
76  self.assertNotIn('CLOSE_WAIT', _get_connection_statii(proc_info),
77  'CLOSE_WAIT sockets after the second subscriber '
78  'exited. (#107)')
79 
80 
81 if __name__ == '__main__':
82  import rostest
83  rostest.run(PKG, NAME, TestLatch, sys.argv)
def _get_connection_statii(process_info)
def _get_connections(process_info)


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