Commit 8727199c authored by Gabriel Monnerat's avatar Gabriel Monnerat

- Refactor to use Process instead of Thread. processes are more reliable to stop it.

- clean up the tests


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@38985 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent eae4a637
...@@ -27,12 +27,12 @@ ...@@ -27,12 +27,12 @@
############################################################################## ##############################################################################
from cloudooo.monitor.monitor import Monitor from cloudooo.monitor.monitor import Monitor
from threading import Thread from multiprocessing import Process
from psutil import Process import psutil
from cloudooo.utils import logger from cloudooo.utils import logger
from time import sleep from time import sleep
class MonitorMemory(Monitor, Thread): class MonitorMemory(Monitor, Process):
"""Usefull to control the memory and does not allow use it unnecessarily""" """Usefull to control the memory and does not allow use it unnecessarily"""
def __init__(self, openoffice, interval, limit_memory_usage): def __init__(self, openoffice, interval, limit_memory_usage):
...@@ -40,11 +40,11 @@ class MonitorMemory(Monitor, Thread): ...@@ -40,11 +40,11 @@ class MonitorMemory(Monitor, Thread):
and ILockable, the limit of memory usage that the openoffice can use and the and ILockable, the limit of memory usage that the openoffice can use and the
interval to check the object.""" interval to check the object."""
Monitor.__init__(self, openoffice, interval) Monitor.__init__(self, openoffice, interval)
Thread.__init__(self) Process.__init__(self)
self.limit = limit_memory_usage self.limit = limit_memory_usage
def create_process(self): def create_process(self):
self.process = Process(int(self.openoffice.pid())) self.process = psutil.Process(int(self.openoffice.pid()))
def get_memory_usage(self): def get_memory_usage(self):
try: try:
...@@ -55,7 +55,7 @@ class MonitorMemory(Monitor, Thread): ...@@ -55,7 +55,7 @@ class MonitorMemory(Monitor, Thread):
except TypeError: except TypeError:
logger.debug("OpenOffice is stopped") logger.debug("OpenOffice is stopped")
return 0 return 0
# convert bytes to GB # convert bytes to MB
return sum(self.process.get_memory_info())/(1024*1024) return sum(self.process.get_memory_info())/(1024*1024)
def run(self): def run(self):
...@@ -71,3 +71,7 @@ class MonitorMemory(Monitor, Thread): ...@@ -71,3 +71,7 @@ class MonitorMemory(Monitor, Thread):
self.openoffice.stop() self.openoffice.stop()
sleep(self.interval) sleep(self.interval)
logger.debug("Stop MonitorMemory") logger.debug("Stop MonitorMemory")
def terminate(self):
Monitor.terminate(self)
Process.terminate(self)
...@@ -54,17 +54,19 @@ class TestMonitorInit(cloudoooTestCase): ...@@ -54,17 +54,19 @@ class TestMonitorInit(cloudoooTestCase):
def testMonitorLoadOnlyMonitorRequest(self): def testMonitorLoadOnlyMonitorRequest(self):
"""Check if the monitors are started""" """Check if the monitors are started"""
cloudooo.monitor.load(self.load_config) cloudooo.monitor.load(self.load_config)
self.assertEquals(isinstance(cloudooo.monitor.monitor_request, MonitorRequest), self.assertEquals(isinstance(cloudooo.monitor.monitor_request,
MonitorRequest),
True) True)
self.assertEquals(cloudooo.monitor.monitor_memory, None)
def testMonitorLoadMonitorMemory(self): def testMonitorLoadMonitorMemory(self):
"""Check if the MemoryMemory is started""" """Check if the MemoryMemory is started"""
self.load_config['enable_memory_monitor'] = True self.load_config['enable_memory_monitor'] = True
cloudooo.monitor.load(self.load_config) cloudooo.monitor.load(self.load_config)
self.assertEquals(isinstance(cloudooo.monitor.monitor_request, MonitorRequest), self.assertEquals(isinstance(cloudooo.monitor.monitor_request,
MonitorRequest),
True) True)
self.assertEquals(isinstance(cloudooo.monitor.monitor_memory, MonitorMemory), self.assertEquals(isinstance(cloudooo.monitor.monitor_memory,
MonitorMemory),
True) True)
def test_suite(): def test_suite():
......
...@@ -38,6 +38,8 @@ class TestMonitorMemory(unittest.TestCase): ...@@ -38,6 +38,8 @@ class TestMonitorMemory(unittest.TestCase):
"""Test case to see if the MonitorMemory is properly managing the """Test case to see if the MonitorMemory is properly managing the
openoffice.""" openoffice."""
interval = 3
def setUp(self): def setUp(self):
if not openoffice.status(): if not openoffice.status():
openoffice.start() openoffice.start()
...@@ -56,7 +58,7 @@ class TestMonitorMemory(unittest.TestCase): ...@@ -56,7 +58,7 @@ class TestMonitorMemory(unittest.TestCase):
try: try:
self.monitor = MonitorMemory(openoffice, 1, 1000) self.monitor = MonitorMemory(openoffice, 1, 1000)
self.monitor.start() self.monitor.start()
sleep(2) sleep(6)
self.assertEquals(openoffice.status(), True) self.assertEquals(openoffice.status(), True)
finally: finally:
self.monitor.terminate() self.monitor.terminate()
...@@ -67,7 +69,7 @@ class TestMonitorMemory(unittest.TestCase): ...@@ -67,7 +69,7 @@ class TestMonitorMemory(unittest.TestCase):
try: try:
self.monitor = MonitorMemory(openoffice, 2, 10) self.monitor = MonitorMemory(openoffice, 2, 10)
self.monitor.start() self.monitor.start()
sleep(3) sleep(self.interval)
self.assertEquals(openoffice.status(), False) self.assertEquals(openoffice.status(), False)
finally: finally:
self.monitor.terminate() self.monitor.terminate()
...@@ -76,9 +78,9 @@ class TestMonitorMemory(unittest.TestCase): ...@@ -76,9 +78,9 @@ class TestMonitorMemory(unittest.TestCase):
"""Tests if the monitor continues to run even with openoffice stopped""" """Tests if the monitor continues to run even with openoffice stopped"""
openoffice.stop() openoffice.stop()
self.monitor = MonitorMemory(openoffice, 2, 1000) self.monitor = MonitorMemory(openoffice, 2, 1000)
try:
self.monitor.start() self.monitor.start()
sleep(3) try:
sleep(self.interval)
self.assertEquals(self.monitor.is_alive(), True) self.assertEquals(self.monitor.is_alive(), True)
finally: finally:
self.monitor.terminate() self.monitor.terminate()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment