Salome HOME
5fbe79718a7f39ba08d58967063e7ad78bafc2b5
[modules/yacs.git] / src / yacsloader_swig / Test / testYacsProxy.py
1 #!/usr/bin/env python3
2 # Copyright (C) 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 unittest
22 import tempfile
23 import os
24
25 import pilot
26 import SALOMERuntime
27 import loader
28 import salome
29
30 class TestYacsProxy(unittest.TestCase):
31   def test0(self):
32     """
33     [EDF27816] : test to check
34     """
35     salome.salome_init()
36     with tempfile.TemporaryDirectory() as tmpdirname:
37       print(tmpdirname)
38       salome.cm.SetOverrideEnvForContainersSimple([("SALOME_BIG_OBJ_ON_DISK_THRES","1"),("SALOME_FILE_BIG_OBJ_DIR",str(tmpdirname))])
39       ####
40       SALOMERuntime.RuntimeSALOME.setRuntime()
41       r=SALOMERuntime.getSALOMERuntime()
42       p=r.createProc("StressTest")
43       ti=p.createType("int","int")
44       td=p.createType("double","double")
45       tdd=p.createSequenceTc("seqdouble","seqdouble",td)
46       tddd=p.createSequenceTc("seqseqdouble","seqseqdouble",tdd)
47       tdddd=p.createSequenceTc("seqseqseqdouble","seqseqseqdouble",tddd)
48       pyobj=p.createInterfaceTc("python:obj:1.0","pyobj",[])
49       seqpyobj=p.createSequenceTc("list[pyobj]","list[pyobj]",pyobj)
50       seqseqpyobj=p.createSequenceTc("list[list[pyobj]]","list[list[pyobj]]",seqpyobj)
51       seqseqseqpyobj=p.createSequenceTc("list[list[list[pyobj]]]","list[list[list[pyobj]]]",seqseqpyobj)
52       cont=p.createContainer("gg","Salome")
53       cont.setProperty("name","localhost")
54       cont.setProperty("hostname","localhost")
55
56       ######## Level 0
57       startNode = r.createScriptNode("Salome","start")
58       startNode.setExecutionMode("local")
59       startNode.setContainer(None)
60       startNode.setSqueezeStatus(True)
61       startNode.setScript("""o2 = [[[ {k:2*k for k in range(23,55)}  ]]]""")
62       po2 = startNode.edAddOutputPort("o2",seqseqseqpyobj)
63       p.edAddChild(startNode)
64       #
65       fe = r.createForEachLoopDyn("fe",seqseqpyobj)
66       p.edAddChild(fe)
67       p.edAddCFLink(startNode,fe)
68       p.edAddLink(po2,fe.edGetSeqOfSamplesPort())
69       #
70       gather2Node = r.createScriptNode("Salome","gather2")
71       p.edAddChild(gather2Node)
72       gather2Node.setExecutionMode("local")
73       gather2Node.setContainer(None)
74       gather2Node.setSqueezeStatus(True)
75       pi5 = gather2Node.edAddInputPort("i5",seqpyobj)
76       po5 = gather2Node.edAddOutputPort("o5",seqpyobj)
77       gather2Node.setScript("""
78 from glob import glob
79 import os
80 import salome
81 salome.salome_init()
82 effEnv = {elt.key:elt.val for elt in salome.cm.GetOverrideEnvForContainers()}
83 if len( glob( os.path.join( effEnv["SALOME_FILE_BIG_OBJ_DIR"], "*.pckl" ) ) ) != 1:
84   raise RuntimeError("Fail !")
85 print("gather2")
86 o5 = i5""")
87       p.edAddCFLink(fe,gather2Node)
88       ####### Level 1
89       n1b = r.createBloc("n1b")
90       fe.edSetNode(n1b)
91       #
92       gather1Node = r.createScriptNode("Salome","gather1")
93       gather1Node.setExecutionMode("local")
94       gather1Node.setContainer(None)
95       gather1Node.setSqueezeStatus(True)
96       pi6 = gather1Node.edAddInputPort("i6",seqpyobj)
97       po6 = gather1Node.edAddOutputPort("o6",seqpyobj)
98       gather1Node.setScript("""print("gather1")
99 print(i6)
100 o6 = i6""")
101       n1b.edAddChild(gather1Node)
102       fe1 = r.createForEachLoopDyn("fe1",seqpyobj)
103       n1b.edAddChild(fe1)
104       n1b.edAddCFLink(fe1,gather1Node)
105       ####### Level2
106       n2b = r.createBloc("n2b")
107       fe1.edSetNode(n2b)
108       fe2 = r.createForEachLoopDyn("fe2",pyobj)
109       n2b.edAddChild(fe2)
110       #
111       gather0Node = r.createScriptNode("Salome","gather0")
112       gather0Node.setExecutionMode("local")
113       gather0Node.setContainer(None)
114       gather0Node.setSqueezeStatus(True)
115       pi7 = gather0Node.edAddInputPort("i7",seqpyobj)
116       po7 = gather0Node.edAddOutputPort("o7",seqpyobj)
117       gather0Node.setScript("""
118 print("gather0")
119 print(i7)
120 o7 = i7""")
121       n2b.edAddChild(gather0Node)
122       n2b.edAddCFLink(fe2,gather0Node)
123       heatNode = r.createScriptNode("Salome","HeatMarcelNode")
124       heatNode.setExecutionMode("remote")
125       heatNode.setContainer(cont)
126       heatNode.setSqueezeStatus(True)
127       heatNode.setScript("""o3 = list(range(100,200))""")
128       pi3 = heatNode.edAddInputPort("i3",pyobj)
129       po3 = heatNode.edAddOutputPort("o3",pyobj)
130       fe2.edSetNode(heatNode)
131       fe2.edAddLink(fe2.edGetSamplePort(),pi3)
132       # connection part
133       p.edAddLink( fe1.edGetSamplePort(), fe2.edGetSeqOfSamplesPort() )
134       p.edAddLink( fe.edGetSamplePort(), fe1.edGetSeqOfSamplesPort() )
135       p.edAddLink( po6, pi5 )
136       p.edAddLink( po7, pi6 )
137       p.edAddLink( po3, pi7 )
138
139       pp = p
140       fname = "stressTest3.xml"
141       #pp.saveSchema(fname)
142       ####
143       l=loader.YACSLoader()
144       #p=l.load(fname)
145       import datetime
146       st = datetime.datetime.now()
147       ex=pilot.ExecutorSwig()
148       ex.setMaxNbOfThreads(1000)
149       ex.RunW(pp,0)
150       salome.cm.ShutdownContainers()
151       print("End of computation {}".format( str(datetime.datetime.now()-st) ) )
152       self.assertTrue( pp.getState() == pilot.DONE )
153       from glob import glob
154       self.assertEqual(len( glob(os.path.join(str(tmpdirname),"*.pckl") ) ), 0 )
155   
156 if __name__ == '__main__':
157   with tempfile.TemporaryDirectory() as dir_test:
158     file_test = os.path.join(dir_test,"UnitTestsResult")
159     with open(file_test, 'a') as f:
160         f.write("  --- TEST src/yacsloader: testYacsProxy.py\n")
161         suite = unittest.makeSuite(TestYacsProxy)
162         result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
163         if not result.wasSuccessful():
164            raise RuntimeError("Test failed !")
165