2 # Copyright (C) 2006-2023 CEA, EDF
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
31 dir_test = tempfile.mkdtemp(suffix=".yacstest")
33 class TestDeco(unittest.TestCase):
36 SALOMERuntime.RuntimeSALOME_setRuntime()
37 # We need a catalog which contains only one resource named "localhost"
38 # with 16 cores. The modifications made here are not saved to the
42 resourceManager = salome.lcc.getResourcesManager()
43 resource_definition = resourceManager.GetResourceDefinition("localhost")
44 resource_definition.nb_node = NB_NODE
45 resourceManager.AddResource(resource_definition, False, "")
46 resource_required = salome.ResourceParameters()
47 resource_required.can_run_containers = True
48 res_list = resourceManager.GetFittingResources(resource_required)
51 resourceManager.RemoveResource(r, False, "")
52 resource_definition = resourceManager.GetResourceDefinition("localhost")
53 self.assertEqual(resource_definition.nb_node, NB_NODE)
56 cm = salome.lcc.getContainerManager()
57 cm.ShutdownContainers()
62 jdd -> foreach -> post
68 expected_1, expected_2 = testforeach.main()
69 yacs_schema_file = os.path.join(dir_test, "schema_t1.xml")
70 yacs_build_command = "yacsbuild.py"
71 test_script = "testforeach.py"
72 main_function_name = "main"
73 subprocess.run([yacs_build_command,
74 test_script, main_function_name, yacs_schema_file])
75 l = loader.YACSLoader()
76 ex = pilot.ExecutorSwig()
77 proc = l.load(yacs_schema_file)
79 obtained_1 = proc.getChildByName("post_0").getOutputPort("s").getPyObj()
80 obtained_2 = proc.getChildByName("f1_1").getOutputPort("r").getPyObj()
81 self.assertEqual(expected_1, obtained_1)
82 self.assertEqual(expected_2, obtained_2)
86 Foreach initialized by value.
89 expected_1, expected_2 = testforeach.mainblock()
90 yacs_schema_file = os.path.join(dir_test, "schema_t2.xml")
91 yacs_build_command = "yacsbuild.py"
92 test_script = "testforeach.py"
93 main_function_name = "mainblock"
94 subprocess.run([yacs_build_command,
95 test_script, main_function_name, yacs_schema_file])
96 l = loader.YACSLoader()
97 ex = pilot.ExecutorSwig()
98 proc = l.load(yacs_schema_file)
100 obtained_1 = proc.getChildByName("output_fr_0").getOutputPort("s_0").getPyObj()
101 obtained_2 = proc.getChildByName("output_fr_0").getOutputPort("p_1").getPyObj()
102 self.assertEqual(expected_1, obtained_1)
103 self.assertEqual(expected_2, obtained_2)
110 expected = testforeach.maindoublefr()
111 yacs_schema_file = os.path.join(dir_test, "schema_t3.xml")
112 yacs_build_command = "yacsbuild.py"
113 test_script = "testforeach.py"
114 main_function_name = "maindoublefr"
115 subprocess.run([yacs_build_command,
116 test_script, main_function_name, yacs_schema_file])
117 l = loader.YACSLoader()
118 ex = pilot.ExecutorSwig()
119 proc = l.load(yacs_schema_file)
121 obtained = proc.getChildByName("output_doublefr_0").getOutputPort("r_0_0").getPyObj()
122 self.assertEqual(expected, obtained)
126 Using specific containers.
127 This test needs at least 4 cores declared in the catalog of resources.
130 cm = yacsdecorator.ContainerManager()
131 cm.addContainer("c1", 1, False)
132 cm.addContainer("c2", 4, True)
133 cm.addContainer(yacsdecorator.ContainerManager.defaultContainerName, 1, False)
134 cont_file = os.path.join(dir_test, "containers_t4.json")
135 cm.saveFile(cont_file)
136 script = """import yacsdecorator
137 @yacsdecorator.leaf("c1")
142 @yacsdecorator.leaf("c2")
158 script_file = os.path.join(dir_test, "script_t4.py")
159 with open(script_file, "w") as f:
161 yacs_build_command = "yacsbuild.py"
162 main_function_name = "main"
163 yacs_schema_file = os.path.join(dir_test, "schema_t4.xml")
164 subprocess.run([yacs_build_command,
165 script_file, main_function_name, yacs_schema_file,
167 l = loader.YACSLoader()
168 ex = pilot.ExecutorSwig()
169 proc = l.load(yacs_schema_file)
171 self.assertEqual(proc.getState(),pilot.DONE)
172 c1 = proc.getChildByName("f_c1_0").getContainer()
173 self.assertFalse(c1.isUsingPythonCache())
174 self.assertEqual(c1.getProperty("nb_parallel_procs"), "1")
175 c2 = proc.getChildByName("f_c2_0").getContainer()
176 self.assertTrue(c2.isUsingPythonCache())
177 self.assertEqual(c2.getProperty("nb_parallel_procs"), "4")
178 c3 = proc.getChildByName("f_def_0").getContainer()
179 self.assertFalse(c3.isUsingPythonCache())
180 self.assertEqual(c3.getProperty("nb_parallel_procs"), "1")
182 if __name__ == '__main__':
183 file_test = os.path.join(dir_test,"UnitTestsResult")
184 with open(file_test, 'a') as f:
185 f.write(" --- TEST src/py2yacs: testDeco.py\n")
186 suite = unittest.makeSuite(TestDeco)
187 result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
188 sys.exit(not result.wasSuccessful())