From 8d8717008ebf9fb41ed6d7ab98c703ff1f9d96df Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 23 Nov 2017 16:25:51 +0100 Subject: [PATCH] Test of new holdRequests activeRequests --- idl/SALOME_SDS.idl | 1 + src/SALOMESDS/SALOMESDS_DataScopeServer.cxx | 18 ++++++ src/SALOMESDS/SALOMESDS_DataScopeServer.hxx | 1 + src/SALOMESDS/SALOMESDS_Exception.cxx | 14 ++++- src/SALOMESDS/SALOMESDS_Exception.hxx | 3 + src/SALOMESDS/TestSalomeSDS.py | 69 ++++++++++++++++++++- 6 files changed, 102 insertions(+), 4 deletions(-) diff --git a/idl/SALOME_SDS.idl b/idl/SALOME_SDS.idl index 2f0f26ed6..51048ba1a 100644 --- a/idl/SALOME_SDS.idl +++ b/idl/SALOME_SDS.idl @@ -89,6 +89,7 @@ module SALOME ByteVec fetchSerializedContent(in string varName) raises (SALOME::SALOME_Exception); SeqOfByteVec getAllKeysOfVarWithTypeDict(in string varName) raises (SALOME::SALOME_Exception); RequestSwitcher getRequestSwitcher(); + void takeANap(in double napDurationInSec) raises (SALOME::SALOME_Exception); }; interface DataScopeServer : DataScopeServerBase diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx index 5045833bd..069b61882 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx @@ -266,6 +266,24 @@ SALOME::RequestSwitcher_ptr DataScopeServerBase::getRequestSwitcher() return SALOME::RequestSwitcher::_narrow(obj); } +void DataScopeServerBase::takeANap(CORBA::Double napDurationInSec) +{ + if(napDurationInSec<0.) + throw Exception("DataScopeServerBase::takeANap : negative value !"); +#ifndef WIN32 + struct timespec req,rem; + long nbSec((long)napDurationInSec); + double remainTime(napDurationInSec-(double)nbSec); + req.tv_sec=nbSec; + req.tv_nsec=(long)(remainTime*1000000.); + int ret(nanosleep(&req,&rem)); + if(ret!=0) + throw Exception("DataScopeServerBase::takeANap : nap not finished as expected !"); +#else + throw Exception("DataScopeServerBase::takeANap : not implemented for Windows !"); +#endif +} + void DataScopeServerBase::initializePython(int argc, char *argv[]) { Py_Initialize(); diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx index e06ec8b22..eebdb37ec 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx @@ -77,6 +77,7 @@ namespace SALOMESDS SALOME::ByteVec *fetchSerializedContent(const char *varName); SALOME::SeqOfByteVec *getAllKeysOfVarWithTypeDict(const char *varName); SALOME::RequestSwitcher_ptr getRequestSwitcher(); + void takeANap(CORBA::Double napDurationInSec); public: ~DataScopeServerBase(); BasicDataServer *retrieveVarInternal2(const std::string& varName); diff --git a/src/SALOMESDS/SALOMESDS_Exception.cxx b/src/SALOMESDS/SALOMESDS_Exception.cxx index 96c6cf506..d5ddd8447 100644 --- a/src/SALOMESDS/SALOMESDS_Exception.cxx +++ b/src/SALOMESDS/SALOMESDS_Exception.cxx @@ -21,12 +21,20 @@ #include "SALOMESDS_Exception.hxx" SALOMESDS::Exception::Exception(const std::string& reason) +{ + this->assign(reason.c_str()); +} + +SALOMESDS::Exception::Exception(const char *reason) +{ + this->assign(reason); +} + +void SALOMESDS::Exception::assign(const char *reason) { SALOME::ExceptionStruct es; es.type=SALOME::INTERNAL_ERROR; - es.text=CORBA::string_dup(reason.c_str()); + es.text=CORBA::string_dup(reason); es.lineNumber=0; (*this).details=es; } - - diff --git a/src/SALOMESDS/SALOMESDS_Exception.hxx b/src/SALOMESDS/SALOMESDS_Exception.hxx index 28d910a15..61b77f33a 100644 --- a/src/SALOMESDS/SALOMESDS_Exception.hxx +++ b/src/SALOMESDS/SALOMESDS_Exception.hxx @@ -34,6 +34,9 @@ namespace SALOMESDS { public: Exception(const std::string& reason); + Exception(const char *reason); + private: + void assign(const char *reason); }; } diff --git a/src/SALOMESDS/TestSalomeSDS.py b/src/SALOMESDS/TestSalomeSDS.py index bdbdc6761..338953335 100644 --- a/src/SALOMESDS/TestSalomeSDS.py +++ b/src/SALOMESDS/TestSalomeSDS.py @@ -26,8 +26,11 @@ import unittest import cPickle import gc import time +from datetime import datetime import multiprocessing as mp +nbOfSecWait=1. + def obj2Str(obj): return cPickle.dumps(obj,cPickle.HIGHEST_PROTOCOL) def str2Obj(strr): @@ -54,7 +57,35 @@ def work(t): print out print err return proc.returncode - + +def func_test7(scopeName,l,l2,cv): + salome.salome_init() + varName="a" + zeValue={"ab":[4,5,6]} + dsm=salome.naming_service.Resolve("/DataServerManager") + dss,isCreated=dsm.giveADataScopeTransactionCalled(scopeName) # should be suspended nbOfSecWait s by main process + assert(not isCreated) + l.release() # tell manager that I'm ready + l2.acquire() # wait for manager to start micro-test1 + ######### micro-test1 - check that all requests are suspended + s=datetime.now() + t0=dss.createRdWrVarTransac(varName,obj2Str(zeValue)) + s=(datetime.now()-s).total_seconds() + assert(s>=0.99*nbOfSecWait and s=0. and s<0.05) # expect to be not locked + ######### end of micro-test2 + with cv: + cv.notify_all() + dss.takeANap(nbOfSecWait) # emulate a DataServer occupation + pass + class SalomeSDSTest(unittest.TestCase): def testList1(self): @@ -309,6 +340,42 @@ class SalomeSDSTest(unittest.TestCase): self.assertEqual(str2Obj(dss.fetchSerializedContent(varName)),{'ab':[4,5,6],'cd':[7,8,9,10]}) pass + def testLockToDump(self): + """ Test to check that holdRequests method. This method wait for clean server status and hold it until activeRequests is called. + Warning this method expects a not overloaded machine to be run because test is based on ellapse time. + """ + scopeName="Scope1" + dsm=salome.naming_service.Resolve("/DataServerManager") + dsm.cleanScopesInNS() + if scopeName in dsm.listScopes(): + dsm.removeDataScope(scopeName) + # l is for main process sync. to be sure to launch test when sub process is ready + # l2 lock is for sub process sync. + l=mp.Lock(); l2=mp.Lock() + l.acquire() ; l2.acquire() + cv=mp.Condition(mp.Lock()) + dss,isCreated=dsm.giveADataScopeTransactionCalled(scopeName) + #assert(isCreated) + p=mp.Process(target=func_test7,args=(scopeName,l,l2,cv)) + p.start() + l.acquire() + rs=dss.getRequestSwitcher() ; rs.holdRequests() # The aim of the test + l2.release() # tell slave process that it's ready for micro-test1 + time.sleep(nbOfSecWait) + rs.activeRequests() # The aim of the test + ######### micro-test3 - check that holdRequests is able to wait for a non finished job + with cv: + cv.wait() + s=datetime.now() + time.sleep(0.01) # let main proc the priority + rs.holdRequests() # the aim of the test is here. main process is occupied 1s -> holdRequests is Expected to wait + s=(datetime.now()-s).total_seconds() + rs.activeRequests() + assert(s>=0.99*nbOfSecWait and s