1919import pdb
2020import rclpy
2121import rclpy .logging
22+ from rclpy .logging import LoggingSeverity
23+ from rclpy .executors import SingleThreadedExecutor
24+ from rclpy .node import Node
25+ import threading
2226from colorama import Fore , Style
27+ import mocha_core .zmq_comm_node as zmq_comm_node
28+
29+ class Comm_node_test (Node ):
30+ def __init__ (self ):
31+ super ().__init__ ("comm_node_test" )
2332
2433
2534class Test (unittest .TestCase ):
@@ -28,44 +37,71 @@ def setUpClass(cls):
2837 # Load configurations at the class level
2938 current_dir = os .path .dirname (os .path .abspath (__file__ ))
3039 ddb_path = os .path .join (current_dir , ".." )
31-
40+
3241 # Load robot configs
3342 robot_configs_path = os .path .join (ddb_path , "config/testConfigs/robot_configs.yaml" )
3443 with open (robot_configs_path , "r" ) as f :
3544 cls .robot_configs = yaml .load (f , Loader = yaml .FullLoader )
3645
46+
3747 def setUp (self ):
3848 test_name = self ._testMethodName
3949 print ("\n " , Fore .RED , 20 * "=" , test_name , 20 * "=" , Style .RESET_ALL )
50+ # Create a mock node to run the tests
51+ rclpy .init ()
52+ self .comm_node_test = Comm_node_test ()
53+ self .executor = SingleThreadedExecutor ()
54+ self .executor .add_node (self .comm_node_test )
55+ executor_thread = threading .Thread (target = self .executor .spin , daemon = True )
56+ executor_thread .start ()
4057 super ().setUp ()
4158
4259 def tearDown (self ):
4360 time .sleep (1 )
61+ self .executor .shutdown ()
62+ self .comm_node_test .destroy_node ()
63+ rclpy .shutdown ()
4464 super ().tearDown ()
4565
4666 def test_simple_connection (self ):
47- self .answer = None
48- logger = rclpy .logging .get_logger ('test_zmq_comm_node' )
49-
50- def cb_groundstation (value ):
51- logger .debug ("cb_groundstation" )
52- self .answer = value
53-
54- def cb_charon (value ):
67+ self .answer_cb_gs_client = None
68+ self .answer_cb_ch_client = None
69+ logger = self .comm_node_test .get_logger ()
70+ logger .set_level (LoggingSeverity .DEBUG )
71+
72+ def cb_groundstation_client (value ):
73+ logger .debug (f"cb groundstation client" )
74+ self .answer_cb_gs_client = value
75+
76+ def cb_groundstation_server (value ):
77+ # This function is called upon reception of a message by the base
78+ # station
79+ logger .debug ("cb groundstation server" )
80+ to_append = b"GSSERVER"
81+ return value + to_append
82+
83+ def cb_charon_client (value ):
84+ logger .debug (f"cb charon client" )
85+ self .answer_cb_ch_client = value
86+
87+ def cb_charon_server (value ):
5588 # This function is called upon reception of a message by charon.
5689 # The return value is transmitted as answer to the original
57- # message.
58- logger .debug (f"cb_charon: { value } " )
59- return value
90+ # message + b"CHARON"
91+ logger .debug (f"cb charon server" )
92+ to_append = b"CHARONSERVER"
93+ return value + to_append
6094
6195 # Create the two robots
6296 node_groundstation = zmq_comm_node .Comm_node (
6397 "basestation" , "charon" , self .robot_configs ,
64- cb_groundstation , cb_groundstation , 2
98+ cb_groundstation_client , cb_groundstation_server ,
99+ 2 , self .comm_node_test
65100 )
66101 node_charon = zmq_comm_node .Comm_node (
67102 "charon" , "basestation" , self .robot_configs ,
68- cb_charon , cb_charon , 2
103+ cb_charon_client , cb_charon_server ,
104+ 2 , self .comm_node_test
69105 )
70106
71107 # Generate random message
@@ -76,21 +112,15 @@ def cb_charon(value):
76112
77113 # Send message from node_groundstation to robot 2
78114 node_groundstation .connect_send_message (random_msg )
79-
80- # node_charon.connect_send_message(random_msg)
115+ node_charon .connect_send_message (random_msg )
81116
82117 # Terminate robots and test assertion
83118 node_groundstation .terminate ()
84119 node_charon .terminate ()
85- self .assertEqual (random_msg , self .answer , "Sent %s" % random_msg )
86-
87-
88- # Add the mocha_core module path for imports
89- current_dir = os .path .dirname (os .path .abspath (__file__ ))
90- mocha_core_path = os .path .join (current_dir , ".." , "mocha_core" )
91- sys .path .append (mocha_core_path )
92-
93- import mocha_core .zmq_comm_node as zmq_comm_node
120+ self .assertEqual (random_msg + b"CHARONSERVER" , self .answer_cb_gs_client ,
121+ "Sent %s" % random_msg )
122+ self .assertEqual (random_msg + b"GSSERVER" , self .answer_cb_ch_client ,
123+ "Sent %s" % random_msg )
94124
95125if __name__ == "__main__" :
96126 # Run test cases!
0 commit comments