Salome HOME
b80dc94195c810e0609dec37f20b253a05cd368b
[modules/yacs.git] / src / yacsloader_swig / Test / testPynodeWithCache.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2006-2021  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, 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 sys
22 import pilot
23 import SALOMERuntime
24 import loader
25 import unittest
26 import tempfile
27 import os
28
29 dir_test = tempfile.mkdtemp(suffix=".yacstest")
30
31 class TestEdit(unittest.TestCase):
32
33     def setUp(self):
34         SALOMERuntime.RuntimeSALOME_setRuntime()
35         self.r = pilot.getRuntime()
36         self.l = loader.YACSLoader()
37         self.e = pilot.ExecutorSwig()
38         pass
39
40     def test1(self):
41       """ Test the conservation of the python context between two nodes sharing
42           the same container.
43           Schema: n1 -> n2
44       """
45       runtime=self.r
46       executor=self.e
47       yacsloader=self.l
48       proc=runtime.createProc("MySchema")
49       ti=proc.createType("int","int")
50       cont=proc.createContainer("MyContainer","Salome")
51       # type "multi" : the workload manager chooses the resource
52       # type "mono" : the resource is chosen by kernel, using the old rules.
53       cont.setProperty("type","multi")
54       # number of cores used by the container
55       cont.setProperty("nb_parallel_procs", "1")
56       n1=runtime.createScriptNode("","n1")
57       n2=runtime.createScriptNode("","n2")
58       n1.setExecutionMode("remote")
59       n2.setExecutionMode("remote")
60       n1.setContainer(cont)
61       n2.setContainer(cont)
62       n1.setScript("v=42")
63       res_port=n2.edAddOutputPort("v", ti)
64       proc.edAddChild(n1)
65       proc.edAddChild(n2)
66       proc.edAddCFLink(n1,n2)
67       # set the default execution mode using the workload manager
68       # if no property is set, the old executor is used
69       proc.setProperty("executor", "workloadmanager")
70       # reuse the same python context for every execution
71       cont.usePythonCache(True)
72       # save & reload
73       schema_file = os.path.join(dir_test,"pynode_with_cache1.xml")
74       proc.saveSchema(schema_file)
75       reloaded_proc = yacsloader.load(schema_file)
76       # default run method of the executor which uses the property "executor"
77       # in order to choose the actual run method
78       executor.RunW(reloaded_proc,0)
79       # you can also directly call the executor you wish, ignoring the property
80       #executor.RunB(proc,0) # always use the "old" executor
81       #executor.runWlm(proc,0) # always use the workload manager based executor
82       reloaded_res_port = reloaded_proc.getChildByName("n2").getOutputPort("v")
83       self.assertEqual(reloaded_res_port.getPyObj(), 42)
84
85     def test2(self):
86       """ Same as test1, but using the old executor instead of workload manager.
87       """
88       runtime=self.r
89       executor=self.e
90       yacsloader=self.l
91       proc=runtime.createProc("MySchema")
92       ti=proc.createType("int","int")
93       cont=proc.createContainer("MyContainer","Salome")
94       # With the old executor the type multi imposes the creation of a new
95       # container for every node. We need the type "mono" in order to have
96       # the same container used for both yacs nodes.
97       cont.setProperty("type","mono")
98       n1=runtime.createScriptNode("","n1")
99       n2=runtime.createScriptNode("","n2")
100       n1.setExecutionMode("remote")
101       n2.setExecutionMode("remote")
102       n1.setContainer(cont)
103       n2.setContainer(cont)
104       n1.setScript("v=42")
105       res_port=n2.edAddOutputPort("v", ti)
106       proc.edAddChild(n1)
107       proc.edAddChild(n2)
108       proc.edAddCFLink(n1,n2)
109       # reuse the same python context for every execution
110       cont.usePythonCache(True)
111       # save & reload
112       schema_file = os.path.join(dir_test,"pynode_with_cache2.xml")
113       proc.saveSchema(schema_file)
114       reloaded_proc = yacsloader.load(schema_file)
115       # default run method of the executor which uses the property "executor"
116       # in order to choose the actual run method
117       executor.RunW(reloaded_proc,0)
118       # you can also directly call the executor you wish, ignoring the property
119       #executor.RunB(proc,0) # always use the "old" executor
120       reloaded_res_port = reloaded_proc.getChildByName("n2").getOutputPort("v")
121       self.assertEqual(reloaded_res_port.getPyObj(), 42)
122
123 if __name__ == '__main__':
124   file_test = os.path.join(dir_test,"UnitTestsResult")
125   with open(file_test, 'a') as f:
126       f.write("  --- TEST src/yacsloader: testPynodeWithCache.py\n")
127       suite = unittest.makeSuite(TestEdit)
128       result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
129   sys.exit(not result.wasSuccessful())