salt/tests/unit/utils/test_process.py

165 lines
5.7 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 sys
2014-12-12 16:04:02 +00:00
import time
import signal
import multiprocessing
# Import Salt Testing libs
from tests.support.unit import TestCase, skipIf
2014-12-12 16:04:02 +00:00
# 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
2016-03-28 18:37:52 +00:00
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.SIGKILL)
process_manager.stop_restarting()
process_manager.kill_children()
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
os.kill(initial_pid, signal.SIGKILL)
2014-12-12 16:04:02 +00:00
# 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.SIGKILL)
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
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.SIGKILL)
process_manager.stop_restarting()
process_manager.kill_children()
2014-12-12 16:04:02 +00:00
2016-10-31 10:21:43 +00:00
@skipIf(sys.version_info < (2, 7), 'Needs > Py 2.7 due to bug in stdlib')
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.SIGKILL)
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)