From ff6426f394ecc496bfceee444f0fad78e955861e Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 15 Jun 2023 17:25:17 +0200 Subject: [PATCH] [EDF27816] : Get rid of SALOME session for 3 last tests (YacsLoader, YacsLoader_Swig, Runtime) --- src/CTestTestfileInstall.cmake | 2 +- src/runtime/CORBAComponent.cxx | 4 +- src/runtime/PythonNode.cxx | 26 ++-- src/runtime/PythonNode.hxx | 1 + src/runtime/RuntimeSALOME.cxx | 65 ++++++++++ src/runtime/RuntimeSALOME.hxx | 2 + src/runtime/Test/TestRuntime.cxx | 25 +++- src/runtime/Test/echoSrv.cxx | 116 ++++-------------- src/runtime/Test/runtimeTest.cxx | 2 +- src/runtime/Test/runtimeTest.sh | 33 +---- src/runtime/TypeConversions.cxx | 4 +- .../Test/CTestTestfileInstall.cmake | 2 +- src/yacsloader/Test/TestYacsLoader.cxx | 36 +++++- src/yacsloader/Test/YacsLoaderTest.cxx | 5 + src/yacsloader/Test/echoSrv.cxx | 116 ++++-------------- src/yacsloader/Test/runYacsLoaderTest.sh | 5 +- src/yacsloader_swig/Test/CMakeLists.txt | 3 +- .../Test/CTestTestfileInstall.cmake | 5 +- src/yacsloader_swig/Test/runUnitTest.sh | 39 ------ src/yacsloader_swig/Test/testExec.py | 23 ++-- src/yacsloader_swig/Test/testLoader.py | 22 +++- src/yacsloader_swig/Test/testSave.py | 17 ++- .../Test/testYacsLoaderSwig.py | 56 +++++++++ 23 files changed, 319 insertions(+), 290 deletions(-) delete mode 100755 src/yacsloader_swig/Test/runUnitTest.sh create mode 100644 src/yacsloader_swig/Test/testYacsLoaderSwig.py diff --git a/src/CTestTestfileInstall.cmake b/src/CTestTestfileInstall.cmake index e377f6957..7e351c0a1 100644 --- a/src/CTestTestfileInstall.cmake +++ b/src/CTestTestfileInstall.cmake @@ -18,7 +18,7 @@ # #SET(SALOME_TEST_DRIVER "$ENV{ABSOLUTE_APPLI_PATH}/bin/salome/appliskel/salome_test_driver.py") -SET(SALOME_TEST_DRIVER "$ENV{KERNEL_ROOT_DIR}/bin/salome/appliskel/salome_test_driver.py") +SET(PYTHON_TEST_DRIVER "$ENV{KERNEL_ROOT_DIR}/bin/salome/appliskel/python_test_driver.py") SET(COMPONENT_NAME YACS) SET(TIMEOUT 500) diff --git a/src/runtime/CORBAComponent.cxx b/src/runtime/CORBAComponent.cxx index 096773574..e3e21455f 100644 --- a/src/runtime/CORBAComponent.cxx +++ b/src/runtime/CORBAComponent.cxx @@ -101,12 +101,10 @@ bool CORBAComponent::isLoaded(Task *askingNode) const void CORBAComponent::load(Task *askingNode) { DEBTRACE( "CORBAComponent::load" ); - CORBA::ORB_ptr orb; try { DEBTRACE( "+++++++++++++++++" << getCompoName() << " +++++++++++++++++" ); - orb = getSALOMERuntime()->getOrb(); - _objComponent= orb->string_to_object(getCompoName().c_str()); + _objComponent= getSALOMERuntime()->getFromNS(getCompoName().c_str()); #ifdef REFCNT std::cerr << "CORBAComponent::load:refCount: " <<_objComponent->_PR_getobj()->pd_refCount << std::endl; #endif diff --git a/src/runtime/PythonNode.cxx b/src/runtime/PythonNode.cxx index 3334d97e2..d0f40b944 100644 --- a/src/runtime/PythonNode.cxx +++ b/src/runtime/PythonNode.cxx @@ -678,33 +678,34 @@ void PythonNode::executeRemote() } DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" ); } -void PythonNode::executeLocalInternal(const std::string& codeStr) + +void PythonNode::ExecuteLocalInternal(const std::string& codeStr, PyObject *context, std::string& errorDetails) { DEBTRACE( code ); - DEBTRACE( "_context refcnt: " << _context->ob_refcnt ); + DEBTRACE( "context refcnt: " << context->ob_refcnt ); std::ostringstream stream; stream << "/tmp/PythonNode_"; stream << getpid(); AutoPyRef code=Py_CompileString(codeStr.c_str(), stream.str().c_str(), Py_file_input); if(code == NULL) { - _errorDetails=""; - AutoPyRef new_stderr = newPyStdOut(_errorDetails); + errorDetails=""; + AutoPyRef new_stderr = newPyStdOut(errorDetails); PySys_SetObject((char*)"stderr", new_stderr); PyErr_Print(); PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__")); - throw Exception("Error during execution"); + throw YACS::Exception("Error during execution"); } { - AutoPyRef res = PyEval_EvalCode( code, _context, _context); + AutoPyRef res = PyEval_EvalCode( code, context, context); } - DEBTRACE( "_context refcnt: " << _context->ob_refcnt ); + DEBTRACE( "context refcnt: " << context->ob_refcnt ); fflush(stdout); fflush(stderr); if(PyErr_Occurred ()) { - _errorDetails=""; - AutoPyRef new_stderr = newPyStdOut(_errorDetails); + errorDetails=""; + AutoPyRef new_stderr = newPyStdOut(errorDetails); PySys_SetObject((char*)"stderr", new_stderr); ofstream errorfile(stream.str().c_str()); if (errorfile.is_open()) @@ -714,10 +715,15 @@ void PythonNode::executeLocalInternal(const std::string& codeStr) } PyErr_Print(); PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__")); - throw Exception("Error during execution"); + throw YACS::Exception("Error during execution"); } } +void PythonNode::executeLocalInternal(const std::string& codeStr) +{ + ExecuteLocalInternal(codeStr,_context,_errorDetails); +} + void PythonNode::executeLocal() { DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" ); diff --git a/src/runtime/PythonNode.hxx b/src/runtime/PythonNode.hxx index d83997cde..49b6a2e7c 100644 --- a/src/runtime/PythonNode.hxx +++ b/src/runtime/PythonNode.hxx @@ -108,6 +108,7 @@ namespace YACS void setSqueezeStatus(bool sqStatus) { _autoSqueeze=sqStatus; } bool getSqueezeStatus() const { return _autoSqueeze; } void squeezeMemorySafe(); + static void ExecuteLocalInternal(const std::string& codeStr, PyObject *context, std::string& errorDetails); private: void executeLocalInternal(const std::string& codeStr); protected: diff --git a/src/runtime/RuntimeSALOME.cxx b/src/runtime/RuntimeSALOME.cxx index 15ad04feb..55401af07 100644 --- a/src/runtime/RuntimeSALOME.cxx +++ b/src/runtime/RuntimeSALOME.cxx @@ -116,6 +116,7 @@ #include "SALOME_ResourcesManager.hxx" #include "SALOME_ContainerManager.hxx" #include "SALOMEconfig.h" +#include "SALOME_Embedded_NamingService.hxx" #include CORBA_CLIENT_HEADER(SALOME_ContainerManager) #endif @@ -124,7 +125,9 @@ #include #include #include +#include #include +#include //#define _DEVDEBUG_ #include "YacsTrace.hxx" @@ -464,6 +467,38 @@ void RuntimeSALOME::fini() } } +PyObject *RuntimeSALOME::launchSubProcess(const std::vector& cmds) +{ + std::ostringstream oss; oss << "from subprocess import Popen" << std::endl; + oss << "p = Popen(["; + for(auto i = 0 ; i < cmds.size() ; ++i) + { + oss << " " << "\"" << cmds[i] << "\""; + if(i < cmds.size()-1) + oss << ", "; + else + oss << " "; + } + oss << "])"; + AutoGIL agil; + AutoPyRef context = PyDict_New(); + PyDict_SetItemString( context, "__builtins__", getBuiltins() ); + std::string errorDetails; + try + { + PythonNode::ExecuteLocalInternal(oss.str().c_str(),context,errorDetails); + } + catch(const YACS::Exception& e) + { + std::cerr << e.what() << std::endl << errorDetails << std::endl; + throw e; + } + PyObject *ret = PyDict_GetItemString(context,"p"); + Py_XINCREF(ret); + Py_XINCREF(ret); + return ret; +} + std::vector< std::pair > RuntimeSALOME::getCatalogOfComputeNodes() const { CORBA::ORB_ptr orb(getOrb()); @@ -1875,6 +1910,36 @@ CORBA::ORB_ptr RuntimeSALOME::getOrb() const return _orb; } +/*! + * Retrieve from custom NS the entry. Custom NS is supposed to be hosted in current process. + * This method try to emulate CORBA ns convention : "corbaname:rir:#test.my_context/Echo.Object" is converted into "Echo" + * + * See Engines::EmbeddedNamingService + */ +CORBA::Object_var RuntimeSALOME::getFromNS(const char *entry) const +{ + CORBA::Object_var ret; + std::string entryCpp(entry); + if(entryCpp.substr(0,4) == "IOR:") + { + ret = _orb->string_to_object( entry ); + } + else + { + auto pos = entryCpp.find_last_of('/'); + std::string entry1( entryCpp.substr(pos+1,std::string::npos) ); + pos = entry1.find_last_of('.'); + std::string entry2( entry1.substr(0,pos) ); + Engines::EmbeddedNamingService_var ns = GetEmbeddedNamingService(); + std::unique_ptr iorRet( ns->Resolve( entry2.c_str() ) ); + auto len = iorRet->length(); + std::unique_ptr iorTrans(new char[len+1]); iorTrans[len] = '\0'; + for(auto i = 0 ; i < len ; ++i) iorTrans[i] = (*iorRet)[i]; + ret = _orb->string_to_object(iorTrans.get()); + } + return ret; +} + PyObject * RuntimeSALOME::getPyOrb() const { return _pyorb; diff --git a/src/runtime/RuntimeSALOME.hxx b/src/runtime/RuntimeSALOME.hxx index 51370ac47..1e94f0af3 100644 --- a/src/runtime/RuntimeSALOME.hxx +++ b/src/runtime/RuntimeSALOME.hxx @@ -84,6 +84,7 @@ namespace YACS virtual void init(long flags, int argc, char* argv[]); virtual void fini(); + PyObject *launchSubProcess(const std::vector& cmds); virtual std::vector< std::pair > getCatalogOfComputeNodes() const; virtual InputPort* createInputPort(const std::string& name, const std::string& impl, @@ -236,6 +237,7 @@ namespace YACS void loadModulCatalog(); CORBA::ORB_ptr getOrb() const; + CORBA::Object_var getFromNS(const char *entry) const; PyObject * getPyOrb() const; PyObject * getBuiltins() const; DynamicAny::DynAnyFactory_ptr getDynFactory() const; diff --git a/src/runtime/Test/TestRuntime.cxx b/src/runtime/Test/TestRuntime.cxx index ec16d53b3..bf813ba89 100644 --- a/src/runtime/Test/TestRuntime.cxx +++ b/src/runtime/Test/TestRuntime.cxx @@ -20,6 +20,8 @@ #define UNIT_TEST_HEADER " --- TEST src/runtime" #include "runtimeTest.hxx" +#include "PythonCppUtils.hxx" +#include "SALOME_Embedded_NamingService.hxx" using namespace YACS; using namespace std; @@ -29,5 +31,26 @@ using namespace std; CPPUNIT_TEST_SUITE_REGISTRATION( RuntimeTest ); // --- generic Main program from bases/Test +#include "BasicMainTestInternal.hxx" +#include "RuntimeSALOME.hxx" -#include "BasicMainTest.hxx" +int main() +{ + YACS::ENGINE::RuntimeSALOME::setRuntime(); + YACS::ENGINE::RuntimeSALOME *rt = YACS::ENGINE::getSALOMERuntime(); + if( !rt ) + return 1; + Engines::EmbeddedNamingService_var ns = GetEmbeddedNamingService(); + CORBA::ORB_ptr orb = rt->getOrb(); + CORBA::String_var ior = orb->object_to_string( ns ); + AutoPyRef proc = rt->launchSubProcess({"./runtimeTestEchoSrv",std::string(ior)}); + usleep(3000000); + int ret = BasicMainTestInternal(); + // + { + AutoGIL agil; + AutoPyRef terminateStr = PyUnicode_FromString("terminate"); + AutoPyRef dummy = PyObject_CallMethodObjArgs(proc,terminateStr,nullptr); + } + return ret; +} diff --git a/src/runtime/Test/echoSrv.cxx b/src/runtime/Test/echoSrv.cxx index 77c10f6a1..33fca7b2f 100644 --- a/src/runtime/Test/echoSrv.cxx +++ b/src/runtime/Test/echoSrv.cxx @@ -27,7 +27,10 @@ using namespace std; //#define _DEVDEBUG_ #include "YacsTrace.hxx" -static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*); +#include +#include CORBA_CLIENT_HEADER(SALOME_Embedded_NamingService) + +static CORBA::Boolean bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr,const char*); static ostream& operator<<(ostream& os, const CORBA::Exception& e) { @@ -325,6 +328,15 @@ int main(int argc, char** argv) try { orb = CORBA::ORB_init(argc, argv); + std::string theIOR( argv[1] ); + + CORBA::Object_var obj = orb->string_to_object(theIOR.c_str()); + Engines::EmbeddedNamingService_var ns = Engines::EmbeddedNamingService::_narrow( obj ); + if( CORBA::is_nil( ns ) ) + return 1; + //std::cout << getpid() << std::endl; + //usleep(20000000); + { CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj); @@ -339,7 +351,7 @@ int main(int argc, char** argv) v <<= omniPolicy::LOCAL_CALLS_SHORTCUT; pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v); pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION); - PortableServer::POA_ptr shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2); + PortableServer::POA_var shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2); // Create and activate servant Echo_i* myecho = new Echo_i(); @@ -350,7 +362,7 @@ int main(int argc, char** argv) DEBTRACE("'" << (char*)sior << "'"); myechoref = eo::Echo::_narrow(obj); - if( !bindObjectToName(orb, myechoref,"Echo") ) return 1; + if( !bindObjectToName(ns, myechoref,"Echo") ) return 1; // Decrement the reference count of the object implementation, so // that it will be properly cleaned up when the POA has determined @@ -362,21 +374,21 @@ int main(int argc, char** argv) obj=myC->_this(); eo::C_var myCref=eo::C::_narrow(obj); myC->_remove_ref(); - if( !bindObjectToName(orb, myCref,"C") ) return 1; + if( !bindObjectToName(ns, myCref,"C") ) return 1; //create object D and register it in naming service D_i* myD = new D_i(); obj=myD->_this(); eo::D_var myDref=eo::D::_narrow(obj); myD->_remove_ref(); - if( !bindObjectToName(orb, myDref,"D") ) return 1; + if( !bindObjectToName(ns, myDref,"D") ) return 1; //create object Obj and register it in naming service Obj_i* myObj = new Obj_i(); obj=myObj->_this(); eo::Obj_var myObjref=eo::Obj::_narrow(obj); myObj->_remove_ref(); - if( !bindObjectToName(orb, myObjref,"Obj") ) return 1; + if( !bindObjectToName(ns, myObjref,"Obj") ) return 1; } orb->run(); } @@ -403,90 +415,16 @@ int main(int argc, char** argv) ////////////////////////////////////////////////////////////////////// static CORBA::Boolean -bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name) +bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr objref,const char *name) { - CosNaming::NamingContext_var rootContext; - - try { - // Obtain a reference to the root context of the Name service: - CORBA::Object_var obj; - obj = orb->resolve_initial_references("NameService"); - - // Narrow the reference returned. - rootContext = CosNaming::NamingContext::_narrow(obj); - if( CORBA::is_nil(rootContext) ) { - DEBTRACE("Failed to narrow the root naming context."); - return 0; - } - } - catch(CORBA::ORB::InvalidName& ex) { - // This should not happen! - DEBTRACE("Service required is invalid [does not exist]." ); - return 0; - } - - try { - // Bind a context called "test" to the root context: - - CosNaming::Name contextName; - contextName.length(1); - contextName[0].id = (const char*) "test"; // string copied - contextName[0].kind = (const char*) "my_context"; // string copied - // Note on kind: The kind field is used to indicate the type - // of the object. This is to avoid conventions such as that used - // by files (name.type -- e.g. test.ps = postscript etc.) - - CosNaming::NamingContext_var testContext; - try { - // Bind the context to root. - testContext = rootContext->bind_new_context(contextName); - } - catch(CosNaming::NamingContext::AlreadyBound& ex) { - // If the context already exists, this exception will be raised. - // In this case, just resolve the name and assign testContext - // to the object returned: - CORBA::Object_var obj; - obj = rootContext->resolve(contextName); - testContext = CosNaming::NamingContext::_narrow(obj); - if( CORBA::is_nil(testContext) ) { - DEBTRACE("Failed to narrow naming context."); - return 0; - } - } - - // Bind objref with name Echo to the testContext: - CosNaming::Name objectName; - objectName.length(1); - objectName[0].id = name; // string copied - objectName[0].kind = (const char*) "Object"; // string copied - - try { - testContext->bind(objectName, objref); - } - catch(CosNaming::NamingContext::AlreadyBound& ex) { - testContext->rebind(objectName, objref); - } - // Note: Using rebind() will overwrite any Object previously bound - // to /test/Echo with obj. - // Alternatively, bind() can be used, which will raise a - // CosNaming::NamingContext::AlreadyBound exception if the name - // supplied is already bound to an object. - - // Amendment: When using OrbixNames, it is necessary to first try bind - // and then rebind, as rebind on it's own will throw a NotFoundexception if - // the Name has not already been bound. [This is incorrect behaviour - - // it should just bind]. - } - catch(CORBA::COMM_FAILURE& ex) { - DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the " - << "naming service."); - return 0; - } - catch(CORBA::SystemException&) { - DEBTRACE("Caught a CORBA::SystemException while using the naming service."); - return 0; - } - + CORBA::String_var iorNSUg = orb->object_to_string(objref); + std::string iorNS(iorNSUg); + Engines::IORType iorInput; + auto len = iorNS.length(); + iorInput.length( len ); + for(auto i = 0 ; i < len ; ++i) + iorInput[i] = iorNS[i]; + ns->Register(iorInput,name); return 1; } diff --git a/src/runtime/Test/runtimeTest.cxx b/src/runtime/Test/runtimeTest.cxx index 6af3df6a9..01fa8b123 100644 --- a/src/runtime/Test/runtimeTest.cxx +++ b/src/runtime/Test/runtimeTest.cxx @@ -1665,7 +1665,7 @@ void RuntimeTest::classTeardown() _tc_seqlong->decrRef(); _tc_string->decrRef(); - _myRuntime->fini(); + //_myRuntime->fini(); delete _myRuntime; } diff --git a/src/runtime/Test/runtimeTest.sh b/src/runtime/Test/runtimeTest.sh index a88954398..c5b7cdae4 100644 --- a/src/runtime/Test/runtimeTest.sh +++ b/src/runtime/Test/runtimeTest.sh @@ -20,34 +20,10 @@ BASEDIR=`pwd` TESTDIR=$(mktemp -d --suffix=.yacstest) -OMNIORB_CONFIG=${TESTDIR}/omniorb.cfg -OMNINAMES_LOGDIR=${TESTDIR}/omnilog export TESTDIR -export OMNIORB_CONFIG -export OMNINAMES_LOGDIR echo ${TESTDIR} -echo ${OMNIORB_CONFIG} - -# do not use the default port 2810 for omninames (to improve, cf. SALOME) -echo "InitRef = NameService=corbaname::localhost:2910" > ${OMNIORB_CONFIG} - -rm -rf ${OMNINAMES_LOGDIR} -mkdir ${OMNINAMES_LOGDIR} - -echo $$ - -omniNames -start 2910 & -pidomni=$! -echo $pidomni - -#wait enough time to let omniNames start -sleep 2 - -./runtimeTestEchoSrv & -pidecho=$! -echo $pidecho # this find should work both for make test and for salome test mkdir -p ${TESTDIR}/lib/salome @@ -56,16 +32,15 @@ cp $LIBTEST ${TESTDIR}/lib/salome export TESTCOMPONENT_ROOT_DIR=${TESTDIR} -#wait enough time to let runtimeTestEchoSrv start and register -sleep 2 - cp xmlrun.sh ${TESTDIR} cp TestRuntime ${TESTDIR} +cp runtimeTestEchoSrv ${TESTDIR} +LIBDIR=$BASEDIR/../../test/lib cd ${TESTDIR} -./TestRuntime +LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBDIR ./TestRuntime ret=$? +echo $ret cd - -kill -9 $pidecho $pidomni cat /tmp/${USER}/UnitTestsResult exit $ret diff --git a/src/runtime/TypeConversions.cxx b/src/runtime/TypeConversions.cxx index 1d194dfdc..faed21173 100644 --- a/src/runtime/TypeConversions.cxx +++ b/src/runtime/TypeConversions.cxx @@ -1039,7 +1039,7 @@ namespace YACS CORBA::Object_var obref; try { - obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str()); + obref = getSALOMERuntime()->getFromNS(o.c_str()); #ifdef REFCNT DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount); #endif @@ -2084,7 +2084,7 @@ namespace YACS { try { - obref=getSALOMERuntime()->getOrb()->string_to_object(o.c_str()); + obref=getSALOMERuntime()->getFromNS(o.c_str()); } catch(CORBA::Exception& ex) { diff --git a/src/yacsloader/Test/CTestTestfileInstall.cmake b/src/yacsloader/Test/CTestTestfileInstall.cmake index 9d64cbd80..51ef9b330 100644 --- a/src/yacsloader/Test/CTestTestfileInstall.cmake +++ b/src/yacsloader/Test/CTestTestfileInstall.cmake @@ -19,7 +19,7 @@ IF(NOT WIN32) SET(TEST_NAME ${COMPONENT_NAME}_YacsLoaderTest) - ADD_TEST(${TEST_NAME} ${SALOME_TEST_DRIVER} ${TIMEOUT} ./runYacsLoaderTest.sh) + ADD_TEST(${TEST_NAME} ${PYTHON_TEST_DRIVER} ${TIMEOUT} ./runYacsLoaderTest.sh) SET_TESTS_PROPERTIES(${TEST_NAME} PROPERTIES LABELS "${COMPONENT_NAME}" ENVIRONMENT "LD_LIBRARY_PATH=${YACS_TEST_LIB}:$ENV{LD_LIBRARY_PATH}") diff --git a/src/yacsloader/Test/TestYacsLoader.cxx b/src/yacsloader/Test/TestYacsLoader.cxx index afa308c42..cc9f87cfa 100644 --- a/src/yacsloader/Test/TestYacsLoader.cxx +++ b/src/yacsloader/Test/TestYacsLoader.cxx @@ -20,6 +20,14 @@ #define UNIT_TEST_HEADER " --- TEST src/yacsloader" #include "YacsLoaderTest.hxx" +#include "RuntimeSALOME.hxx" +#include "PythonCppUtils.hxx" + +#if SALOME_KERNEL +#include "SALOME_Embedded_NamingService.hxx" +#include "SALOME_ContainerManager.hxx" +#include "SALOME_NamingService_Wrapper.hxx" +#endif using namespace YACS; using namespace std; @@ -30,4 +38,30 @@ CPPUNIT_TEST_SUITE_REGISTRATION( YacsLoaderTest ); // --- generic Main program from bases/Test -#include "BasicMainTest.hxx" +#include "BasicMainTestInternal.hxx" +#include "RuntimeSALOME.hxx" + +int main() +{ + YACS::ENGINE::RuntimeSALOME::setRuntime(); + YACS::ENGINE::RuntimeSALOME *rt = YACS::ENGINE::getSALOMERuntime(); + if( !rt ) + return 1; + Engines::EmbeddedNamingService_var ns = GetEmbeddedNamingService(); + CORBA::ORB_ptr orb = rt->getOrb(); + CORBA::String_var ior = orb->object_to_string( ns ); + AutoPyRef proc = rt->launchSubProcess({"./echoSrv",std::string(ior)}); + //std::cout << getpid() << std::endl; + usleep(3000000); + int ret = BasicMainTestInternal(); + // + SALOME_NamingService_Wrapper namingService(orb); + CORBA::Object_var objDSM(namingService.Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS)); + Engines::ContainerManager_var dsm(Engines::ContainerManager::_narrow(objDSM)); + dsm->ShutdownContainers(); + // + AutoGIL agil; + AutoPyRef terminateStr = PyUnicode_FromString("terminate"); + AutoPyRef dummy = PyObject_CallMethodObjArgs(proc,terminateStr,nullptr); + return ret; +} diff --git a/src/yacsloader/Test/YacsLoaderTest.cxx b/src/yacsloader/Test/YacsLoaderTest.cxx index e63361827..43df387c8 100644 --- a/src/yacsloader/Test/YacsLoaderTest.cxx +++ b/src/yacsloader/Test/YacsLoaderTest.cxx @@ -70,6 +70,7 @@ int driverTest(Proc* &p, const char* schema) DEBTRACE("+++++++++++++++++++ END execution " << schema); std::ofstream g("titi"); p->writeDot(g); + p->shutdown(10); g.close(); DEBTRACE("+++++++++++++++++++ END test " << schema); return 0; @@ -204,6 +205,7 @@ void YacsLoaderTest::cschema() CPPUNIT_ASSERT_EQUAL(string("Hello Hello coucou!!"), string(text) ); } } + p->shutdown(10); delete p; } #endif @@ -237,6 +239,7 @@ void YacsLoaderTest::dschema() CPPUNIT_ASSERT_EQUAL( string("Hello Hello coucou!!"), string(text)); } } + p->shutdown(10); delete p; } #endif @@ -667,6 +670,7 @@ void YacsLoaderTest::foreachs() CPPUNIT_ASSERT_MESSAGE("Schema: foreach2.xml", p->getEffectiveState() == YACS::DONE ); delete p; ret = driverTest(p, "samples/foreach3.xml"); + p->shutdown(10); CPPUNIT_ASSERT_MESSAGE("Schema: foreach3.xml", ret == 1); delete p; ret = driverTest(p, "samples/foreach4.xml"); @@ -684,6 +688,7 @@ void YacsLoaderTest::foreachs() ret = driverTest(p, "samples/foreach8.xml"); CPPUNIT_ASSERT_MESSAGE("Schema: foreach8.xml", ret == 0); CPPUNIT_ASSERT_MESSAGE("Schema: foreach8.xml", p->getEffectiveState() == YACS::DONE ); + p->shutdown(10); delete p; ret = driverTest(p, "samples/foreach_init2fin.xml"); CPPUNIT_ASSERT_MESSAGE("Schema: foreach_init2fin.xml", ret == 0); diff --git a/src/yacsloader/Test/echoSrv.cxx b/src/yacsloader/Test/echoSrv.cxx index 6212fc3b0..21930e01d 100644 --- a/src/yacsloader/Test/echoSrv.cxx +++ b/src/yacsloader/Test/echoSrv.cxx @@ -29,14 +29,18 @@ #include #include +#include //#define _DEVDEBUG_ #include "YacsTrace.hxx" +#include +#include CORBA_CLIENT_HEADER(SALOME_Embedded_NamingService) + using namespace std; CORBA::ORB_var orb; -static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*); +static CORBA::Boolean bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr,const char*); static ostream& operator<<(ostream& os, const CORBA::Exception& e) { @@ -377,6 +381,15 @@ int main(int argc, char** argv) try { orb = CORBA::ORB_init(argc, argv); + std::string theIOR( argv[1] ); + + CORBA::Object_var obj = orb->string_to_object(theIOR.c_str()); + Engines::EmbeddedNamingService_var ns = Engines::EmbeddedNamingService::_narrow( obj ); + if( CORBA::is_nil( ns ) ) + return 1; + //std::cout << getpid() << std::endl; + //usleep(20000000); + { CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj); @@ -408,29 +421,28 @@ int main(int argc, char** argv) // print the reference as a stringified IOR. CORBA::String_var sior(orb->object_to_string(obj2)); DEBTRACE("'" << (char*)sior << "'"); - - if( !bindObjectToName(orb, myechoref,"Echo") ) return 1; + if( !bindObjectToName(ns, myechoref,"Echo") ) return 1; //create object C and register it in naming service C_i* myC = new C_i(); CORBA::Object_var obj3 =myC->_this(); eo::C_var myCref=eo::C::_narrow(obj3); myC->_remove_ref(); - if( !bindObjectToName(orb, myCref,"C") ) return 1; + if( !bindObjectToName(ns, myCref,"C") ) return 1; //create object D and register it in naming service D_i* myD = new D_i(); CORBA::Object_var obj4=myD->_this(); eo::D_var myDref=eo::D::_narrow(obj4); myD->_remove_ref(); - if( !bindObjectToName(orb, myDref,"D") ) return 1; + if( !bindObjectToName(ns, myDref,"D") ) return 1; //create object Obj and register it in naming service Obj_i* myObj = new Obj_i(); CORBA::Object_var obj5=myObj->_this(); eo::Obj_var myObjref=eo::Obj::_narrow(obj5); myObj->_remove_ref(); - if( !bindObjectToName(orb, myObjref,"Obj") ) return 1; + if( !bindObjectToName(ns, myObjref,"Obj") ) return 1; } orb->run(); std::cout << "Returned from orb->run()." << std::endl; @@ -459,90 +471,16 @@ int main(int argc, char** argv) ////////////////////////////////////////////////////////////////////// static CORBA::Boolean -bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name) +bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr objref,const char *name) { - CosNaming::NamingContext_var rootContext; - - try { - // Obtain a reference to the root context of the Name service: - CORBA::Object_var obj; - obj = orb->resolve_initial_references("NameService"); - - // Narrow the reference returned. - rootContext = CosNaming::NamingContext::_narrow(obj); - if( CORBA::is_nil(rootContext) ) { - DEBTRACE("Failed to narrow the root naming context."); - return 0; - } - } - catch(CORBA::ORB::InvalidName& ex) { - // This should not happen! - DEBTRACE("Service required is invalid [does not exist]." ); - return 0; - } - - try { - // Bind a context called "test" to the root context: - - CosNaming::Name contextName; - contextName.length(1); - contextName[0].id = (const char*) "test"; // string copied - contextName[0].kind = (const char*) "my_context"; // string copied - // Note on kind: The kind field is used to indicate the type - // of the object. This is to avoid conventions such as that used - // by files (name.type -- e.g. test.ps = postscript etc.) - - CosNaming::NamingContext_var testContext; - try { - // Bind the context to root. - testContext = rootContext->bind_new_context(contextName); - } - catch(CosNaming::NamingContext::AlreadyBound& ex) { - // If the context already exists, this exception will be raised. - // In this case, just resolve the name and assign testContext - // to the object returned: - CORBA::Object_var obj; - obj = rootContext->resolve(contextName); - testContext = CosNaming::NamingContext::_narrow(obj); - if( CORBA::is_nil(testContext) ) { - DEBTRACE("Failed to narrow naming context."); - return 0; - } - } - - // Bind objref with name Echo to the testContext: - CosNaming::Name objectName; - objectName.length(1); - objectName[0].id = name; // string copied - objectName[0].kind = (const char*) "Object"; // string copied - - try { - testContext->bind(objectName, objref); - } - catch(CosNaming::NamingContext::AlreadyBound& ex) { - testContext->rebind(objectName, objref); - } - // Note: Using rebind() will overwrite any Object previously bound - // to /test/Echo with obj. - // Alternatively, bind() can be used, which will raise a - // CosNaming::NamingContext::AlreadyBound exception if the name - // supplied is already bound to an object. - - // Amendment: When using OrbixNames, it is necessary to first try bind - // and then rebind, as rebind on it's own will throw a NotFoundexception if - // the Name has not already been bound. [This is incorrect behaviour - - // it should just bind]. - } - catch(CORBA::COMM_FAILURE& ex) { - DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the " - << "naming service."); - return 0; - } - catch(CORBA::SystemException&) { - DEBTRACE("Caught a CORBA::SystemException while using the naming service."); - return 0; - } - + CORBA::String_var iorNSUg = orb->object_to_string(objref); + std::string iorNS(iorNSUg); + Engines::IORType iorInput; + auto len = iorNS.length(); + iorInput.length( len ); + for(auto i = 0 ; i < len ; ++i) + iorInput[i] = iorNS[i]; + ns->Register(iorInput,name); return 1; } diff --git a/src/yacsloader/Test/runYacsLoaderTest.sh b/src/yacsloader/Test/runYacsLoaderTest.sh index dcc3b46ea..615a8a48b 100644 --- a/src/yacsloader/Test/runYacsLoaderTest.sh +++ b/src/yacsloader/Test/runYacsLoaderTest.sh @@ -19,8 +19,6 @@ # # script used by "salome test" command -./echoSrv & -pidecho=$! BASEDIR=`pwd` TESTDIR=$(mktemp -d --suffix=.yacstest) @@ -34,12 +32,11 @@ LIBDIR=$BASEDIR/../lib cp xmlrun.sh $TESTDIR cp *.py $TESTDIR ln -s $BASEDIR/samples $TESTDIR/samples +cp $BASEDIR/echoSrv $TESTDIR cd $TESTDIR LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBDIR $BASEDIR/TestYacsLoader ret=$? cd $BASEDIR echo "exec status TestYacsLoader " $ret -kill -9 $pidecho - exit $ret diff --git a/src/yacsloader_swig/Test/CMakeLists.txt b/src/yacsloader_swig/Test/CMakeLists.txt index 7d7e15a9b..b1092982d 100644 --- a/src/yacsloader_swig/Test/CMakeLists.txt +++ b/src/yacsloader_swig/Test/CMakeLists.txt @@ -41,6 +41,7 @@ IF(NOT WIN32) testResume.py testSave.py testSaveLoadRun.py + testYacsLoaderSwig.py optim_plugin.py testValidationChecks.py testProgress.py @@ -51,8 +52,6 @@ IF(NOT WIN32) ) INSTALL(PROGRAMS ${LOCAL_TEST_FILES} DESTINATION ${LOCAL_TEST_DIR}) - INSTALL(PROGRAMS runUnitTest.sh - DESTINATION ${LOCAL_TEST_DIR}) INSTALL(FILES CTestTestfileInstall.cmake DESTINATION ${LOCAL_TEST_DIR} RENAME CTestTestfile.cmake) diff --git a/src/yacsloader_swig/Test/CTestTestfileInstall.cmake b/src/yacsloader_swig/Test/CTestTestfileInstall.cmake index fad6c31a8..fbb046d97 100644 --- a/src/yacsloader_swig/Test/CTestTestfileInstall.cmake +++ b/src/yacsloader_swig/Test/CTestTestfileInstall.cmake @@ -19,9 +19,10 @@ IF(NOT WIN32) SET(TEST_NAME ${COMPONENT_NAME}_YacsLoaderTest_swig) - ADD_TEST(${TEST_NAME} ${SALOME_TEST_DRIVER} ${TIMEOUT} ./runUnitTest.sh) + ADD_TEST(${TEST_NAME} testYacsLoaderSwig.py) SET_TESTS_PROPERTIES(${TEST_NAME} PROPERTIES - LABELS "${COMPONENT_NAME}" + LABELS "${COMPONENT_NAME}" + ENVIRONMENT "SALOME_EMB_SERVANT=1" ) SET(TEST_NAME ${COMPONENT_NAME}_StdAloneYacsLoaderTest1) diff --git a/src/yacsloader_swig/Test/runUnitTest.sh b/src/yacsloader_swig/Test/runUnitTest.sh deleted file mode 100755 index 3029264b9..000000000 --- a/src/yacsloader_swig/Test/runUnitTest.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -# Copyright (C) 2006-2023 CEA, EDF -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -# - -# script used by "salome test" command -../yacsloader/echoSrv & -pidecho=$! - -# give some time for echoSrv to start... -sleep 3 - -export TESTCOMPONENT_ROOT_DIR=`pwd`/../runtime - -#python3 -m unittest discover -MODULES_TO_TEST="testEdit testExec testLoader testSave " - -python3 -m unittest $MODULES_TO_TEST -ret=$? -echo "exec status salome_test_driver " $ret - -kill -9 $pidecho - -exit $ret diff --git a/src/yacsloader_swig/Test/testExec.py b/src/yacsloader_swig/Test/testExec.py index eac472abb..6ed7c4ad8 100755 --- a/src/yacsloader_swig/Test/testExec.py +++ b/src/yacsloader_swig/Test/testExec.py @@ -185,11 +185,20 @@ class TestExec(unittest.TestCase): pass if __name__ == '__main__': - dir_test = tempfile.mkdtemp(suffix=".yacstest") - file_test = os.path.join(dir_test,"UnitTestsResult") - with open(file_test, 'a') as f: - f.write(" --- TEST src/yacsloader: testExec.py\n") - suite = unittest.makeSuite(TestExec) - result = unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite) - + import salome + import NamingService + import os + import subprocess + salome.salome_init() + ior = NamingService.NamingService.IOROfNS() + p = subprocess.Popen(["../yacsloader/echoSrv",ior]) + import time + time.sleep(3) + with tempfile.TemporaryDirectory(suffix=".yacstest") as dir_test: + file_test = os.path.join(dir_test,"UnitTestsResult") + with open(file_test, 'a') as f: + f.write(" --- TEST src/yacsloader: testExec.py\n") + suite = unittest.makeSuite(TestExec) + result = unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite) + p.terminate() sys.exit(not result.wasSuccessful()) diff --git a/src/yacsloader_swig/Test/testLoader.py b/src/yacsloader_swig/Test/testLoader.py index 5b8fc61e2..be94a3058 100755 --- a/src/yacsloader_swig/Test/testLoader.py +++ b/src/yacsloader_swig/Test/testLoader.py @@ -84,12 +84,22 @@ class TestLoader(unittest.TestCase): pass if __name__ == '__main__': + import salome + import NamingService + import os + import subprocess + salome.salome_init() + ior = NamingService.NamingService.IOROfNS() + p = subprocess.Popen(["../yacsloader/echoSrv",ior]) + import time + time.sleep(3) import tempfile import os - dir_test = tempfile.mkdtemp(suffix=".yacstest") - file_test = os.path.join(dir_test,"UnitTestsResult") - with open(file_test, 'a') as f: - f.write(" --- TEST src/yacsloader: testLoader.py\n") - suite = unittest.makeSuite(TestLoader) - result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite) + with tempfile.TemporaryDirectory(suffix=".yacstest") as dir_test: + file_test = os.path.join(dir_test,"UnitTestsResult") + with open(file_test, 'a') as f: + f.write(" --- TEST src/yacsloader: testLoader.py\n") + suite = unittest.makeSuite(TestLoader) + result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite) + p.terminate() sys.exit(not result.wasSuccessful()) diff --git a/src/yacsloader_swig/Test/testSave.py b/src/yacsloader_swig/Test/testSave.py index 9e5facf71..61fe5e6dd 100755 --- a/src/yacsloader_swig/Test/testSave.py +++ b/src/yacsloader_swig/Test/testSave.py @@ -56,6 +56,7 @@ class TestSave(unittest.TestCase): l = loader.YACSLoader() e = pilot.ExecutorSwig() for schema in schemaList: + print(schema) fileOrig = "samples/" + schema + ".xml" saveSchema1 = os.path.join(self.workdir, "schema1_" + schema) dumpSchema1 = os.path.join(self.workdir, "dump1_" + schema) @@ -99,10 +100,20 @@ class TestSave(unittest.TestCase): pass if __name__ == '__main__': - dir_test = tempfile.mkdtemp(suffix=".yacstest") - file_test = os.path.join(dir_test,"UnitTestsResult") - with open(file_test, 'a') as f: + import salome + import NamingService + import os + import subprocess + salome.salome_init() + ior = NamingService.NamingService.IOROfNS() + p = subprocess.Popen(["../yacsloader/echoSrv",ior]) + import time + time.sleep(3) + with tempfile.TemporaryDirectory(suffix=".yacstest") as dir_test: + file_test = os.path.join(dir_test,"UnitTestsResult") + with open(file_test, 'a') as f: f.write(" --- TEST src/yacsloader: testSave.py\n") suite = unittest.makeSuite(TestSave) result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite) + p.terminate() sys.exit(not result.wasSuccessful()) diff --git a/src/yacsloader_swig/Test/testYacsLoaderSwig.py b/src/yacsloader_swig/Test/testYacsLoaderSwig.py new file mode 100644 index 000000000..cb2678970 --- /dev/null +++ b/src/yacsloader_swig/Test/testYacsLoaderSwig.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# Copyright (C) 2023 CEA/DEN, EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +import salome +import NamingService +import os +import sys +import subprocess +salome.salome_init() +ior = NamingService.NamingService.IOROfNS() + +from testEdit import TestEdit +from testExec import TestExec +from testLoader import TestLoader +from testSave import TestSave + +p = subprocess.Popen(["../yacsloader/echoSrv",ior]) +import time +time.sleep(3) +import tempfile +import unittest +zezeResult = True +zeResultStr = [] +with tempfile.TemporaryDirectory(suffix=".yacstest") as dir_test: + file_test = os.path.join(dir_test,"UnitTestsResult") + with open(file_test, 'a') as f: + f.write(" --- TEST src/yacsloader: testLoader.py\n") + for zeTest in [TestEdit,TestExec,TestLoader,TestSave]: + suite = unittest.makeSuite(zeTest) + result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite) + zeResult = result.wasSuccessful() + zezeResult = zeResult and zezeResult + zeResultStr.append( "Result for {} is {}".format(str(zeTest),zeResult) ) +p.terminate() +zeResultStr.append("So at the end the result is {}".format(zezeResult)) +print("\n".join(zeResultStr)) +returnCode = int( not zezeResult ) +print("Return code is {}".format( returnCode )) +sys.exit(returnCode) -- 2.39.2