Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / yacsloader / Test / testExec.py
1 #!/usr/bin/env python
2 #  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
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.
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 import time
21 import unittest
22 import threading
23
24 import SALOMERuntime
25 import loader
26 import pilot
27
28 class TestExec(unittest.TestCase):
29
30     def setUp(self):
31         SALOMERuntime.RuntimeSALOME_setRuntime(1)
32         self.l = loader.YACSLoader()
33         self.e = pilot.ExecutorSwig()
34         self.p = self.l.load("samples/aschema.xml")
35         pass
36         
37     def test1_StepByStep(self):
38         # --- execution step by step
39        
40         print "================= Start of STEPBYSTEP ==================="
41         self.e.setExecMode(1) # YACS::STEPBYSTEP
42         
43         run1 = threading.Thread(None, self.e.RunPy, "stepbystep", (self.p,0))
44         run1.start()
45         time.sleep(0.1)       # let the thread be initialised 
46         #e.displayDot(self.p)
47        
48         tocont = True
49         while tocont:
50             self.e.waitPause()
51             #e.displayDot(p)
52             bp = self.e.getTasksToLoad()
53             print "nexts possible steps = ", bp
54             if len(bp) > 0:
55                 tte= bp[-1:] # only one node at each step, the last one in the list
56                 r = self.e.setStepsToExecute(tte)
57                 self.e.resumeCurrentBreakPoint()
58                 tocont = self.e.isNotFinished()
59             else:
60                 tocont = False
61                 pass
62             print "toContinue = ", tocont
63             pass
64         
65         self.e.resumeCurrentBreakPoint()
66         run1.join()
67         self.assertEqual(106, self.p.getChildByName('node48').getEffectiveState())
68         self.assertEqual(999, self.p.getChildByName('node13').getEffectiveState())
69         self.assertEqual(888, self.p.getChildByName('node14').getEffectiveState())
70         self.assertEqual(777, self.p.getChildByName('c1').getEffectiveState())
71         self.assertEqual(777, self.p.getChildByName('c1.c1.n2').getEffectiveState())
72         self.assertEqual(106, self.p.getChildByName('c0.c1.n1').getEffectiveState())
73         self.assertEqual(999, self.p.getChildByName('c0.n2').getEffectiveState())
74         self.assertEqual(888, self.p.getChildByName('node62').getEffectiveState())
75         print "================= End of STEPBYSTEP ====================="
76         pass
77
78     def test2_StopToBreakpoint(self):
79         # --- start execution, set a breakpoint before node48, then continue
80         time.sleep(1)
81         print "================= Start of BREAKPOINT ==================="
82         brp=['node48']
83         self.e.setListOfBreakPoints(brp)
84         self.e.setExecMode(2) # YACS::STOPBEFORENODES
85         self.run2 = threading.Thread(None, self.e.RunPy, "breakpoint", (self.p,0))
86         self.run2.start()
87         time.sleep(0.1)
88         self.e.waitPause()
89         #self.e.displayDot(p)
90         print "================= reach BREAKPOINT ======================"
91         # --- resume from breakpoint
92         print "=========== BREAKPOINT, start RESUME ===================="
93         time.sleep(1)
94         self.e.setExecMode(0) # YACS::CONTINUE
95         self.e.resumeCurrentBreakPoint()
96         time.sleep(0.1)
97         self.e.waitPause()
98         #self.e.displayDot(p)
99         self.run2.join()
100         self.assertEqual(106, self.p.getChildByName('node48').getEffectiveState())
101         self.assertEqual(999, self.p.getChildByName('node13').getEffectiveState())
102         self.assertEqual(888, self.p.getChildByName('node14').getEffectiveState())
103         self.assertEqual(777, self.p.getChildByName('c1').getEffectiveState())
104         self.assertEqual(777, self.p.getChildByName('c1.c1.n2').getEffectiveState())
105         self.assertEqual(106, self.p.getChildByName('c0.c1.n1').getEffectiveState())
106         self.assertEqual(999, self.p.getChildByName('c0.n2').getEffectiveState())
107         self.assertEqual(888, self.p.getChildByName('node62').getEffectiveState())
108         print "================= End of RESUME ========================="
109         pass
110     
111     def test3_RunWithoutBreakpoints(self):
112         # --- start execution, run without breakpoints
113         time.sleep(1)
114         
115         print "================= Start of CONTINUE ====================="
116         self.e.setExecMode(0) # YACS::CONTINUE
117         run3 = threading.Thread(None, self.e.RunPy, "continue", (self.p,0))
118         run3.start()
119         time.sleep(0.1)
120         self.e.waitPause()
121         #self.e.displayDot(p)
122         run3.join()
123         self.assertEqual(106, self.p.getChildByName('node48').getEffectiveState())
124         self.assertEqual(999, self.p.getChildByName('node13').getEffectiveState())
125         self.assertEqual(888, self.p.getChildByName('node14').getEffectiveState())
126         self.assertEqual(777, self.p.getChildByName('c1').getEffectiveState())
127         self.assertEqual(777, self.p.getChildByName('c1.c1.n2').getEffectiveState())
128         self.assertEqual(106, self.p.getChildByName('c0.c1.n1').getEffectiveState())
129         self.assertEqual(999, self.p.getChildByName('c0.n2').getEffectiveState())
130         self.assertEqual(888, self.p.getChildByName('node62').getEffectiveState())
131         print "================= End of CONTINUE ======================="
132         pass
133
134     def test4_StopOnError(self):
135         # --- stop execution on first error and save state
136         time.sleep(1)
137
138         print "================= Start of STOPONERROR =================="
139         self.e.setStopOnError()
140         run4 = threading.Thread(None, self.e.RunPy, "continue", (self.p,0))
141         run4.start()
142         time.sleep(0.1)
143         self.e.waitPause()
144         self.e.saveState("dumpErrorASchema.xml")
145         self.e.stopExecution()
146         run4.join()
147         #self.e.displayDot(self.p)
148         s13 = self.p.getChildByName('node13').getEffectiveState()
149         s43 = self.p.getChildByName('node43').getEffectiveState()
150         self.assert_((s13==999) or (s43==999))
151         print "================= End of STOPONERROR ====================="
152         pass
153
154     def test5_PartialExec(self):
155         # --- stop execution after breakpoint
156         time.sleep(1)
157
158         print "================= Start of PARTIALEXEC ==================="
159         brp=['node35']
160         self.e.setListOfBreakPoints(brp)
161         self.e.setExecMode(2) # YACS::STOPBEFORENODES
162         #self.e.displayDot(self.p)
163         run5 = threading.Thread(None, self.e.RunPy, "breakpoint", (self.p,0))
164         run5.start()
165         time.sleep(0.1)
166         self.e.waitPause()
167         #self.e.displayDot(self.p)
168         self.e.saveState('dumpPartialASchema.xml')
169         #self.e.displayDot(self.p)
170         self.e.stopExecution()
171         run5.join()
172         #self.e.displayDot(self.p)
173         self.assertEqual(106, self.p.getChildByName('node34').getEffectiveState())
174         self.assertEqual(101, self.p.getChildByName('node35').getEffectiveState())
175         print "================= reach BREAKPOINT PARTIAL EXEC =========="
176         pass
177                           
178     pass
179
180 import os
181 U = os.getenv('USER')
182 f=open("/tmp/" + U + "/UnitTestsResult", 'a')
183 f.write("  --- TEST src/yacsloader: testExec.py\n")
184 suite = unittest.makeSuite(TestExec)
185 unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
186 f.close()