Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 __author__ = 'Kentaro Wada <www.kentaro.wada@gmail.com>'
00037
00038 import os
00039 import sys
00040
00041 import unittest
00042
00043 import rosgraph
00044 import rospy
00045 import rosmsg
00046 import roslib
00047
00048
00049 PKG = 'test_nodelet_topic_tools'
00050 NAME = 'test_lazy'
00051
00052
00053 class TestConnection(unittest.TestCase):
00054
00055 def __init__(self, *args):
00056 super(TestConnection, self).__init__(*args)
00057 rospy.init_node(NAME)
00058 self.master = rosgraph.Master(NAME)
00059
00060 def test_no_subscribers(self):
00061 check_connected_topics = rospy.get_param('~check_connected_topics')
00062
00063 _, subscriptions, _ = self.master.getSystemState()
00064 for check_topic in check_connected_topics:
00065 for topic, sub_node in subscriptions:
00066 if topic == rospy.get_namespace() + check_topic:
00067 raise ValueError('Found topic: {}'.format(check_topic))
00068
00069 def test_subscriber_appears(self):
00070 topic_type = rospy.get_param('~input_topic_type')
00071 check_connected_topics = rospy.get_param('~check_connected_topics')
00072 wait_time = rospy.get_param('~wait_for_connection', 0)
00073 msg_class = roslib.message.get_message_class(topic_type)
00074
00075 sub = rospy.Subscriber('~input', msg_class,
00076 self._cb_test_subscriber_appears)
00077 print('Waiting for connection for {} sec.'.format(wait_time))
00078 rospy.sleep(wait_time)
00079
00080 _, subscriptions, _ = self.master.getSystemState()
00081 for check_topic in check_connected_topics:
00082 for topic, sub_node in subscriptions:
00083 if topic == rospy.get_namespace() + check_topic:
00084 break
00085 else:
00086 raise ValueError('Not found topic: {}'.format(check_topic))
00087
00088 sub.unregister()
00089 rospy.sleep(1)
00090
00091
00092 _, subscriptions, _ = self.master.getSystemState()
00093 for check_topic in check_connected_topics:
00094 for topic, sub_node in subscriptions:
00095 if topic == rospy.get_namespace() + check_topic:
00096 raise ValueError('Found topic: {}'.format(check_topic))
00097
00098 def _cb_test_subscriber_appears(self, msg):
00099 pass
00100
00101
00102 if __name__ == "__main__":
00103 import rostest
00104 rostest.rosrun(PKG, NAME, TestConnection)