1919import  subprocess 
2020from  tempfile  import  TemporaryFile 
2121
22- import  platform 
22+ import  pytest 
2323import  requests 
2424import  time 
2525
26- import  pytest 
27- 
2826from  tests  import  get_binary_file_path , clear_octobot_previous_folders , get_log_file_content , is_on_windows 
2927
3028logger  =  logging .getLogger ()
3129logger .setLevel (logging .DEBUG )
3230
3331BINARY_DISABLE_WEB_OPTION  =  "-nw" 
32+ LOG_CHECKS_MAX_ATTEMPTS  =  300 
3433
3534
3635@pytest .fixture  
3736def  start_binary ():
3837    clear_octobot_previous_folders ()
3938    with  TemporaryFile () as  output , TemporaryFile () as  err :
40-         binary_process  =  create_binary ("" , output , err )
41-         yield 
42-         terminate_binary (binary_process , output , err )
39+         binary_process  =  start_binary_process ("" , output , err )
40+         try :
41+             yield 
42+         except  Exception :
43+             pass 
44+         finally :
45+             terminate_binary (binary_process , output , err )
4346
4447
4548@pytest .fixture  
4649def  start_binary_without_web_app ():
4750    clear_octobot_previous_folders ()
4851    with  TemporaryFile () as  output , TemporaryFile () as  err :
49-         binary_process  =  create_binary (BINARY_DISABLE_WEB_OPTION , output , err )
50-         yield 
51-         terminate_binary (binary_process , output , err )
52+         binary_process  =  start_binary_process (BINARY_DISABLE_WEB_OPTION , output , err )
53+         logger .debug (err .read ())
54+         try :
55+             yield 
56+         except  Exception :
57+             pass 
58+         finally :
59+             terminate_binary (binary_process , output , err )
5260
5361
54- def  create_binary (binary_options , output_file , err_file ):
62+ def  start_binary_process (binary_options , output_file , err_file ):
5563    logger .debug ("Starting binary process..." )
5664    return  subprocess .Popen (f"{ get_binary_file_path ()} { binary_options }  ,
5765                            shell = True ,
@@ -68,44 +76,64 @@ def terminate_binary(binary_process, output_file, err_file):
6876        raise  ValueError (f"Error happened during process execution : { errors }  )
6977    logger .debug ("Killing binary process..." )
7078    if  is_on_windows ():
71-         binary_process .kill ()
79+         os .kill (binary_process . pid ,  signal . CTRL_C_EVENT )
7280    else :
7381        try :
7482            os .killpg (os .getpgid (binary_process .pid ), signal .SIGTERM )  # Send the signal to all the process groups 
7583        except  ProcessLookupError :
7684            binary_process .kill ()
7785
7886
79- def  test_version_endpoint (start_binary ):
80-     max_attempts  =  10 
87+ def  multiple_checks (check_method , sleep_time = 1 , max_attempts = 10 , ** kwargs ):
8188    attempt  =  1 
8289    while  max_attempts  >=  attempt  >  0 :
8390        try :
84-             requests .get ('http://localhost:5001/version' )
85-             attempt  =  - 1   # success 
86-         except  requests .exceptions .ConnectionError :
87-             logger .warning (f"Failed to get http://localhost/version, retrying ({ attempt } { max_attempts }  )
91+             result  =  check_method (** kwargs )
92+             if  result :  # success 
93+                 return 
94+         except  Exception  as  e :
95+             logger .warning (f"Check ({ attempt } { max_attempts } { e }  )
96+         finally :
8897            attempt  +=  1 
89-             time .sleep (1 )
90-     assert  attempt   <=   max_attempts 
98+             time .sleep (sleep_time )
99+     assert  False    # fail 
91100
92101
93- def  test_evaluation_state_created (start_binary_without_web_app ):
94-     time .sleep (10 )
102+ def  check_endpoint (endpoint_url , expected_code ):
103+     try :
104+         result  =  requests .get (endpoint_url )
105+         return  result .status_code  ==  expected_code 
106+     except  requests .exceptions .ConnectionError :
107+         logger .warning (f"Failed to get { endpoint_url }  )
108+         return  False 
109+ 
110+ 
111+ def  check_logs_content (expected_content : str , should_appear : bool  =  True ):
95112    log_content  =  get_log_file_content ()
96113    logger .debug (log_content )
97-     assert  "new state:"  in  log_content 
114+     if  should_appear :
115+         return  expected_content  in  log_content 
116+     return  expected_content  not  in log_content 
117+ 
118+ 
119+ def  test_terms_endpoint (start_binary ):
120+     multiple_checks (check_endpoint , endpoint_url = "http://localhost:5001/terms" , expected_code = 200 )
121+ 
122+ 
123+ def  test_evaluation_state_created (start_binary_without_web_app ):
124+     multiple_checks (check_logs_content ,
125+                     max_attempts = LOG_CHECKS_MAX_ATTEMPTS ,
126+                     expected_content = "new state:" )
98127
99128
100129def  test_logs_content_has_no_errors (start_binary_without_web_app ):
101-     time . sleep ( 10 ) 
102-     log_content   =   get_log_file_content () 
103-     logger . debug ( log_content ) 
104-     assert   "ERROR"   not   in   log_content 
130+     multiple_checks ( check_logs_content , 
131+                      max_attempts = LOG_CHECKS_MAX_ATTEMPTS , 
132+                      expected_content = "ERROR" , 
133+                      should_appear = False ) 
105134
106135
107136def  test_balance_profitability_updated (start_binary_without_web_app ):
108-     time .sleep (10 )
109-     log_content  =  get_log_file_content ()
110-     logger .debug (log_content )
111-     assert  "BALANCE PROFITABILITY :"  in  log_content 
137+     multiple_checks (check_logs_content ,
138+                     max_attempts = LOG_CHECKS_MAX_ATTEMPTS ,
139+                     expected_content = "BALANCE PROFITABILITY :" )
0 commit comments