Commit e756352ff86900de9b9a046c8068302e96253d79
1 parent
e031b1a1
Exists in
dev
updated Agent : test scenario est maintenant une liste de tuples cmd, expected_r…
…esult et plante si result different de expected
Showing
2 changed files
with
39 additions
and
28 deletions
Show diff stats
doc/codestyle_examples/codestyle_first.py
... | ... | @@ -80,14 +80,12 @@ import random |
80 | 80 | # - (General) Module level Constants |
81 | 81 | # |
82 | 82 | |
83 | -DEBUG = False | |
84 | 83 | |
85 | 84 | IS_WINDOWS = platform.system() == "Windows" |
86 | 85 | |
86 | +DEBUG = False | |
87 | + | |
87 | 88 | |
88 | -# | |
89 | -# - (General) Module level Functions | |
90 | -# | |
91 | 89 | |
92 | 90 | # - Typehint : Union (but not yet '|', only available with python 3.10) |
93 | 91 | ... | ... |
src/core/pyros_django/agent/Agent.py
... | ... | @@ -420,32 +420,32 @@ class Agent: |
420 | 420 | |
421 | 421 | |
422 | 422 | # Agent general command |
423 | - "Agent set_mode ATTENTIVE", | |
423 | + ("Agent set_mode ATTENTIVE", "MODE = ATTENTIVE"), | |
424 | 424 | # => should get "ATTENTIVE" |
425 | - "self get_mode", | |
425 | + ("self get_mode", "MODE = ATTENTIVE"), | |
426 | 426 | # => should get "7" |
427 | - "self do_eval 3+5-1", | |
427 | + ("self do_eval 3+5-1", 7), | |
428 | 428 | |
429 | 429 | # END, will not go further |
430 | - "self do_exit", | |
430 | + ("self do_exit", "STOPPING"), | |
431 | 431 | |
432 | 432 | # Agent specific commands => should be executed |
433 | - "self do_specific3", | |
434 | - "self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", | |
433 | + ("self do_specific3", ), | |
434 | + ("self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", 7), | |
435 | 435 | |
436 | - "self set_mode ROUTINE", | |
436 | + ("self set_mode ROUTINE", "MODE = ROUTINE"), | |
437 | 437 | # => should get "ROUTINE" |
438 | - "self get_mode", | |
438 | + ("self get_mode", "MODE = ROUTINE"), | |
439 | 439 | # Agent specific command => should be skipped (because not ATTENTIVE) |
440 | - "self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", | |
440 | + ("self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", "SKIPPED"), | |
441 | 441 | |
442 | 442 | # From now on, should not run anymore process_before/after |
443 | 443 | # => and should skip next specific commands |
444 | - "self set_mode IDLE", | |
444 | + ("self set_mode IDLE", "MODE = IDLE"), | |
445 | 445 | # => should get "IDLE" |
446 | - "self get_mode", | |
446 | + ("self get_mode", "MODE = IDLE"), | |
447 | 447 | # Agent specific command => should be skipped (because not ATTENTIVE) |
448 | - "self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", | |
448 | + ("self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", 'SKIPPED'), | |
449 | 449 | |
450 | 450 | # TODO: test priority commands : do_abort, do_flush_cmds, ... |
451 | 451 | # - Stop executing new commands (just let them accumulate) |
... | ... | @@ -462,10 +462,10 @@ class Agent: |
462 | 462 | ##"self do_resume_exec", |
463 | 463 | |
464 | 464 | # Restart the restart loop (from init()) |
465 | - "self do_restart_loop", | |
465 | + ("self do_restart_loop", "RESTARTING"), | |
466 | 466 | |
467 | 467 | # Now stop |
468 | - "self do_exit", | |
468 | + ("self do_exit", "STOPPING"), | |
469 | 469 | |
470 | 470 | ''' |
471 | 471 | # specific0 not_executed_because_idle |
... | ... | @@ -1072,7 +1072,7 @@ class Agent: |
1072 | 1072 | ''' |
1073 | 1073 | |
1074 | 1074 | if not self.IS_IDLE(): self._routine_process_after() |
1075 | - # TEST MODE only : send next command | |
1075 | + # TEST MODE only : execute test routine_process always (even if IDLE) in order to (always) send next command from test scenario | |
1076 | 1076 | if self.TEST_MODE: self._TEST_test_routine_process() |
1077 | 1077 | |
1078 | 1078 | |
... | ... | @@ -1155,7 +1155,7 @@ class Agent: |
1155 | 1155 | #log.info("*"*10 + " NEXT COMMAND PROCESSING (START) " + "*"*10 + '\n') |
1156 | 1156 | |
1157 | 1157 | |
1158 | - # Purge commands (every N iterations, delete old commands) | |
1158 | + # Purge commands (every N iterations, starting from 1st, delete old commands) | |
1159 | 1159 | N=5 |
1160 | 1160 | if ((self._iter_num-1) % N) == 0: |
1161 | 1161 | log.info("Purging expired commands if exists") |
... | ... | @@ -1177,6 +1177,7 @@ class Agent: |
1177 | 1177 | log.info('-'*6) |
1178 | 1178 | log.info('-'*6 + " RECEIVED NEW COMMAND TO PROCESS: ") |
1179 | 1179 | log.info('-'*6 + str(cmd)) |
1180 | + if self.is_in_test_mode(): log.info(f"*** (with expected result : " + str(self._cmdts.res_expected) + ')') | |
1180 | 1181 | log.info('-'*6) |
1181 | 1182 | |
1182 | 1183 | cmd.set_read_time() |
... | ... | @@ -1209,6 +1210,10 @@ class Agent: |
1209 | 1210 | self._cleanup_before_exit() |
1210 | 1211 | if cmd.name != 'do_restart_loop': |
1211 | 1212 | self.DO_RESTART_LOOP = False |
1213 | + if self.is_in_test_mode(): | |
1214 | + #print(cmd.result) | |
1215 | + #print(self._cmdts.res_expected) | |
1216 | + assert(cmd.result == self._cmdts.res_expected) | |
1212 | 1217 | return |
1213 | 1218 | |
1214 | 1219 | # CASE 2 - AGENT SPECIFIC command |
... | ... | @@ -1875,13 +1880,13 @@ class Agent: |
1875 | 1880 | self._DO_EXIT=True |
1876 | 1881 | cmd.set_result('SHOULD BE DONE NOW') |
1877 | 1882 | ''' |
1878 | - result = "STOP or RESTART" | |
1883 | + result = "RESTARTING" if cmd_name == "do_restart_loop" else "STOPPING" | |
1879 | 1884 | |
1880 | 1885 | elif cmd_name == "get_state": |
1881 | 1886 | result = "I am now " + self.get_state() |
1882 | 1887 | |
1883 | 1888 | elif cmd_name == "get_mode": |
1884 | - result = "I am currently in mode " + self.get_mode() | |
1889 | + result = "MODE = " + self.get_mode() | |
1885 | 1890 | |
1886 | 1891 | elif cmd_name == "set_mode": |
1887 | 1892 | #if not cmd_args: raise ValueError() |
... | ... | @@ -1892,7 +1897,7 @@ class Agent: |
1892 | 1897 | elif mode == "ATTENTIVE": self.set_attentive() |
1893 | 1898 | else: raise AgentCmdBadArgsException(cmd) |
1894 | 1899 | #cmd.set_result("I am now " + state) |
1895 | - result = "I am now in mode " + mode | |
1900 | + result = "MODE = " + mode | |
1896 | 1901 | #time.sleep(1) |
1897 | 1902 | #self.waitfor(1) |
1898 | 1903 | |
... | ... | @@ -2156,7 +2161,7 @@ class Agent: |
2156 | 2161 | if self.TEST_MODE: log.info("in TEST MODE") |
2157 | 2162 | |
2158 | 2163 | def _TEST_get_next_command_to_send(self)->AgentCmd: |
2159 | - cmd_full_name = next(self.TEST_COMMANDS, None) | |
2164 | + (cmd_full_name, res_expected) = next(self.TEST_COMMANDS, (None,None)) | |
2160 | 2165 | #return cmd_name |
2161 | 2166 | if cmd_full_name is None: return None |
2162 | 2167 | # Remove excessive spaces |
... | ... | @@ -2167,10 +2172,8 @@ class Agent: |
2167 | 2172 | if agent_recipient == 'self': agent_recipient = self.name |
2168 | 2173 | cmd_name, cmd_args = cmd_name_and_args, None |
2169 | 2174 | if ' ' in cmd_name_and_args: cmd_name,cmd_args = cmd_name_and_args.split(' ', 1) |
2170 | - # Command is to be sent to myself ? | |
2171 | - ##recipient_agent = self.name if self.TEST_COMMANDS_DEST=="myself" else self.TEST_COMMANDS_DEST | |
2172 | - #return Command(sender=self.name, recipient=recipient_agent, name=cmd_name) | |
2173 | 2175 | cmd = self.create_cmd_for(agent_recipient, cmd_name, cmd_args) |
2176 | + cmd.res_expected = res_expected | |
2174 | 2177 | # If no cmd created (because of error, bad AgentDevice name), call again this method for next cmd |
2175 | 2178 | #if cmd is None: return self._TEST_get_next_command_to_send() |
2176 | 2179 | return cmd |
... | ... | @@ -2193,9 +2196,13 @@ class Agent: |
2193 | 2196 | def _TEST_test_routine_process(self): |
2194 | 2197 | """ |
2195 | 2198 | TEST MODE ONLY |
2199 | + | |
2200 | + Send next command from scenario defined in TEST_COMMANDS_LIST | |
2201 | + (only if previous command finished) | |
2196 | 2202 | """ |
2197 | 2203 | |
2198 | 2204 | log.info("(TEST mode) Trying to send a new command if possible...") |
2205 | + | |
2199 | 2206 | # There is a current command being processed |
2200 | 2207 | # => check if next command is "do_abort" |
2201 | 2208 | # => if so, instantly send a "do_abort" to abort previous command |
... | ... | @@ -2203,6 +2210,8 @@ class Agent: |
2203 | 2210 | log.info(f"Waiting for end execution of cmd '{self._cmdts.name}' (sent to {self._cmdts.recipient}) ...") |
2204 | 2211 | # Update cmdts fields from DB |
2205 | 2212 | self._cmdts.refresh_from_db() |
2213 | + | |
2214 | + # Current cmd is pending or running | |
2206 | 2215 | if self._cmdts.is_pending() or self._cmdts.is_running(): |
2207 | 2216 | if self._next_cmdts is None: |
2208 | 2217 | # If next command is "do_abort" then abort becomes the new current command (to be sent) |
... | ... | @@ -2222,6 +2231,7 @@ class Agent: |
2222 | 2231 | |
2223 | 2232 | # Current cmd is no more running |
2224 | 2233 | else: |
2234 | + | |
2225 | 2235 | # Execution was not completed |
2226 | 2236 | #if self._cmdts.is_expired() or self._cmdts.is_skipped() or self._cmdts.is_killed(): |
2227 | 2237 | if self._cmdts.is_skipped() or self._cmdts.is_killed(): |
... | ... | @@ -2239,7 +2249,8 @@ class Agent: |
2239 | 2249 | ''' |
2240 | 2250 | # - (2) Send next command |
2241 | 2251 | #self._cmdts = None |
2242 | - # Execution was not complete => get result | |
2252 | + | |
2253 | + # Execution was completeted => get result | |
2243 | 2254 | elif self._cmdts.is_executed(): |
2244 | 2255 | cmdts_res = self._cmdts.get_result() |
2245 | 2256 | print("toto") |
... | ... | @@ -2269,6 +2280,7 @@ class Agent: |
2269 | 2280 | log.info("***") |
2270 | 2281 | #self.printd(f"*** SEND ", self._cmdts) |
2271 | 2282 | log.info(f"*** NEW COMMAND TO SEND is: " + str(self._cmdts)) |
2283 | + log.info(f"*** (with expected result : " + str(self._cmdts.res_expected) + ')') | |
2272 | 2284 | log.info("***") |
2273 | 2285 | #self._cmdts.set_as_pending() |
2274 | 2286 | # SEND |
... | ... | @@ -2276,6 +2288,7 @@ class Agent: |
2276 | 2288 | #cmdts_is_processed = False |
2277 | 2289 | #cmdts_res = None |
2278 | 2290 | |
2291 | + | |
2279 | 2292 | def _TEST_test_results(self): |
2280 | 2293 | if self.TEST_COMMANDS_LIST == [] : return |
2281 | 2294 | nb_commands_to_send = len(self.TEST_COMMANDS_LIST) | ... | ... |