Salome HOME
Synchronize adm files
[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=[]):
46     try:
47       self.SALOME.main(self.SALOME_appli_args + args)
48     except SystemExit, e:
49       if str(e) != '0':
50         logging.error(e)
51       pass
52   #
53   def session(self, args=[]):
54     try:
55       self.SALOME.main(self.SALOME_shell_args + args)
56     except SystemExit, e:
57       if str(e) != '0':
58         logging.error(e)
59       pass
60   #
61   def test01_SingleSession(self):
62     print "** Testing single session **"
63     self.session(["hello.py"])
64   #
65   def test02_MultiSession(self):
66     print "** Testing multi sessions **"
67     jobs = []
68     for i in range(9):
69       p = multiprocessing.Process(target=self.session, args=(["hello.py"],))
70       jobs.append(p)
71       p.start()
72
73     for j in jobs:
74       j.join()
75   #
76   def test03_SingleAppli(self):
77     print "** Testing single appli **"
78     current_directory = os.path.dirname(os.path.abspath(__file__))
79     session_log = tempfile.NamedTemporaryFile(prefix='session_', suffix='.log')
80     self.appli(["--ns-port-log=%s"%session_log.name])
81     port_number = None
82     with open(session_log.name, "r") as f:
83       port_number = f.readline()
84     self.session(["hello.py"])
85     self.session(["-p", port_number, "killSalomeWithPort.py", "args:%s"%port_number])
86     session_log.close()
87   #
88   def test04_MultiAppli(self):
89     print "** Testing multi appli **"
90     jobs = []
91     for i in range(9):
92       p = multiprocessing.Process(target=self.test03_SingleAppli)
93       jobs.append(p)
94       p.start()
95
96     for j in jobs:
97       j.join()
98   #
99 #
100
101 if __name__ == "__main__":
102   path_to_launcher = os.getenv("SALOME_LAUNCHER")
103   if not path_to_launcher:
104     msg = "\n"
105     msg += "Error: please set SALOME_LAUNCHER variable to the salome command in your application folder.\n"
106     logging.error(msg)
107     sys.exit(1)
108
109   if not os.path.isfile("hello.py"):
110     with open("hello.py", "w") as f:
111       f.write("print 'Hello!'")
112
113   unittest.main()
114 #