66import time
77from pprint import pprint
88import multiprocessing
9- sys .path .append ('..' )
10- import get_sample_db as sdb
9+ import os
10+ import rclpy
11+ import rclpy .time
12+ import yaml
13+
14+ # Add the mocha_core module path for imports
15+ current_dir = os .path .dirname (os .path .abspath (__file__ ))
16+ mocha_core_path = os .path .join (current_dir , ".." , "mocha_core" )
17+ sys .path .append (mocha_core_path )
18+
19+ import sample_db as sdb
1120import synchronize_channel as sync
12- import synchronize_utils as su
13- import database_server_utils as du
21+ import database as db
22+ import database_utils as du
1423from colorama import Fore , Style
1524
16- CONFIG_FILE = "testConfigs/robotConfigs.yml"
25+ # Load robot configs for ROS2
26+ current_dir = os .path .dirname (os .path .abspath (__file__ ))
27+ robot_configs_path = os .path .join (
28+ current_dir , ".." , "config" , "testConfigs" , "robot_configs.yaml"
29+ )
30+ with open (robot_configs_path , "r" ) as f :
31+ robot_configs = yaml .load (f , Loader = yaml .FullLoader )
32+
33+ client_timeout = 6.0
1734
1835class test (unittest .TestCase ):
1936 def setUp (self ):
2037 test_name = self ._testMethodName
2138 print ("\n " , Fore .RED , 20 * "=" , test_name , 20 * "=" , Style .RESET_ALL )
39+
40+ # Initialize ROS2 if not already done
41+ if not rclpy .ok ():
42+ rclpy .init ()
43+
44+ # Create a ROS2 node for testing
45+ self .node = rclpy .create_node ('test_delay_2robots_node' )
46+
47+ def tearDown (self ):
48+ # Shutdown ROS2 node
49+ if hasattr (self , 'node' ):
50+ self .node .destroy_node ()
51+
52+ time .sleep (1 )
53+ super ().tearDown ()
2254
2355 def test_delay_run (self ):
2456 self .maxDiff = None
2557 db_gs = sdb .get_sample_dbl ()
2658 db_r1 = sdb .get_sample_dbl ()
2759 db_r2 = sdb .get_sample_dbl ()
2860
29- node_gs_r1 = sync .Channel (db_gs , 'groundstation ' , 'charon' , CONFIG_FILE )
30- node_gs_r2 = sync .Channel (db_gs , 'groundstation ' , 'styx' , CONFIG_FILE )
61+ node_gs_r1 = sync .Channel (db_gs , 'basestation ' , 'charon' , robot_configs , client_timeout , self . node )
62+ node_gs_r2 = sync .Channel (db_gs , 'basestation ' , 'styx' , robot_configs , client_timeout , self . node )
3163
32- node_r1_gs = sync .Channel (db_r1 , 'charon' , 'groundstation ' , CONFIG_FILE )
33- node_r2_gs = sync .Channel (db_r2 , 'styx' , 'groundstation ' , CONFIG_FILE )
64+ node_r1_gs = sync .Channel (db_r1 , 'charon' , 'basestation ' , robot_configs , client_timeout , self . node )
65+ node_r2_gs = sync .Channel (db_r2 , 'styx' , 'basestation ' , robot_configs , client_timeout , self . node )
3466
35- node_r1_r2 = sync .Channel (db_r1 , 'charon' , 'styx' , CONFIG_FILE )
36- node_r2_r1 = sync .Channel (db_r2 , 'styx' , 'charon' , CONFIG_FILE )
67+ node_r1_r2 = sync .Channel (db_r1 , 'charon' , 'styx' , robot_configs , client_timeout , self . node )
68+ node_r2_r1 = sync .Channel (db_r2 , 'styx' , 'charon' , robot_configs , client_timeout , self . node )
3769
3870 pprint (db_gs .db )
3971 pprint (db_r1 .db )
@@ -42,17 +74,17 @@ def test_delay_run(self):
4274 node_gs_r1 .run ()
4375 node_gs_r2 .run ()
4476
45- dbm = su .DBMessage (1 , 'Node_1' , 1 , 2 , time .time () , bytes ('r1_data' , 'utf-8' ), False )
46- du . add_modify_data_dbl ( db_r1 , dbm )
77+ dbm = db .DBMessage (1 , 1 , 2 , 1 , rclpy . time .Time ( seconds = int ( time . time ())) , bytes ('r1_data' , 'utf-8' ))
78+ db_r1 . add_modify_data ( dbm )
4779
4880 node_gs_r1 .trigger_sync ()
4981 time .sleep (4 )
5082
5183 node_r1_gs .run ()
5284 node_r2_gs .run ()
5385
54- dbm = su .DBMessage (2 , 'Node_2' , 2 , 1 , time .time () , bytes ('r2_data' , 'utf-8' ), False )
55- du . add_modify_data_dbl ( db_r2 , dbm )
86+ dbm = db .DBMessage (2 , 2 , 1 , 2 , rclpy . time .Time ( seconds = int ( time . time ())) , bytes ('r2_data' , 'utf-8' ))
87+ db_r2 . add_modify_data ( dbm )
5688
5789 node_r1_r2 .run ()
5890
@@ -85,14 +117,14 @@ def test_multiple_trigger_sync(self):
85117 db_r1 = sdb .get_sample_dbl ()
86118 db_r2 = sdb .get_sample_dbl ()
87119
88- node_gs_r1 = sync .Channel (db_gs , 'groundstation ' , 'charon' , CONFIG_FILE )
89- node_gs_r2 = sync .Channel (db_gs , 'groundstation ' , 'styx' , CONFIG_FILE )
120+ node_gs_r1 = sync .Channel (db_gs , 'basestation ' , 'charon' , robot_configs , client_timeout , self . node )
121+ node_gs_r2 = sync .Channel (db_gs , 'basestation ' , 'styx' , robot_configs , client_timeout , self . node )
90122
91- node_r1_gs = sync .Channel (db_r1 , 'charon' , 'groundstation ' , CONFIG_FILE )
92- node_r2_gs = sync .Channel (db_r2 , 'styx' , 'groundstation ' , CONFIG_FILE )
123+ node_r1_gs = sync .Channel (db_r1 , 'charon' , 'basestation ' , robot_configs , client_timeout , self . node )
124+ node_r2_gs = sync .Channel (db_r2 , 'styx' , 'basestation ' , robot_configs , client_timeout , self . node )
93125
94- node_r1_r2 = sync .Channel (db_r1 , 'charon' , 'styx' , CONFIG_FILE )
95- node_r2_r1 = sync .Channel (db_r2 , 'styx' , 'charon' , CONFIG_FILE )
126+ node_r1_r2 = sync .Channel (db_r1 , 'charon' , 'styx' , robot_configs , client_timeout , self . node )
127+ node_r2_r1 = sync .Channel (db_r2 , 'styx' , 'charon' , robot_configs , client_timeout , self . node )
96128
97129# pprint(db_gs)
98130# pprint(db_r1)
@@ -101,8 +133,8 @@ def test_multiple_trigger_sync(self):
101133# node_r1_r2.run()
102134 node_r2_r1 .run ()
103135
104- dbm = su .DBMessage (1 , 'Node_1' , 1 , 2 , time .time () , bytes ('R1_data' , 'utf-8' ), False )
105- du . add_modify_data_dbl ( db_r1 , dbm )
136+ dbm = db .DBMessage (1 , 1 , 2 , 1 , rclpy . time .Time ( seconds = int ( time . time ())) , bytes ('R1_data' , 'utf-8' ))
137+ db_r1 . add_modify_data ( dbm )
106138
107139 node_r2_r1 .trigger_sync ()
108140 time .sleep (5 )
0 commit comments