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 @@
##############################################################################
from cloudooo.monitor.monitor import Monitor
from threading import Thread
from psutil import Process
from multiprocessing import Process
import psutil
from cloudooo.utils import logger
from time import sleep
class MonitorMemory(Monitor, Thread):
class MonitorMemory(Monitor, Process):
"""Usefull to control the memory and does not allow use it unnecessarily"""
def __init__(self, openoffice, interval, limit_memory_usage):
......@@ -40,11 +40,11 @@ class MonitorMemory(Monitor, Thread):
and ILockable, the limit of memory usage that the openoffice can use and the
interval to check the object."""
Monitor.__init__(self, openoffice, interval)
Thread.__init__(self)
Process.__init__(self)
self.limit = limit_memory_usage
def create_process(self):
self.process = Process(int(self.openoffice.pid()))
self.process = psutil.Process(int(self.openoffice.pid()))
def get_memory_usage(self):
try:
......@@ -55,7 +55,7 @@ class MonitorMemory(Monitor, Thread):
except TypeError:
logger.debug("OpenOffice is stopped")
return 0
# convert bytes to GB
# convert bytes to MB
return sum(self.process.get_memory_info())/(1024*1024)
def run(self):
......@@ -71,3 +71,7 @@ class MonitorMemory(Monitor, Thread):
self.openoffice.stop()
sleep(self.interval)
logger.debug("Stop MonitorMemory")
def terminate(self):
Monitor.terminate(self)
Process.terminate(self)
......@@ -54,18 +54,20 @@ class TestMonitorInit(cloudoooTestCase):
def testMonitorLoadOnlyMonitorRequest(self):
"""Check if the monitors are started"""
cloudooo.monitor.load(self.load_config)
self.assertEquals(isinstance(cloudooo.monitor.monitor_request, MonitorRequest),
True)
self.assertEquals(cloudooo.monitor.monitor_memory, None)
self.assertEquals(isinstance(cloudooo.monitor.monitor_request,
MonitorRequest),
True)
def testMonitorLoadMonitorMemory(self):
"""Check if the MemoryMemory is started"""
self.load_config['enable_memory_monitor'] = True
cloudooo.monitor.load(self.load_config)
self.assertEquals(isinstance(cloudooo.monitor.monitor_request, MonitorRequest),
True)
self.assertEquals(isinstance(cloudooo.monitor.monitor_memory, MonitorMemory),
True)
self.assertEquals(isinstance(cloudooo.monitor.monitor_request,
MonitorRequest),
True)
self.assertEquals(isinstance(cloudooo.monitor.monitor_memory,
MonitorMemory),
True)
def test_suite():
return make_suite(TestMonitorInit)
......
......@@ -38,6 +38,8 @@ class TestMonitorMemory(unittest.TestCase):
"""Test case to see if the MonitorMemory is properly managing the
openoffice."""
interval = 3
def setUp(self):
if not openoffice.status():
openoffice.start()
......@@ -56,7 +58,7 @@ class TestMonitorMemory(unittest.TestCase):
try:
self.monitor = MonitorMemory(openoffice, 1, 1000)
self.monitor.start()
sleep(2)
sleep(6)
self.assertEquals(openoffice.status(), True)
finally:
self.monitor.terminate()
......@@ -67,7 +69,7 @@ class TestMonitorMemory(unittest.TestCase):
try:
self.monitor = MonitorMemory(openoffice, 2, 10)
self.monitor.start()
sleep(3)
sleep(self.interval)
self.assertEquals(openoffice.status(), False)
finally:
self.monitor.terminate()
......@@ -76,9 +78,9 @@ class TestMonitorMemory(unittest.TestCase):
"""Tests if the monitor continues to run even with openoffice stopped"""
openoffice.stop()
self.monitor = MonitorMemory(openoffice, 2, 1000)
self.monitor.start()
try:
self.monitor.start()
sleep(3)
sleep(self.interval)
self.assertEquals(self.monitor.is_alive(), True)
finally:
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