salt/tests/unit/utils/process_test.py

183 lines
6.3 KiB
Python
Raw Normal View History

2014-12-12 16:04:02 +00:00
# -*- coding: utf-8 -*-
# Import python libs
from __future__ import absolute_import
2014-12-12 16:04:02 +00:00
import os
import time
import signal
import platform
2014-12-12 16:04:02 +00:00
import multiprocessing
# Import Salt Testing libs
from salttesting import TestCase, skipIf
2014-12-12 16:04:02 +00:00
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import salt libs
import salt.utils
2014-12-12 16:04:02 +00:00
import salt.utils.process
2014-11-21 20:17:01 +00:00
# Import 3rd-party libs
import salt.ext.six as six
from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
IS_UBUNTU = False
if 'Ubuntu' and '14.04' in platform.dist():
IS_UBUNTU = True
2014-12-12 16:04:02 +00:00
class TestProcessManager(TestCase):
def test_basic(self):
'''
Make sure that the process is alive 2s later
'''
def spin():
salt.utils.appendproctitle('test_basic')
2014-12-12 16:04:02 +00:00
while True:
time.sleep(1)
process_manager = salt.utils.process.ProcessManager()
process_manager.add_process(spin)
2014-11-21 20:17:01 +00:00
initial_pid = next(six.iterkeys(process_manager._process_map))
2014-12-12 16:04:02 +00:00
time.sleep(2)
process_manager.check_children()
try:
assert initial_pid == next(six.iterkeys(process_manager._process_map))
finally:
process_manager.stop_restarting()
process_manager.kill_children()
time.sleep(0.5)
# Are there child processes still running?
if process_manager._process_map.keys():
process_manager.send_signal_to_processes(signal.SIGILL)
process_manager.stop_restarting()
process_manager.kill_children()
2014-12-12 16:04:02 +00:00
# On Ubuntu 14.04 the os.kill command does not get run, which causes this test to fail
# when the whole test suite is run.
# This is not due to a salt.utils.process code problem.
@skipIf(IS_UBUNTU, 'Skipping on Ubuntu due to os.kill not being run on Ubuntu 14.04')
2014-12-12 16:04:02 +00:00
def test_kill(self):
def spin():
salt.utils.appendproctitle('test_kill')
2014-12-12 16:04:02 +00:00
while True:
time.sleep(1)
process_manager = salt.utils.process.ProcessManager()
process_manager.add_process(spin)
2014-11-21 20:17:01 +00:00
initial_pid = next(six.iterkeys(process_manager._process_map))
2014-12-12 16:04:02 +00:00
# kill the child
time.sleep(1)
2014-12-12 16:04:02 +00:00
os.kill(initial_pid, signal.SIGTERM)
# give the OS time to give the signal...
time.sleep(0.1)
process_manager.check_children()
try:
assert initial_pid != next(six.iterkeys(process_manager._process_map))
finally:
process_manager.stop_restarting()
process_manager.kill_children()
time.sleep(0.5)
# Are there child processes still running?
if process_manager._process_map.keys():
process_manager.send_signal_to_processes(signal.SIGILL)
process_manager.stop_restarting()
process_manager.kill_children()
2014-12-12 16:04:02 +00:00
def test_restarting(self):
'''
Make sure that the process is alive 2s later
'''
def die():
salt.utils.appendproctitle('test_restarting')
2014-12-12 16:04:02 +00:00
time.sleep(1)
process_manager = salt.utils.process.ProcessManager()
process_manager.add_process(die)
2014-11-21 20:17:01 +00:00
initial_pid = next(six.iterkeys(process_manager._process_map))
2014-12-12 16:04:02 +00:00
time.sleep(2)
process_manager.check_children()
try:
assert initial_pid != next(six.iterkeys(process_manager._process_map))
finally:
process_manager.stop_restarting()
process_manager.kill_children()
time.sleep(0.5)
# Are there child processes still running?
if process_manager._process_map.keys():
process_manager.send_signal_to_processes(signal.SIGILL)
process_manager.stop_restarting()
process_manager.kill_children()
2014-12-12 16:04:02 +00:00
def test_counter(self):
def incr(counter, num):
salt.utils.appendproctitle('test_counter')
2014-11-21 20:17:01 +00:00
for _ in range(0, num):
2014-12-12 16:04:02 +00:00
counter.value += 1
counter = multiprocessing.Value('i', 0)
process_manager = salt.utils.process.ProcessManager()
process_manager.add_process(incr, args=(counter, 2))
time.sleep(1)
process_manager.check_children()
time.sleep(1)
# we should have had 2 processes go at it
try:
assert counter.value == 4
finally:
process_manager.stop_restarting()
process_manager.kill_children()
time.sleep(0.5)
# Are there child processes still running?
if process_manager._process_map.keys():
process_manager.send_signal_to_processes(signal.SIGILL)
process_manager.stop_restarting()
process_manager.kill_children()
2014-12-12 16:04:02 +00:00
2014-12-12 17:42:41 +00:00
2014-12-12 16:15:34 +00:00
class TestThreadPool(TestCase):
def test_basic(self):
'''
Make sure the threadpool can do things
'''
def incr_counter(counter):
counter.value += 1
2014-12-12 17:42:41 +00:00
counter = multiprocessing.Value('i', 0)
2014-12-12 16:15:34 +00:00
pool = salt.utils.process.ThreadPool()
sent = pool.fire_async(incr_counter, args=(counter,))
self.assertTrue(sent)
time.sleep(1) # Sleep to let the threads do things
self.assertEqual(counter.value, 1)
self.assertEqual(pool._job_queue.qsize(), 0)
def test_full_queue(self):
'''
Make sure that a full threadpool acts as we expect
'''
def incr_counter(counter):
counter.value += 1
2014-12-12 17:42:41 +00:00
counter = multiprocessing.Value('i', 0)
2014-12-12 16:15:34 +00:00
# Create a pool with no workers and 1 queue size
pool = salt.utils.process.ThreadPool(0, 1)
# make sure we can put the one item in
sent = pool.fire_async(incr_counter, args=(counter,))
self.assertTrue(sent)
# make sure we can't put more in
sent = pool.fire_async(incr_counter, args=(counter,))
self.assertFalse(sent)
time.sleep(1) # Sleep to let the threads do things
# make sure no one updated the counter
self.assertEqual(counter.value, 0)
# make sure the queue is still full
self.assertEqual(pool._job_queue.qsize(), 1)
2014-12-12 16:04:02 +00:00
if __name__ == '__main__':
from integration import run_tests
run_tests(
2014-12-12 16:15:34 +00:00
[TestProcessManager, TestThreadPool],
2014-12-12 16:04:02 +00:00
needs_daemon=False
)