From e756352ff86900de9b9a046c8068302e96253d79 Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Fri, 8 Jul 2022 12:59:15 +0200 Subject: [PATCH] updated Agent : test scenario est maintenant une liste de tuples cmd, expected_result et plante si result different de expected --- doc/codestyle_examples/codestyle_first.py | 6 ++---- src/core/pyros_django/agent/Agent.py | 61 +++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/doc/codestyle_examples/codestyle_first.py b/doc/codestyle_examples/codestyle_first.py index db910b0..2cde82f 100755 --- a/doc/codestyle_examples/codestyle_first.py +++ b/doc/codestyle_examples/codestyle_first.py @@ -80,14 +80,12 @@ import random # - (General) Module level Constants # -DEBUG = False IS_WINDOWS = platform.system() == "Windows" +DEBUG = False + -# -# - (General) Module level Functions -# # - Typehint : Union (but not yet '|', only available with python 3.10) diff --git a/src/core/pyros_django/agent/Agent.py b/src/core/pyros_django/agent/Agent.py index 3f92221..617daf7 100755 --- a/src/core/pyros_django/agent/Agent.py +++ b/src/core/pyros_django/agent/Agent.py @@ -420,32 +420,32 @@ class Agent: # Agent general command - "Agent set_mode ATTENTIVE", + ("Agent set_mode ATTENTIVE", "MODE = ATTENTIVE"), # => should get "ATTENTIVE" - "self get_mode", + ("self get_mode", "MODE = ATTENTIVE"), # => should get "7" - "self do_eval 3+5-1", + ("self do_eval 3+5-1", 7), # END, will not go further - "self do_exit", + ("self do_exit", "STOPPING"), # Agent specific commands => should be executed - "self do_specific3", - "self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", + ("self do_specific3", ), + ("self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", 7), - "self set_mode ROUTINE", + ("self set_mode ROUTINE", "MODE = ROUTINE"), # => should get "ROUTINE" - "self get_mode", + ("self get_mode", "MODE = ROUTINE"), # Agent specific command => should be skipped (because not ATTENTIVE) - "self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", + ("self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", "SKIPPED"), # From now on, should not run anymore process_before/after # => and should skip next specific commands - "self set_mode IDLE", + ("self set_mode IDLE", "MODE = IDLE"), # => should get "IDLE" - "self get_mode", + ("self get_mode", "MODE = IDLE"), # Agent specific command => should be skipped (because not ATTENTIVE) - "self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", + ("self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", 'SKIPPED'), # TODO: test priority commands : do_abort, do_flush_cmds, ... # - Stop executing new commands (just let them accumulate) @@ -462,10 +462,10 @@ class Agent: ##"self do_resume_exec", # Restart the restart loop (from init()) - "self do_restart_loop", + ("self do_restart_loop", "RESTARTING"), # Now stop - "self do_exit", + ("self do_exit", "STOPPING"), ''' # specific0 not_executed_because_idle @@ -1072,7 +1072,7 @@ class Agent: ''' if not self.IS_IDLE(): self._routine_process_after() - # TEST MODE only : send next command + # TEST MODE only : execute test routine_process always (even if IDLE) in order to (always) send next command from test scenario if self.TEST_MODE: self._TEST_test_routine_process() @@ -1155,7 +1155,7 @@ class Agent: #log.info("*"*10 + " NEXT COMMAND PROCESSING (START) " + "*"*10 + '\n') - # Purge commands (every N iterations, delete old commands) + # Purge commands (every N iterations, starting from 1st, delete old commands) N=5 if ((self._iter_num-1) % N) == 0: log.info("Purging expired commands if exists") @@ -1177,6 +1177,7 @@ class Agent: log.info('-'*6) log.info('-'*6 + " RECEIVED NEW COMMAND TO PROCESS: ") log.info('-'*6 + str(cmd)) + if self.is_in_test_mode(): log.info(f"*** (with expected result : " + str(self._cmdts.res_expected) + ')') log.info('-'*6) cmd.set_read_time() @@ -1209,6 +1210,10 @@ class Agent: self._cleanup_before_exit() if cmd.name != 'do_restart_loop': self.DO_RESTART_LOOP = False + if self.is_in_test_mode(): + #print(cmd.result) + #print(self._cmdts.res_expected) + assert(cmd.result == self._cmdts.res_expected) return # CASE 2 - AGENT SPECIFIC command @@ -1875,13 +1880,13 @@ class Agent: self._DO_EXIT=True cmd.set_result('SHOULD BE DONE NOW') ''' - result = "STOP or RESTART" + result = "RESTARTING" if cmd_name == "do_restart_loop" else "STOPPING" elif cmd_name == "get_state": result = "I am now " + self.get_state() elif cmd_name == "get_mode": - result = "I am currently in mode " + self.get_mode() + result = "MODE = " + self.get_mode() elif cmd_name == "set_mode": #if not cmd_args: raise ValueError() @@ -1892,7 +1897,7 @@ class Agent: elif mode == "ATTENTIVE": self.set_attentive() else: raise AgentCmdBadArgsException(cmd) #cmd.set_result("I am now " + state) - result = "I am now in mode " + mode + result = "MODE = " + mode #time.sleep(1) #self.waitfor(1) @@ -2156,7 +2161,7 @@ class Agent: if self.TEST_MODE: log.info("in TEST MODE") def _TEST_get_next_command_to_send(self)->AgentCmd: - cmd_full_name = next(self.TEST_COMMANDS, None) + (cmd_full_name, res_expected) = next(self.TEST_COMMANDS, (None,None)) #return cmd_name if cmd_full_name is None: return None # Remove excessive spaces @@ -2167,10 +2172,8 @@ class Agent: if agent_recipient == 'self': agent_recipient = self.name cmd_name, cmd_args = cmd_name_and_args, None if ' ' in cmd_name_and_args: cmd_name,cmd_args = cmd_name_and_args.split(' ', 1) - # Command is to be sent to myself ? - ##recipient_agent = self.name if self.TEST_COMMANDS_DEST=="myself" else self.TEST_COMMANDS_DEST - #return Command(sender=self.name, recipient=recipient_agent, name=cmd_name) cmd = self.create_cmd_for(agent_recipient, cmd_name, cmd_args) + cmd.res_expected = res_expected # If no cmd created (because of error, bad AgentDevice name), call again this method for next cmd #if cmd is None: return self._TEST_get_next_command_to_send() return cmd @@ -2193,9 +2196,13 @@ class Agent: def _TEST_test_routine_process(self): """ TEST MODE ONLY + + Send next command from scenario defined in TEST_COMMANDS_LIST + (only if previous command finished) """ log.info("(TEST mode) Trying to send a new command if possible...") + # There is a current command being processed # => check if next command is "do_abort" # => if so, instantly send a "do_abort" to abort previous command @@ -2203,6 +2210,8 @@ class Agent: log.info(f"Waiting for end execution of cmd '{self._cmdts.name}' (sent to {self._cmdts.recipient}) ...") # Update cmdts fields from DB self._cmdts.refresh_from_db() + + # Current cmd is pending or running if self._cmdts.is_pending() or self._cmdts.is_running(): if self._next_cmdts is None: # If next command is "do_abort" then abort becomes the new current command (to be sent) @@ -2222,6 +2231,7 @@ class Agent: # Current cmd is no more running else: + # Execution was not completed #if self._cmdts.is_expired() or self._cmdts.is_skipped() or self._cmdts.is_killed(): if self._cmdts.is_skipped() or self._cmdts.is_killed(): @@ -2239,7 +2249,8 @@ class Agent: ''' # - (2) Send next command #self._cmdts = None - # Execution was not complete => get result + + # Execution was completeted => get result elif self._cmdts.is_executed(): cmdts_res = self._cmdts.get_result() print("toto") @@ -2269,6 +2280,7 @@ class Agent: log.info("***") #self.printd(f"*** SEND ", self._cmdts) log.info(f"*** NEW COMMAND TO SEND is: " + str(self._cmdts)) + log.info(f"*** (with expected result : " + str(self._cmdts.res_expected) + ')') log.info("***") #self._cmdts.set_as_pending() # SEND @@ -2276,6 +2288,7 @@ class Agent: #cmdts_is_processed = False #cmdts_res = None + def _TEST_test_results(self): if self.TEST_COMMANDS_LIST == [] : return nb_commands_to_send = len(self.TEST_COMMANDS_LIST) -- libgit2 0.21.2