adding multiprocess check

This commit is contained in:
AleaJactaEst 2018-02-09 23:29:36 +01:00
parent 4a5bd254e7
commit e5fe6e5388

View file

@ -25,6 +25,7 @@ import multiprocessing
import time import time
import re import re
import queue import queue
import signal
from unittest.mock import patch from unittest.mock import patch
try: try:
@ -41,6 +42,12 @@ except ImportError:
# sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# import pymanager.certificate as cert # import pymanager.certificate as cert
def handler(signum, frame):
print("TimeOut !")
raise Exception("end of time")
class TestManager(unittest.TestCase): class TestManager(unittest.TestCase):
def setUp(self): def setUp(self):
self.openssl = '/usr/bin/openssl' self.openssl = '/usr/bin/openssl'
@ -57,6 +64,7 @@ class TestManager(unittest.TestCase):
self.path = os.path.dirname(os.path.abspath(__file__)) self.path = os.path.dirname(os.path.abspath(__file__))
self.program = os.path.join(self.path, 'simulate_program.py') self.program = os.path.join(self.path, 'simulate_program.py')
self.badprogram = os.path.join(self.path, 'test.cfg') self.badprogram = os.path.join(self.path, 'test.cfg')
signal.signal(signal.SIGALRM, handler)
def test_load_config(self): def test_load_config(self):
config = configparser.ConfigParser() config = configparser.ConfigParser()
@ -407,77 +415,133 @@ class TestManager(unittest.TestCase):
config.flush() config.flush()
Manager.main(['--conf=' + config.name]) Manager.main(['--conf=' + config.name])
# def test_run_manager_command(self): def test_run_manager_command(self):
# # Doesn't work (we need enable --concurrency=multiprocessing on coverage command but we need coverage 4.0) # Enable timeout
# logsize = 10 signal.alarm(10)
# bufsize = 10 class MockServerHttp:
# queueIn = multiprocessing.Queue() def append(self, name, queueIn, queueOut, event):
# queueOut = multiprocessing.Queue() pass
# event = multiprocessing.Event() def terminate(self):
# threadCommand = multiprocessing.Process(target=self._runCommand, pass
# args=('test_run_manager_command', def join(self):
# self.program, pass
# self.path, config = configparser.ConfigParser()
# logsize, config.add_section('config:server')
# bufsize, config.add_section('command:test')
# queueIn, config.set('command:test', 'command', self.program)
# queueOut,
# event)) manage = Manager.Manager(False)
# threadCommand.start() manage.serverHttp = MockServerHttp()
# event.set() manage._load_config(config)
# queueIn.put("START") manage.launch_command()
# item = queueOut.get(timeout=4)
# self.assertEqual(item, "started", 'Error impossible to start program') queueIn = manage.threadCommand[0]._args[5]
# time.sleep(1) queueOut = manage.threadCommand[0]._args[6]
# event.set() event = manage.threadCommand[0]._args[7]
# queueIn.put("STATUS")
# item = queueOut.get(timeout=4) queueIn.put("START")
# self.assertEqual(item, "started", 'Error impossible to start program') event.set()
# time.sleep(1) # Enable timeout
# print("-" * 80, "shutdown" ) signal.alarm(10)
# event.set() item = queueOut.get(timeout=4)
# queueIn.put("SHUTDOWN") self.assertEqual(item, "started", 'Error impossible to start program')
# with self.assertRaises(queue.Empty): signal.alarm(0)
# item = queueOut.get(timeout=4) time.sleep(1)
# print("-" * 80, "wait thread" ) signal.alarm(10)
# threadCommand.join() event.set()
# self.assertTrue(True) queueIn.put("STATUS")
# item = queueOut.get(timeout=4)
# def test_run_manager_command_2(self): self.assertEqual(item, "started", 'Error impossible to read status')
# manage = Manager.Manager(True) time.sleep(1)
# logsize = 10 event.set()
# bufsize = 10 queueIn.put("STDIN arg")
# queueIn = multiprocessing.Queue() item = queueOut.get(timeout=4)
# queueOut = multiprocessing.Queue() self.assertEqual(item, "ok", 'Error when send STDIN')
# event = multiprocessing.Event() signal.alarm(0)
# manage.runCommand('test_run_manager_command', time.sleep(1)
# self.program, signal.alarm(10)
# self.path, event.set()
# logsize, queueIn.put("STDOUT 4")
# bufsize, item = queueOut.get(timeout=4)
# queueIn, signal.alarm(0)
# queueOut, self.assertRegex(item,
# event) '^[{](.*)("first-line": 4)(.*)[}]$',
# event.set() 'Error when read STDOUT (Missing first-line)')
# queueIn.put("START") self.assertRegex(item,
# item = queueOut.get(timeout=4) '^[{](.*)("last-line": 4)(.*)[}]$',
# self.assertEqual(item, "started", 'Error impossible to start program') 'Error when read STDOUT (Missing last-line)')
# time.sleep(1) self.assertRegex(item,
# event.set() '^[{](.*)(4 arg")(.*)[}]$',
# queueIn.put("STATUS") 'Error when read STDOUT (bad record)')
# item = queueOut.get(timeout=4) time.sleep(1)
# self.assertEqual(item, "started", 'Error impossible to start program') signal.alarm(10)
# time.sleep(1) event.set()
# print("-" * 80, "shutdown" ) queueIn.put("BADCOMMAND")
# event.set() item = queueOut.get(timeout=4)
# queueIn.put("SHUTDOWN") self.assertEqual(item, "error : command unknown", 'Error impossible to read status')
# with self.assertRaises(queue.Empty): signal.alarm(0)
# item = queueOut.get(timeout=4) time.sleep(1)
# print("-" * 80, "wait thread" ) signal.alarm(10)
# #threadCommand.join() event.set()
# manage.receive_signal(15, 1) queueIn.put("STOP")
# manage.wait_children_commands() item = queueOut.get(timeout=4)
# self.assertTrue(True) self.assertEqual(item, "stopped", 'Error impossible to read status')
signal.alarm(0)
time.sleep(1)
signal.alarm(10)
event.set()
queueIn.put("SHUTDOWN")
with self.assertRaises(queue.Empty):
item = queueOut.get(timeout=4)
#threadCommand.join()
manage.receive_signal(15, 1)
manage.wait_children_commands()
#Disable timeout
signal.alarm(0)
self.assertTrue(True)
def test_run_manager_command_autostart(self):
# Enable timeout
signal.alarm(10)
class MockServerHttp:
def append(self, name, queueIn, queueOut, event):
pass
def terminate(self):
pass
def join(self):
pass
config = configparser.ConfigParser()
config.add_section('config:server')
config.add_section('command:test')
config.set('command:test', 'command', self.program)
manage = Manager.Manager(True)
manage.serverHttp = MockServerHttp()
manage._load_config(config)
manage.launch_command()
queueIn = manage.threadCommand[0]._args[5]
queueOut = manage.threadCommand[0]._args[6]
event = manage.threadCommand[0]._args[7]
signal.alarm(10)
event.set()
queueIn.put("STATUS")
item = queueOut.get(timeout=4)
self.assertEqual(item, "started", 'Error impossible to read status')
time.sleep(1)
signal.alarm(10)
event.set()
queueIn.put("SHUTDOWN")
with self.assertRaises(queue.Empty):
item = queueOut.get(timeout=4)
#threadCommand.join()
manage.receive_signal(15, 1)
manage.wait_children_commands()
#Disable timeout
signal.alarm(0)
self.assertTrue(True)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()