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