From: Anthony Geay Date: Tue, 17 Jan 2023 07:38:17 +0000 (+0100) Subject: Adding non regression test X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=refs%2Ftlpr%2F1%2Fhead;p=tools%2Fydefx.git Adding non regression test --- diff --git a/src/pydefx/mpmcn.py b/src/pydefx/mpmcn.py index f66d317..49aa44b 100644 --- a/src/pydefx/mpmcn.py +++ b/src/pydefx/mpmcn.py @@ -32,6 +32,10 @@ class Pool: self.myScript = pydefx.PyScript() self.mySample = None self.myParams = params + self.removeTmpDir = False + + def getResultDirectory(self): + return self.myParams.salome_parameters.result_directory def map(self, func, iterable): if len(iterable) == 0: @@ -81,10 +85,14 @@ def _exec({}): ret = [elt for elt in zip(*[self.myStudy.sample._output[n] for n in self.myStudy.sample.getOutputNames()])] if len(self.myStudy.sample.getOutputNames()) == 1: ret = [elt[0] for elt in ret] + self.removeTmpDir = True return ret else: excMsg = "\n".join(["Error for sample # {} : \'{}\' ".format(i,elt) for i,elt in enumerate(messageFromSlaves) if elt != ""]) - raise RuntimeError( excMsg ) + excMsg += "\nDirectory containing information for debug : {}".format(self.getResultDirectory()) + exc = RuntimeError( excMsg ) + exc.tmp_dir = self.getResultDirectory() + raise exc else: raise RuntimeError("Error during job submission or during the driver execution (that should never happend)") @@ -93,6 +101,15 @@ def _exec({}): return self def __exit__(self, exc_type, exc_val, exc_tb): + import os + from glob import glob + import shutil + if self.removeTmpDir: + for fn in glob(os.path.join(self.getResultDirectory(),"*")): + if os.path.isdir( fn ): + shutil.rmtree( fn) + else: + os.unlink( fn ) pass pass @@ -119,7 +136,6 @@ def getNumberOfCoresForLocalhost(): def init(resourceName, resultDirectory = "/tmp"): """ - Instanciate a pydefx.Parameters intance that can be overriden right after. Here some example of typical override of the returned object of this method : diff --git a/src/pyexample/CMakeLists.txt b/src/pyexample/CMakeLists.txt index 2160cd7..a144487 100644 --- a/src/pyexample/CMakeLists.txt +++ b/src/pyexample/CMakeLists.txt @@ -26,6 +26,7 @@ IF(SALOME_BUILD_TESTS) test_insitu.py test_prescript.py test_default.py + test_mpmcn.py ) INSTALL(FILES ${TESTFILES} DESTINATION ${LOCAL_TEST_DIR}) INSTALL(PROGRAMS runUnitTest.sh diff --git a/src/pyexample/runUnitTest.sh b/src/pyexample/runUnitTest.sh index d516d31..7531d2d 100755 --- a/src/pyexample/runUnitTest.sh +++ b/src/pyexample/runUnitTest.sh @@ -18,6 +18,9 @@ # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # -python3 -m unittest test_insitu.py test_prescript.py test_default.py -ret=$? +python3 -m unittest test_insitu.py test_prescript.py test_default.py +ret0=$? +python3 test_mpmcn.py +ret1=$? +let ret=$ret0+$ret1 exit $ret diff --git a/src/pyexample/test_mpmcn.py b/src/pyexample/test_mpmcn.py new file mode 100644 index 0000000..098bd38 --- /dev/null +++ b/src/pyexample/test_mpmcn.py @@ -0,0 +1,40 @@ + +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# + +# multiple processes on multiple compute nodes + +def f(x): + return x*x*x + +def f2(x): + if x==3: + raise RuntimeError("lllll") + return x*x*x + +import mpmcn + +def gg(): + # case 0 : normal behavior with no raise + params = mpmcn.init("localhost") + with mpmcn.Pool(params) as p: + res = p.map(f,list(range(10))) + # getResultDirectory : for advanced users + p.getResultDirectory() + if res != [0.0, 1.0, 8.0, 27.0, 64.0, 125.0, 216.0, 343.0, 512.0, 729.0]: + raise RuntimeError("Test Failed !") + # case 1 : behavior with raise + params = mpmcn.init("localhost") + with mpmcn.Pool(params) as p2: + try: + res = p2.map(f2,list(range(10))) + raise RuntimeError("Exception not thrown -> Error !") + except RuntimeError as e: + strExpected = "Error for sample # 3 : \'lllll\'" + # tmp_dir attr of e returns the ResultDirectory to dive into + if str(e)[:len(strExpected)] != strExpected or not hasattr(e,"tmp_dir"): + raise RuntimeError("Test Failed 2 !") + +if __name__ == "__main__": + gg()