Salome HOME
Merge remote branch 'origin/V7_4_BR'
[modules/kernel.git] / bin / appliskel / tests / concurrentSession / TestConcurrentSession.py
1 # Copyright (C) 2013-2014  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 import unittest
21 import tempfile
22
23 import os
24 import sys
25 import imp
26 from cStringIO import StringIO
27 import multiprocessing
28 import logging
29
30 class TestConcurrentLaunch(unittest.TestCase):
31   def setUp(self):
32     # Initialize path to SALOME application
33     path_to_launcher = os.getenv("SALOME_LAUNCHER")
34     appli_dir = os.path.dirname(path_to_launcher)
35     sys.path[:0] = [os.path.join(appli_dir, "bin", "salome", "appliskel")]
36
37     # Configure session startup
38     self.SALOME = imp.load_source("SALOME", os.path.join(appli_dir,"salome"))
39     self.SALOME_appli_args = ["start", "-t"]
40     self.SALOME_shell_args = ["shell"]
41   #
42   def tearDown(self):
43     pass
44   #
45   def appli(self, args=None):
46     if args is None:
47       args = []
48     try:
49       self.SALOME.main(self.SALOME_appli_args + args)
50     except SystemExit, e:
51       if str(e) != '0':
52         logging.error(e)
53       pass
54   #
55   def session(self, args=None):
56     if args is None:
57       args = []
58     try:
59       self.SALOME.main(self.SALOME_shell_args + args)
60     except SystemExit, e:
61       if str(e) != '0':
62         logging.error(e)
63       pass
64   #
65   def test01_SingleSession(self):
66     print "** Testing single session **"
67     self.session(["hello.py"])
68   #
69   def test02_MultiSession(self):
70     print "** Testing multi sessions **"
71     jobs = []
72     for i in range(9):
73       p = multiprocessing.Process(target=self.session, args=(["hello.py"],))
74       jobs.append(p)
75       p.start()
76
77     for j in jobs:
78       j.join()
79   #
80   def test03_SingleAppli(self):
81     print "** Testing single appli **"
82     current_directory = os.path.dirname(os.path.abspath(__file__))
83     session_log = tempfile.NamedTemporaryFile(prefix='session_', suffix='.log')
84     self.appli(["--ns-port-log=%s"%session_log.name])
85     port_number = None
86     with open(session_log.name, "r") as f:
87       port_number = f.readline()
88     self.session(["hello.py"])
89     self.session(["-p", port_number, "killSalomeWithPort.py", "args:%s"%port_number])
90     session_log.close()
91   #
92   def test04_MultiAppli(self):
93     print "** Testing multi appli **"
94     jobs = []
95     for i in range(9):
96       p = multiprocessing.Process(target=self.test03_SingleAppli)
97       jobs.append(p)
98       p.start()
99
100     for j in jobs:
101       j.join()
102   #
103 #
104
105 if __name__ == "__main__":
106   path_to_launcher = os.getenv("SALOME_LAUNCHER")
107   if not path_to_launcher:
108     msg = "\n"
109     msg += "Error: please set SALOME_LAUNCHER variable to the salome command in your application folder.\n"
110     logging.error(msg)
111     sys.exit(1)
112
113   if not os.path.isfile("hello.py"):
114     with open("hello.py", "w") as f:
115       f.write("print 'Hello!'")
116
117   unittest.main()
118 #