2 # Copyright (C) 2013-2020 CEA/DEN, EDF R&D, OPEN CASCADE
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 import multiprocessing
27 def port_reservation(obtained_ports, preferred=None, test=None, expected=None):
28 from PortManager import getPort
30 port = getPort(preferred)
33 print("obtained port = %s"%port)
35 obtained_ports.put(port)
38 test.assertTrue(port == expected, "used = %s, expected = %s"%(port, expected))
41 class TestMinimalExample(unittest.TestCase):
42 def testSequential(self):
43 from PortManager import releasePort, getBusyPorts
44 print("\nBEGIN testSequential")
45 print("Busy ports", getBusyPorts())
46 obtained_ports = multiprocessing.Queue()
49 multiprocessing.Process(target=port_reservation, args=(obtained_ports,))
59 print("Busy ports", getBusyPorts())
60 # Try to get specific port number
61 p = multiprocessing.Process(target=port_reservation, args=(obtained_ports, 2872, self, 2872,))
65 # Try to get specific port number
66 p = multiprocessing.Process(target=port_reservation, args=(obtained_ports, 2812, self,))
70 # Try to get specific port number
71 p = multiprocessing.Process(target=port_reservation, args=(obtained_ports, 2899, self, 2899,))
76 print ("release port 2899")
77 p = multiprocessing.Process(target=releasePort, args=(2899,))
81 # Try to get specific port number
82 p = multiprocessing.Process(target=port_reservation, args=(obtained_ports, 2899, self, 2899,))
87 print("Busy ports", getBusyPorts())
88 while not obtained_ports.empty():
89 port = obtained_ports.get()
90 print("release port", port)
91 p = multiprocessing.Process(target=releasePort, args=(port,))
95 print("END testSequential")
98 def testConcurrent(self):
99 from PortManager import releasePort, getBusyPorts
100 print("\nBEGIN testConcurrent")
101 print("Busy ports", getBusyPorts())
102 obtained_ports = multiprocessing.Queue()
104 multiprocessing.Process(target=port_reservation, args=(obtained_ports,))
109 # Try to get specific port number
110 p = multiprocessing.Process(target=port_reservation, args=(obtained_ports, 2872, self, 2872,))
113 # Try to get specific port number
114 p = multiprocessing.Process(target=port_reservation, args=(obtained_ports, 2812,))
117 # Try to get specific port number
118 p = multiprocessing.Process(target=port_reservation, args=(obtained_ports, 2812,))
128 print("Busy ports", getBusyPorts())
129 while not obtained_ports.empty():
130 port = obtained_ports.get()
131 print("release port", port)
132 p = multiprocessing.Process(target=releasePort, args=(port,))
136 print("END testConcurrent")
140 if __name__ == "__main__":
141 omniorb_user_path = os.getenv("OMNIORB_USER_PATH")
142 if not omniorb_user_path:
144 msg += "Error: please set OMNIORB_USER_PATH variable.\n"
145 msg += " Usually this points to your application USERS directory.\n"
153 msg += "Error: can't import PortManager; please check PYTHONPATH variable.\n"
154 msg += " You need to add <KERNEL_INSTALL_PATH>/bin/salome path.\n"