Salome HOME
6ed7c4ad8c75dcdbd9381480a4d9a642b628bba1
[modules/yacs.git] / src / yacsloader_swig / Test / testExec.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2006-2023  CEA, EDF
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import time
22 import unittest
23 import threading
24 import tempfile
25 import os
26
27 import SALOMERuntime
28 import loader
29 import pilot
30
31 class TestExec(unittest.TestCase):
32
33     def setUp(self):
34         SALOMERuntime.RuntimeSALOME_setRuntime(1)
35         self.l = loader.YACSLoader()
36         self.e = pilot.ExecutorSwig()
37         self.p = self.l.load("samples/aschema.xml")
38         self.workdir = tempfile.mkdtemp(suffix=".yacstest")
39         pass
40         
41     def test1_StepByStep(self):
42         # --- execution step by step
43        
44         print("================= Start of STEPBYSTEP ===================")
45         self.e.setExecMode(1) # YACS::STEPBYSTEP
46         
47         run1 = threading.Thread(None, self.e.RunPy, "stepbystep", (self.p,0))
48         run1.start()
49         time.sleep(0.1)       # let the thread be initialised 
50         #e.displayDot(self.p)
51        
52         tocont = True
53         while tocont:
54             self.e.waitPause()
55             #e.displayDot(p)
56             bp = self.e.getTasksToLoad()
57             print("nexts possible steps = ", bp)
58             if len(bp) > 0:
59                 tte= bp[-1:] # only one node at each step, the last one in the list
60                 r = self.e.setStepsToExecute(tte)
61                 self.e.resumeCurrentBreakPoint()
62                 tocont = self.e.isNotFinished()
63             else:
64                 tocont = False
65                 pass
66             print("toContinue = ", tocont)
67             pass
68         
69         self.e.resumeCurrentBreakPoint()
70         run1.join()
71         self.assertEqual(106, self.p.getChildByName('node48').getEffectiveState())
72         self.assertEqual(999, self.p.getChildByName('node13').getEffectiveState())
73         self.assertEqual(888, self.p.getChildByName('node14').getEffectiveState())
74         self.assertEqual(777, self.p.getChildByName('c1').getEffectiveState())
75         self.assertEqual(777, self.p.getChildByName('c1.c1.n2').getEffectiveState())
76         self.assertEqual(106, self.p.getChildByName('c0.c1.n1').getEffectiveState())
77         self.assertEqual(999, self.p.getChildByName('c0.n2').getEffectiveState())
78         self.assertEqual(888, self.p.getChildByName('node62').getEffectiveState())
79         print("================= End of STEPBYSTEP =====================")
80         pass
81
82     def test2_StopToBreakpoint(self):
83         # --- start execution, set a breakpoint before node48, then continue
84         time.sleep(1)
85         print("================= Start of BREAKPOINT ===================")
86         brp=['node48']
87         self.e.setListOfBreakPoints(brp)
88         self.e.setExecMode(2) # YACS::STOPBEFORENODES
89         self.run2 = threading.Thread(None, self.e.RunPy, "breakpoint", (self.p,0))
90         self.run2.start()
91         time.sleep(0.1)
92         self.e.waitPause()
93         #self.e.displayDot(p)
94         print("================= reach BREAKPOINT ======================")
95         # --- resume from breakpoint
96         print("=========== BREAKPOINT, start RESUME ====================")
97         time.sleep(1)
98         self.e.setExecMode(0) # YACS::CONTINUE
99         self.e.resumeCurrentBreakPoint()
100         time.sleep(0.1)
101         self.e.waitPause()
102         #self.e.displayDot(p)
103         self.run2.join()
104         self.assertEqual(106, self.p.getChildByName('node48').getEffectiveState())
105         self.assertEqual(999, self.p.getChildByName('node13').getEffectiveState())
106         self.assertEqual(888, self.p.getChildByName('node14').getEffectiveState())
107         self.assertEqual(777, self.p.getChildByName('c1').getEffectiveState())
108         self.assertEqual(777, self.p.getChildByName('c1.c1.n2').getEffectiveState())
109         self.assertEqual(106, self.p.getChildByName('c0.c1.n1').getEffectiveState())
110         self.assertEqual(999, self.p.getChildByName('c0.n2').getEffectiveState())
111         self.assertEqual(888, self.p.getChildByName('node62').getEffectiveState())
112         print("================= End of RESUME =========================")
113         pass
114     
115     def test3_RunWithoutBreakpoints(self):
116         # --- start execution, run without breakpoints
117         time.sleep(1)
118         
119         print("================= Start of CONTINUE =====================")
120         self.e.setExecMode(0) # YACS::CONTINUE
121         run3 = threading.Thread(None, self.e.RunPy, "continue", (self.p,0))
122         run3.start()
123         time.sleep(0.1)
124         self.e.waitPause()
125         #self.e.displayDot(p)
126         run3.join()
127         self.assertEqual(106, self.p.getChildByName('node48').getEffectiveState())
128         self.assertEqual(999, self.p.getChildByName('node13').getEffectiveState())
129         self.assertEqual(888, self.p.getChildByName('node14').getEffectiveState())
130         self.assertEqual(777, self.p.getChildByName('c1').getEffectiveState())
131         self.assertEqual(777, self.p.getChildByName('c1.c1.n2').getEffectiveState())
132         self.assertEqual(106, self.p.getChildByName('c0.c1.n1').getEffectiveState())
133         self.assertEqual(999, self.p.getChildByName('c0.n2').getEffectiveState())
134         self.assertEqual(888, self.p.getChildByName('node62').getEffectiveState())
135         print("================= End of CONTINUE =======================")
136         pass
137
138     def test4_StopOnError(self):
139         # --- stop execution on first error and save state
140         time.sleep(1)
141
142         print("================= Start of STOPONERROR ==================")
143         self.e.setStopOnError()
144         run4 = threading.Thread(None, self.e.RunPy, "continue", (self.p,0))
145         run4.start()
146         time.sleep(0.1)
147         self.e.waitPause()
148         self.e.saveState(os.path.join(self.workdir, "dumpErrorASchema.xml"))
149         self.e.setStopOnError(False)
150         self.e.resumeCurrentBreakPoint()
151         time.sleep(0.1)
152         self.e.waitPause()
153         run4.join()
154         #self.e.displayDot(self.p)
155         s13 = self.p.getChildByName('node13').getEffectiveState()
156         s43 = self.p.getChildByName('node43').getEffectiveState()
157         self.assertTrue((s13==999) or (s43==999))
158         print("================= End of STOPONERROR =====================")
159         pass
160
161     def test5_PartialExec(self):
162         # --- stop execution after breakpoint
163         time.sleep(1)
164
165         print("================= Start of PARTIALEXEC ===================")
166         brp=['node35']
167         self.e.setListOfBreakPoints(brp)
168         self.e.setExecMode(2) # YACS::STOPBEFORENODES
169         #self.e.displayDot(self.p)
170         run5 = threading.Thread(None, self.e.RunPy, "breakpoint", (self.p,0))
171         run5.start()
172         time.sleep(0.1)
173         self.e.waitPause()
174         #self.e.displayDot(self.p)
175         self.e.saveState(os.path.join(self.workdir, 'dumpPartialASchema.xml'))
176         #self.e.displayDot(self.p)
177         self.e.stopExecution()
178         run5.join()
179         #self.e.displayDot(self.p)
180         self.assertEqual(106, self.p.getChildByName('node34').getEffectiveState())
181         self.assertEqual(101, self.p.getChildByName('node35').getEffectiveState())
182         print("================= reach BREAKPOINT PARTIAL EXEC ==========")
183         pass
184
185     pass
186
187 if __name__ == '__main__':
188   import salome
189   import NamingService
190   import os
191   import subprocess
192   salome.salome_init()
193   ior = NamingService.NamingService.IOROfNS()
194   p = subprocess.Popen(["../yacsloader/echoSrv",ior])
195   import time
196   time.sleep(3)
197   with tempfile.TemporaryDirectory(suffix=".yacstest") as dir_test:
198     file_test = os.path.join(dir_test,"UnitTestsResult")
199     with open(file_test, 'a') as f:
200       f.write("  --- TEST src/yacsloader: testExec.py\n")
201       suite = unittest.makeSuite(TestExec)
202       result = unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
203   p.terminate()
204   sys.exit(not result.wasSuccessful())