Salome HOME
bos #26457 Factorization of ORB initialization
[modules/kernel.git] / src / LifeCycleCORBA / TestContainerManager.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  SALOME TestContainer : test of container creation and its life cycle
21 //  File   : TestContainer.cxx
22 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
23 //  Module : SALOME
24 //  $Header$
25 //
26 #include "utilities.h"
27 #include <iostream>
28 #ifndef WIN32
29 #include <unistd.h>
30 #endif
31 #include <SALOMEconfig.h>
32 #include "ArgvKeeper.hxx"
33 #include "SALOME_NamingService.hxx"
34 #include "SALOME_ResourcesManager.hxx"
35 #include "SALOME_ContainerManager.hxx"
36 #include "SALOME_LifeCycleCORBA.hxx"
37 #include "SALOME_ResourcesManager.hxx"
38 #include "NamingService_WaitForServerReadiness.hxx"
39 #include "OpUtil.hxx"
40 #include "Utils_SALOME_Exception.hxx"
41 #include "Utils_CommException.hxx"
42 #include "Basics_DirUtils.hxx"
43
44 int main (int argc, char * argv[])
45 {
46   std::map<std::string, int> cycle;
47   std::map<std::string, int> first;
48   Engines::Container_ptr cont;
49   Engines::EngineComponent_ptr compo;
50   bool error = false;
51   bool bestImplemented;
52   int status;
53
54   // Initializing omniORB
55   SetArgcArgv( argc, argv ) ;
56   CORBA::ORB_ptr orb = KERNEL::GetRefToORB();
57
58   SALOME_NamingService *_NS=new SALOME_NamingService(orb);
59
60   CORBA::Object_var obj = _NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
61   ASSERT( !CORBA::is_nil(obj));
62   Engines::ContainerManager_var _ContManager=Engines::ContainerManager::_narrow(obj);
63   obj = _NS->Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
64   ASSERT( !CORBA::is_nil(obj));
65   Engines::ResourcesManager_var _ResManager=Engines::ResourcesManager::_narrow(obj);
66
67   Engines::ContainerParameters p;
68   p.resource_params.componentList.length(2);
69   p.resource_params.componentList[0] = "MED";
70   p.resource_params.componentList[1] = "GEOM";
71
72   p.resource_params.hostname = "";
73   p.resource_params.OS = "LINUX";
74   p.resource_params.mem_mb = 1000;
75   p.resource_params.cpu_clock = 1000;
76   p.resource_params.nb_proc_per_node = 1;
77   p.resource_params.nb_node = 1;
78   p.isMPI = false;
79
80   char st[10];
81   for(int i=0;i<10;i++){
82     sprintf(st,"cycl_%d",i);
83     p.container_name = CORBA::string_dup(st);
84     p.resource_params.policy="cycl";
85     cont = _ContManager->GiveContainer(p);
86     if(CORBA::is_nil(cont)) error = true;
87   }
88
89   for(int i=0;i<10;i++){
90     sprintf(st,"first_%d",i);
91     p.container_name = CORBA::string_dup(st);
92     p.resource_params.policy="first";
93     cont = _ContManager->GiveContainer(p);
94     if(CORBA::is_nil(cont)) error = true;
95   }
96
97   p.container_name = CORBA::string_dup("best");
98   p.resource_params.policy="best";
99   cont = _ContManager->GiveContainer(p);
100   if(CORBA::is_nil(cont)) bestImplemented = false;
101   else bestImplemented = true;
102
103   SALOME_LifeCycleCORBA LCC(_NS);
104   p.container_name = CORBA::string_dup("FactoryServer");
105   compo = LCC.FindOrLoad_Component(p,"SMESH");
106   if(CORBA::is_nil(compo)) error = true;
107   compo = LCC.FindOrLoad_Component(p,"SMESH");
108   if(CORBA::is_nil(compo)) error = true;
109
110   _NS->Change_Directory("/Containers");
111
112   std::vector<std::string> vec = _NS->list_directory_recurs();
113   std::list<std::string> lstCont;
114   for(std::vector<std::string>::iterator iter = vec.begin();iter!=vec.end();iter++){
115     CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
116     Engines::Container_var cont=Engines::Container::_narrow(obj);
117     if(!CORBA::is_nil(cont)){
118       cycle[cont->getHostName()]=0;
119       first[cont->getHostName()]=0;
120       lstCont.push_back((*iter));
121     }
122   }
123   for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
124     CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
125     Engines::Container_var cont=Engines::Container::_narrow(obj);
126     if(!CORBA::is_nil(cont)){
127       std::string basename = Kernel_Utils::GetBaseName(cont->name());
128       if(basename.compare(0,4,"cycl")==0)
129         cycle[cont->getHostName()]++;
130       if(basename.compare(0,5,"first")==0)
131         first[cont->getHostName()]++;
132     }
133   }
134   _ContManager->ShutdownContainers();
135
136   int cmin=10;
137   int cmax=0;
138   int fmin=10;
139   int fmax=0;
140   int nbpmax=1;
141   for(std::map<std::string,int>::iterator iter=cycle.begin();iter!=cycle.end();iter++){
142     if(strcmp((*iter).first.c_str(),"localhost")!=0){
143       Engines::ResourceDefinition *p = _ResManager->GetResourceDefinition((*iter).first.c_str());
144       int nbproc = p->nb_node * p->nb_proc_per_node;
145       if(cycle[(*iter).first]/nbproc<cmin) cmin=cycle[(*iter).first]/nbproc;
146       if(cycle[(*iter).first]/nbproc>cmax) cmax=cycle[(*iter).first]/nbproc;
147       if(first[(*iter).first]/nbproc<fmin) fmin=first[(*iter).first]/nbproc;
148       if(first[(*iter).first]/nbproc>fmax){
149         fmax=first[(*iter).first]/nbproc;
150         nbpmax = nbproc;
151       }
152     }
153   }
154   std::string msg;
155   if( ((cmax-cmin) <= 1) && (fmax == 10/nbpmax) && !error ){
156     if(bestImplemented)
157       msg = "TEST OK";
158     else
159       msg = "TEST OK but FindBest not implemented!";
160     status=0;
161   }
162   else{
163     msg ="TEST KO";
164     status=1;
165   }
166   std::cout << msg << std::endl;
167
168   return status;
169 }