Commit c7318a4a95e38de61feca88670a0e70ee00fc431

Authored by Etienne Pallier
1 parent bde11e9b
Exists in dev

Nouveau mode test (simulateur)

Scenario de test :

	- lancer agents A et B en mode simu (option -t):
	./pyros.py -t start agentA,agentB

	- attendre 1 à 2mn jusqu'à obtenir les 2 résultats suivants:
	(AgentA): Finished testing => result is ok
	(AgentB): Finished testing => result is ok
README.md
... ... @@ -73,28 +73,25 @@ Author: E. Pallier
73 73  
74 74 VERSION: 0.20.25
75 75  
76   -Comment:
77   - pyros.py peut lancer plusieurs agents (A et B) en même temps (+ flush_commands + test)
  76 +Comment: mode "test" (-t) géré (= mode simulateur)
78 77  
79 78 - Scenario de test :
80   - - S'assurer que AgentA.py et AgentB.py sont en mode simulateur (SIMULATOR_MODE=True)
81   - - lancer agents A et B : ./pyros.py start agentA,agentB
  79 + - lancer agents A et B en mode simu (option -t): ./pyros.py -t start agentA,agentB
82 80 - attendre 1 à 2mn jusqu'à obtenir les 2 résultats suivants:
83 81 (AgentA): Finished testing => result is ok
84 82 (AgentB): Finished testing => result is ok
85   -
86   - - Mode opératoire pour lancer un agent :
87   - - S'assurer que AgentA.py et AgentB.py ne sont PAS en mode simulateur (SIMULATOR_MODE=False)
  83 + - Mode opératoire pour lancer un agent (en mode normal, hors test) :
88 84 - pour lancer agentA seulement : ./pyros.py start agentA [-c configfile]
89 85 - pour lancer plusieurs agents : ./pyros.py start agentA,agentB,... [-c configfile]
90 86 (ou encore: activer l'environnement virtuel, puis lancer "cd src/agent/ ; ./AgentA.py configfile")
91 87 - pour utiliser thread ou processus : il suffit de mettre la constante RUN_IN_THREAD de AgentA (ou AgentB ou AgentX) à False ou True
92 88  
93 89 - Autres remarques:
  90 + - pyros.py peut lancer plusieurs agents (A et B) en même temps
94 91 - send_command() implemented
95 92 - EVAL is now a generic command
96 93 - mode DEBUG (2 niveaux pour print)
97   - - Nouvelle commande "flush_commands" pour purger les commmandes en attente
  94 + - flush_command : nouvelle commande pour purger les commmandes en attente
98 95 - routine_process() implemented
99 96 - Eval command implemented
100 97 - Timeout géré : si commande pas exécutée en temps raisonnable => la même commande est ré-exéuctée à l'itération suivante
... ...
pyros.py
... ... @@ -379,11 +379,14 @@ def start(agent:str, configfile:str):
379 379 ##agentX.run(FOR_REAL=True)
380 380 os.chdir("src/agent/")
381 381 #cmd = "-m AgentX"
382   - cmd = f" Agent{agent_name[5:]}.py {configfile}"
383   -
384   - #if not test_mode(): execProcessFromVenv(cmd)
385   - #if not test_mode(): current_processes.append( (execProcessFromVenvAsync(cmd), agent_name, 0) )
386   - if not test_mode(): current_processes.append( [execProcessFromVenvAsync(cmd), agent_name, -1] )
  382 + #cmd = f" Agent{agent_name[5:]}.py {configfile}"
  383 + cmd = f"Agent{agent_name[5:]}.py"
  384 + if test_mode(): cmd += " -t"
  385 + if configfile: cmd += " {configfile}"
  386 + #if not test_mode(): current_processes.append( [execProcessFromVenvAsync(cmd), agent_name, -1] )
  387 + # Append this process ( [process id, agent_name, result=failure] )
  388 + # ("result" will be updated at the end of execution)
  389 + current_processes.append( [execProcessFromVenvAsync(cmd), agent_name, -1] )
387 390 # self._change_dir("..")
388 391 os.chdir(current_dir)
389 392  
... ...
src/agent/Agent.py
... ... @@ -194,8 +194,8 @@ class Agent:
194 194 # Maximum duration of this agent (only for SIMULATION mode)
195 195 # If set to None, it will never exit except if asked (or CTRL-C)
196 196 # If set to 20, it will exit after 20s
197   - MAX_DURATION_SEC = None
198   - #MAX_DURATION_SEC = 30
  197 + SIMULATOR_MAX_DURATION_SEC = None
  198 + #SIMULATOR_MAX_DURATION_SEC = 30
199 199  
200 200 # FOR TEST ONLY
201 201 # Run this agent in simulator mode
... ... @@ -338,7 +338,6 @@ class Agent:
338 338 def __init__(self, name:str="Agent", config_filename:str=None, RUN_IN_THREAD=True):
339 339 self.name = name
340 340 self.SIMULATOR_COMMANDS = iter(self.SIMULATOR_COMMANDS_LIST)
341   - if not self.SIMULATOR_MODE: self.MAX_DURATION_SEC=None
342 341 self.RUN_IN_THREAD = RUN_IN_THREAD
343 342 self.set_status(self.STATUS_LAUNCH)
344 343 self.set_idle()
... ... @@ -409,6 +408,12 @@ class Agent:
409 408 FOR_REAL: set to False if you don't want Majordome to send commands to devices
410 409 """
411 410  
  411 + if self.SIMULATOR_MODE:
  412 + self.print("[IN SIMULATOR MODE]")
  413 + else:
  414 + self.print("[IN NORMAL MODE]")
  415 + self.SIMULATOR_MAX_DURATION_SEC=None
  416 +
412 417 start_time = time.time()
413 418  
414 419 self.FOR_REAL = FOR_REAL
... ... @@ -498,8 +503,8 @@ class Agent:
498 503 self._iter_num += 1
499 504  
500 505 # Exit if max duration is reached
501   - if self.MAX_DURATION_SEC and (time.time()-start_time > self.MAX_DURATION_SEC):
502   - self.print("Exit because of max duration set to ", self.MAX_DURATION_SEC, "s")
  506 + if self.SIMULATOR_MAX_DURATION_SEC and (time.time()-start_time > self.SIMULATOR_MAX_DURATION_SEC):
  507 + self.print("Exit because of max duration set to ", self.SIMULATOR_MAX_DURATION_SEC, "s")
503 508 self.kill_running_specific_cmd_if_exists()
504 509 if self.SIMULATOR_MODE and self.SIMULATOR_WITH_TEST: self.simulator_test_results()
505 510 break
... ... @@ -1015,6 +1020,9 @@ class Agent:
1015 1020 =================================================================
1016 1021 """
1017 1022  
  1023 + def setSimulatorMode(self, mode):
  1024 + self.SIMULATOR_MODE=mode
  1025 +
1018 1026 def simulator_get_next_command_to_send(self)->Command:
1019 1027 cmd_name = next(self.SIMULATOR_COMMANDS, None)
1020 1028 #return cmd_name
... ... @@ -1258,19 +1266,33 @@ class Agent:
1258 1266 """
1259 1267  
1260 1268  
  1269 +
1261 1270 """
1262 1271 =================================================================
1263   - MAIN FUNCTION
  1272 + MAIN
1264 1273 =================================================================
1265 1274 """
1266   -if __name__ == "__main__":
1267 1275  
  1276 +def extract_parameters():
  1277 + """ Usage: Agent.py [-t] [configfile] """
  1278 + # arg 1 : -t
  1279 + # arg 2 : configfile
  1280 + TEST_MODE = False
1268 1281 configfile = None
  1282 + if len(sys.argv) > 1:
  1283 + if sys.argv[1] == "-t":
  1284 + TEST_MODE = True
  1285 + if len(sys.argv) == 3:
  1286 + configfile = sys.argv[2]
  1287 + else:
  1288 + configfile = sys.argv[1]
  1289 + return TEST_MODE, configfile
1269 1290  
1270   - # arg 1 : config file
1271   - if len(sys.argv) == 2:
1272   - configfile = sys.argv[1]
1273 1291  
  1292 +if __name__ == "__main__":
  1293 +
  1294 + TEST_MODE, configfile = extract_parameters()
1274 1295 agent = Agent("GenericAgent", configfile, RUN_IN_THREAD=True)
  1296 + agent.setSimulatorMode(TEST_MODE)
1275 1297 print(agent)
1276 1298 agent.run()
... ...
src/agent/AgentA.py
... ... @@ -5,7 +5,7 @@ import sys
5 5 ##import utils.Logger as L
6 6  
7 7 ##from .Agent import Agent
8   -from Agent import Agent
  8 +from Agent import Agent, extract_parameters
9 9  
10 10  
11 11 ##log = L.setupLogger("AgentXTaskLogger", "AgentX")
... ... @@ -14,14 +14,13 @@ from Agent import Agent
14 14  
15 15 class AgentA(Agent):
16 16  
17   - #MAX_DURATION_SEC = None
18   - MAX_DURATION_SEC = 120
19   -
20 17 # FOR TEST ONLY
21 18 # Run this agent in simulator mode
22   - SIMULATOR_MODE = True
  19 + SIMULATOR_MODE = False
23 20 # Run the assertion tests at the end
24 21 SIMULATOR_WITH_TEST = True
  22 + #SIMULATOR_MAX_DURATION_SEC = None
  23 + SIMULATOR_MAX_DURATION_SEC = 120
25 24 # Who should I send commands to ?
26 25 #SIMULATOR_COMMANDS_DEST = "myself"
27 26 SIMULATOR_COMMANDS_DEST = "AgentB"
... ... @@ -220,19 +219,20 @@ class AgentA(Agent):
220 219 =================================================================
221 220 """
222 221 if __name__ == "__main__":
223   -
224 222 # with thread
225 223 RUN_IN_THREAD=True
226 224 # with process
227 225 #RUN_IN_THREAD=False
228 226  
  227 + TEST_MODE, configfile = extract_parameters()
  228 + """
229 229 configfile = None
230   -
231 230 # arg 1 : config file
232 231 if len(sys.argv) == 2:
233 232 configfile = sys.argv[1]
234   -
  233 + """
235 234 #agent = AgentX()
236 235 agent = AgentA("AgentA", configfile, RUN_IN_THREAD)
  236 + agent.setSimulatorMode(TEST_MODE)
237 237 print(agent)
238 238 agent.run()
... ...
src/agent/AgentB.py
... ... @@ -5,7 +5,7 @@ import sys
5 5 ##import utils.Logger as L
6 6  
7 7 ##from .Agent import Agent
8   -from Agent import Agent
  8 +from Agent import Agent, extract_parameters
9 9  
10 10  
11 11 ##log = L.setupLogger("AgentXTaskLogger", "AgentX")
... ... @@ -14,14 +14,13 @@ from Agent import Agent
14 14  
15 15 class AgentB(Agent):
16 16  
17   - #MAX_DURATION_SEC = None
18   - MAX_DURATION_SEC = 120
19   -
20 17 # FOR TEST ONLY
21 18 # Run this agent in simulator mode
22 19 SIMULATOR_MODE = False
23 20 # Run the assertion tests at the end
24 21 SIMULATOR_WITH_TEST = True
  22 + #SIMULATOR_MAX_DURATION_SEC = None
  23 + SIMULATOR_MAX_DURATION_SEC = 120
25 24 # Who should I send commands to ?
26 25 #SIMULATOR_COMMANDS_DEST = "myself"
27 26 SIMULATOR_COMMANDS_DEST = "AgentA"
... ... @@ -188,17 +187,12 @@ class AgentB(Agent):
188 187 if __name__ == "__main__":
189 188  
190 189 # with thread
191   - #RUN_IN_THREAD=True
  190 + RUN_IN_THREAD=True
192 191 # with process
193   - RUN_IN_THREAD=False
194   -
195   - configfile = None
196   -
197   - # arg 1 : config file
198   - if len(sys.argv) == 2:
199   - configfile = sys.argv[1]
  192 + #RUN_IN_THREAD=False
200 193  
201   - #agent = AgentX()
  194 + TEST_MODE, configfile = extract_parameters()
202 195 agent = AgentB("AgentB", configfile, RUN_IN_THREAD)
  196 + agent.setSimulatorMode(TEST_MODE)
203 197 print(agent)
204 198 agent.run()
... ...
src/agent/AgentX.py
... ... @@ -9,7 +9,7 @@ import sys
9 9 #from common.models import Command
10 10  
11 11 ##from .Agent import Agent
12   -from Agent import Agent
  12 +from Agent import Agent, extract_parameters
13 13  
14 14  
15 15  
... ... @@ -22,9 +22,11 @@ class AgentX(Agent):
22 22  
23 23 # FOR TEST ONLY
24 24 # Run this agent in simulator mode
25   - SIMULATOR_MODE = True
  25 + SIMULATOR_MODE = False
26 26 # Run the assertion tests at the end
27 27 SIMULATOR_WITH_TEST = True
  28 + #SIMULATOR_MAX_DURATION_SEC = None
  29 + SIMULATOR_MAX_DURATION_SEC = 120
28 30 '''
29 31 # Who should I send commands to ?
30 32 #SIMULATOR_COMMANDS_DEST = "myself"
... ... @@ -164,15 +166,11 @@ if __name__ == "__main__":
164 166 # with thread
165 167 RUN_IN_THREAD=True
166 168 # with process
167   - RUN_IN_THREAD=False
168   -
169   - configfile = None
170   -
171   - # arg 1 : config file
172   - if len(sys.argv) == 2:
173   - configfile = sys.argv[1]
  169 + #RUN_IN_THREAD=False
174 170  
  171 + TEST_MODE, configfile = extract_parameters()
175 172 #agent = AgentX()
176 173 agent = AgentX("AgentX", configfile, RUN_IN_THREAD)
  174 + agent.setSimulatorMode(TEST_MODE)
177 175 print(agent)
178 176 agent.run()
... ...