2008-05-25 10:05:15 -03:00
|
|
|
import _dummy_thread as _thread
|
2002-12-30 18:30:22 -04:00
|
|
|
import time
|
2008-05-11 16:59:59 -03:00
|
|
|
import queue
|
2002-12-30 18:30:22 -04:00
|
|
|
import random
|
|
|
|
import unittest
|
2008-05-20 18:35:26 -03:00
|
|
|
from test import support
|
2016-09-08 06:46:22 -03:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
DELAY = 0
|
2002-12-30 18:30:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
class LockTests(unittest.TestCase):
|
|
|
|
"""Test lock objects."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# Create a lock
|
|
|
|
self.lock = _thread.allocate_lock()
|
|
|
|
|
|
|
|
def test_initlock(self):
|
|
|
|
#Make sure locks start locked
|
2016-03-14 05:28:59 -03:00
|
|
|
self.assertFalse(self.lock.locked(),
|
2002-12-30 18:30:22 -04:00
|
|
|
"Lock object is not initialized unlocked.")
|
|
|
|
|
|
|
|
def test_release(self):
|
|
|
|
# Test self.lock.release()
|
|
|
|
self.lock.acquire()
|
|
|
|
self.lock.release()
|
2016-03-14 05:28:59 -03:00
|
|
|
self.assertFalse(self.lock.locked(),
|
2002-12-30 18:30:22 -04:00
|
|
|
"Lock object did not release properly.")
|
2003-02-18 22:35:07 -04:00
|
|
|
|
2016-09-08 06:46:22 -03:00
|
|
|
def test_LockType_context_manager(self):
|
|
|
|
with _thread.LockType():
|
|
|
|
pass
|
|
|
|
self.assertFalse(self.lock.locked(),
|
|
|
|
"Acquired Lock was not released")
|
|
|
|
|
2002-12-30 18:30:22 -04:00
|
|
|
def test_improper_release(self):
|
2011-03-01 19:05:42 -04:00
|
|
|
#Make sure release of an unlocked thread raises RuntimeError
|
|
|
|
self.assertRaises(RuntimeError, self.lock.release)
|
2002-12-30 18:30:22 -04:00
|
|
|
|
|
|
|
def test_cond_acquire_success(self):
|
|
|
|
#Make sure the conditional acquiring of the lock works.
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(self.lock.acquire(0),
|
2002-12-30 18:30:22 -04:00
|
|
|
"Conditional acquiring of the lock failed.")
|
|
|
|
|
|
|
|
def test_cond_acquire_fail(self):
|
|
|
|
#Test acquiring locked lock returns False
|
|
|
|
self.lock.acquire(0)
|
2016-03-14 05:28:59 -03:00
|
|
|
self.assertFalse(self.lock.acquire(0),
|
2002-12-30 18:30:22 -04:00
|
|
|
"Conditional acquiring of a locked lock incorrectly "
|
|
|
|
"succeeded.")
|
|
|
|
|
|
|
|
def test_uncond_acquire_success(self):
|
|
|
|
#Make sure unconditional acquiring of a lock works.
|
|
|
|
self.lock.acquire()
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(self.lock.locked(),
|
2002-12-30 18:30:22 -04:00
|
|
|
"Uncondional locking failed.")
|
|
|
|
|
|
|
|
def test_uncond_acquire_return_val(self):
|
|
|
|
#Make sure that an unconditional locking returns True.
|
2016-03-14 05:28:59 -03:00
|
|
|
self.assertIs(self.lock.acquire(1), True,
|
2002-12-30 18:30:22 -04:00
|
|
|
"Unconditional locking did not return True.")
|
2016-03-14 05:28:59 -03:00
|
|
|
self.assertIs(self.lock.acquire(), True)
|
2003-02-18 22:35:07 -04:00
|
|
|
|
2002-12-30 18:30:22 -04:00
|
|
|
def test_uncond_acquire_blocking(self):
|
|
|
|
#Make sure that unconditional acquiring of a locked lock blocks.
|
|
|
|
def delay_unlock(to_unlock, delay):
|
|
|
|
"""Hold on to lock for a set amount of time before unlocking."""
|
|
|
|
time.sleep(delay)
|
|
|
|
to_unlock.release()
|
|
|
|
|
|
|
|
self.lock.acquire()
|
|
|
|
start_time = int(time.time())
|
2003-04-30 00:03:37 -03:00
|
|
|
_thread.start_new_thread(delay_unlock,(self.lock, DELAY))
|
2008-05-20 18:35:26 -03:00
|
|
|
if support.verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print()
|
|
|
|
print("*** Waiting for thread to release the lock "\
|
|
|
|
"(approx. %s sec.) ***" % DELAY)
|
2002-12-30 18:30:22 -04:00
|
|
|
self.lock.acquire()
|
|
|
|
end_time = int(time.time())
|
2008-05-20 18:35:26 -03:00
|
|
|
if support.verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print("done")
|
2016-03-14 05:28:59 -03:00
|
|
|
self.assertGreaterEqual(end_time - start_time, DELAY,
|
2002-12-30 18:30:22 -04:00
|
|
|
"Blocking by unconditional acquiring failed.")
|
|
|
|
|
2016-09-08 06:46:22 -03:00
|
|
|
@mock.patch('time.sleep')
|
|
|
|
def test_acquire_timeout(self, mock_sleep):
|
|
|
|
"""Test invoking acquire() with a positive timeout when the lock is
|
|
|
|
already acquired. Ensure that time.sleep() is invoked with the given
|
|
|
|
timeout and that False is returned."""
|
|
|
|
|
|
|
|
self.lock.acquire()
|
|
|
|
retval = self.lock.acquire(waitflag=0, timeout=1)
|
|
|
|
self.assertTrue(mock_sleep.called)
|
|
|
|
mock_sleep.assert_called_once_with(1)
|
|
|
|
self.assertEqual(retval, False)
|
|
|
|
|
|
|
|
def test_lock_representation(self):
|
|
|
|
self.lock.acquire()
|
|
|
|
self.assertIn("locked", repr(self.lock))
|
|
|
|
self.lock.release()
|
|
|
|
self.assertIn("unlocked", repr(self.lock))
|
|
|
|
|
|
|
|
|
2002-12-30 18:30:22 -04:00
|
|
|
class MiscTests(unittest.TestCase):
|
|
|
|
"""Miscellaneous tests."""
|
|
|
|
|
|
|
|
def test_exit(self):
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertRaises(SystemExit, _thread.exit)
|
2002-12-30 18:30:22 -04:00
|
|
|
|
|
|
|
def test_ident(self):
|
2010-01-24 15:26:24 -04:00
|
|
|
self.assertIsInstance(_thread.get_ident(), int,
|
|
|
|
"_thread.get_ident() returned a non-integer")
|
2017-03-23 10:48:39 -03:00
|
|
|
self.assertGreater(_thread.get_ident(), 0)
|
2002-12-30 18:30:22 -04:00
|
|
|
|
|
|
|
def test_LockType(self):
|
2010-01-24 15:26:24 -04:00
|
|
|
self.assertIsInstance(_thread.allocate_lock(), _thread.LockType,
|
|
|
|
"_thread.LockType is not an instance of what "
|
|
|
|
"is returned by _thread.allocate_lock()")
|
2002-12-30 18:30:22 -04:00
|
|
|
|
2016-09-08 06:46:22 -03:00
|
|
|
def test_set_sentinel(self):
|
|
|
|
self.assertIsInstance(_thread._set_sentinel(), _thread.LockType,
|
|
|
|
"_thread._set_sentinel() did not return a "
|
|
|
|
"LockType instance.")
|
|
|
|
|
2003-06-13 20:44:35 -03:00
|
|
|
def test_interrupt_main(self):
|
|
|
|
#Calling start_new_thread with a function that executes interrupt_main
|
|
|
|
# should raise KeyboardInterrupt upon completion.
|
|
|
|
def call_interrupt():
|
|
|
|
_thread.interrupt_main()
|
2016-09-08 06:46:22 -03:00
|
|
|
|
|
|
|
self.assertRaises(KeyboardInterrupt,
|
|
|
|
_thread.start_new_thread,
|
|
|
|
call_interrupt,
|
|
|
|
tuple())
|
2003-06-15 20:26:30 -03:00
|
|
|
|
2003-06-13 20:56:32 -03:00
|
|
|
def test_interrupt_in_main(self):
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertRaises(KeyboardInterrupt, _thread.interrupt_main)
|
2003-06-13 20:44:35 -03:00
|
|
|
|
2016-09-08 06:46:22 -03:00
|
|
|
def test_stack_size_None(self):
|
|
|
|
retval = _thread.stack_size(None)
|
|
|
|
self.assertEqual(retval, 0)
|
|
|
|
|
|
|
|
def test_stack_size_not_None(self):
|
|
|
|
with self.assertRaises(_thread.error) as cm:
|
|
|
|
_thread.stack_size("")
|
|
|
|
self.assertEqual(cm.exception.args[0],
|
|
|
|
"setting thread stack size not supported")
|
|
|
|
|
|
|
|
|
2002-12-30 18:30:22 -04:00
|
|
|
class ThreadTests(unittest.TestCase):
|
|
|
|
"""Test thread creation."""
|
|
|
|
|
|
|
|
def test_arg_passing(self):
|
|
|
|
#Make sure that parameter passing works.
|
|
|
|
def arg_tester(queue, arg1=False, arg2=False):
|
|
|
|
"""Use to test _thread.start_new_thread() passes args properly."""
|
|
|
|
queue.put((arg1, arg2))
|
|
|
|
|
2008-05-11 16:59:59 -03:00
|
|
|
testing_queue = queue.Queue(1)
|
2002-12-30 18:30:22 -04:00
|
|
|
_thread.start_new_thread(arg_tester, (testing_queue, True, True))
|
|
|
|
result = testing_queue.get()
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(result[0] and result[1],
|
2016-09-08 06:46:22 -03:00
|
|
|
"Argument passing for thread creation "
|
|
|
|
"using tuple failed")
|
|
|
|
|
|
|
|
_thread.start_new_thread(
|
|
|
|
arg_tester,
|
|
|
|
tuple(),
|
|
|
|
{'queue':testing_queue, 'arg1':True, 'arg2':True})
|
|
|
|
|
2002-12-30 18:30:22 -04:00
|
|
|
result = testing_queue.get()
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(result[0] and result[1],
|
2016-09-08 06:46:22 -03:00
|
|
|
"Argument passing for thread creation "
|
|
|
|
"using kwargs failed")
|
|
|
|
|
|
|
|
_thread.start_new_thread(
|
|
|
|
arg_tester,
|
|
|
|
(testing_queue, True),
|
|
|
|
{'arg2':True})
|
|
|
|
|
2002-12-30 18:30:22 -04:00
|
|
|
result = testing_queue.get()
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(result[0] and result[1],
|
2002-12-30 18:30:22 -04:00
|
|
|
"Argument passing for thread creation using both tuple"
|
|
|
|
" and kwargs failed")
|
2003-02-18 22:35:07 -04:00
|
|
|
|
2016-09-08 06:46:22 -03:00
|
|
|
def test_multi_thread_creation(self):
|
2002-12-30 18:30:22 -04:00
|
|
|
def queue_mark(queue, delay):
|
|
|
|
time.sleep(delay)
|
|
|
|
queue.put(_thread.get_ident())
|
2003-02-18 22:35:07 -04:00
|
|
|
|
2002-12-30 18:30:22 -04:00
|
|
|
thread_count = 5
|
2008-05-11 16:59:59 -03:00
|
|
|
testing_queue = queue.Queue(thread_count)
|
2016-09-08 06:46:22 -03:00
|
|
|
|
2008-05-20 18:35:26 -03:00
|
|
|
if support.verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print()
|
2016-09-08 06:46:22 -03:00
|
|
|
print("*** Testing multiple thread creation "
|
|
|
|
"(will take approx. %s to %s sec.) ***" % (
|
|
|
|
DELAY, thread_count))
|
|
|
|
|
2007-05-07 19:24:25 -03:00
|
|
|
for count in range(thread_count):
|
2003-04-30 00:03:37 -03:00
|
|
|
if DELAY:
|
|
|
|
local_delay = round(random.random(), 1)
|
|
|
|
else:
|
|
|
|
local_delay = 0
|
2002-12-30 18:30:22 -04:00
|
|
|
_thread.start_new_thread(queue_mark,
|
2003-04-30 00:03:37 -03:00
|
|
|
(testing_queue, local_delay))
|
|
|
|
time.sleep(DELAY)
|
2008-05-20 18:35:26 -03:00
|
|
|
if support.verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('done')
|
2016-03-14 05:28:59 -03:00
|
|
|
self.assertEqual(testing_queue.qsize(), thread_count,
|
2016-09-08 06:46:22 -03:00
|
|
|
"Not all %s threads executed properly "
|
|
|
|
"after %s sec." % (thread_count, DELAY))
|
|
|
|
|
|
|
|
def test_args_not_tuple(self):
|
|
|
|
"""
|
|
|
|
Test invoking start_new_thread() with a non-tuple value for "args".
|
|
|
|
Expect TypeError with a meaningful error message to be raised.
|
|
|
|
"""
|
|
|
|
with self.assertRaises(TypeError) as cm:
|
|
|
|
_thread.start_new_thread(mock.Mock(), [])
|
|
|
|
self.assertEqual(cm.exception.args[0], "2nd arg must be a tuple")
|
|
|
|
|
|
|
|
def test_kwargs_not_dict(self):
|
|
|
|
"""
|
|
|
|
Test invoking start_new_thread() with a non-dict value for "kwargs".
|
|
|
|
Expect TypeError with a meaningful error message to be raised.
|
|
|
|
"""
|
|
|
|
with self.assertRaises(TypeError) as cm:
|
|
|
|
_thread.start_new_thread(mock.Mock(), tuple(), kwargs=[])
|
|
|
|
self.assertEqual(cm.exception.args[0], "3rd arg must be a dict")
|
|
|
|
|
|
|
|
def test_SystemExit(self):
|
|
|
|
"""
|
|
|
|
Test invoking start_new_thread() with a function that raises
|
|
|
|
SystemExit.
|
|
|
|
The exception should be discarded.
|
|
|
|
"""
|
|
|
|
func = mock.Mock(side_effect=SystemExit())
|
|
|
|
try:
|
|
|
|
_thread.start_new_thread(func, tuple())
|
|
|
|
except SystemExit:
|
|
|
|
self.fail("start_new_thread raised SystemExit.")
|
|
|
|
|
|
|
|
@mock.patch('traceback.print_exc')
|
|
|
|
def test_RaiseException(self, mock_print_exc):
|
|
|
|
"""
|
|
|
|
Test invoking start_new_thread() with a function that raises exception.
|
|
|
|
|
|
|
|
The exception should be discarded and the traceback should be printed
|
|
|
|
via traceback.print_exc()
|
|
|
|
"""
|
|
|
|
func = mock.Mock(side_effect=Exception)
|
|
|
|
_thread.start_new_thread(func, tuple())
|
|
|
|
self.assertTrue(mock_print_exc.called)
|