You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_threadpool.py 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.python.threadpool}
  5. """
  6. from __future__ import division, absolute_import
  7. import pickle
  8. import time
  9. import weakref
  10. import gc
  11. import threading
  12. from twisted.python.compat import range
  13. from twisted.trial import unittest
  14. from twisted.python import threadpool, threadable, failure, context
  15. from twisted._threads import Team, createMemoryWorker
  16. class Synchronization(object):
  17. failures = 0
  18. def __init__(self, N, waiting):
  19. self.N = N
  20. self.waiting = waiting
  21. self.lock = threading.Lock()
  22. self.runs = []
  23. def run(self):
  24. # This is the testy part: this is supposed to be invoked
  25. # serially from multiple threads. If that is actually the
  26. # case, we will never fail to acquire this lock. If it is
  27. # *not* the case, we might get here while someone else is
  28. # holding the lock.
  29. if self.lock.acquire(False):
  30. if not len(self.runs) % 5:
  31. # Constant selected based on empirical data to maximize the
  32. # chance of a quick failure if this code is broken.
  33. time.sleep(0.0002)
  34. self.lock.release()
  35. else:
  36. self.failures += 1
  37. # This is just the only way I can think of to wake up the test
  38. # method. It doesn't actually have anything to do with the
  39. # test.
  40. self.lock.acquire()
  41. self.runs.append(None)
  42. if len(self.runs) == self.N:
  43. self.waiting.release()
  44. self.lock.release()
  45. synchronized = ["run"]
  46. threadable.synchronize(Synchronization)
  47. class ThreadPoolTests(unittest.SynchronousTestCase):
  48. """
  49. Test threadpools.
  50. """
  51. def getTimeout(self):
  52. """
  53. Return number of seconds to wait before giving up.
  54. """
  55. return 5 # Really should be order of magnitude less
  56. def _waitForLock(self, lock):
  57. items = range(1000000)
  58. for i in items:
  59. if lock.acquire(False):
  60. break
  61. time.sleep(1e-5)
  62. else:
  63. self.fail("A long time passed without succeeding")
  64. def test_attributes(self):
  65. """
  66. L{ThreadPool.min} and L{ThreadPool.max} are set to the values passed to
  67. L{ThreadPool.__init__}.
  68. """
  69. pool = threadpool.ThreadPool(12, 22)
  70. self.assertEqual(pool.min, 12)
  71. self.assertEqual(pool.max, 22)
  72. def test_start(self):
  73. """
  74. L{ThreadPool.start} creates the minimum number of threads specified.
  75. """
  76. pool = threadpool.ThreadPool(0, 5)
  77. pool.start()
  78. self.addCleanup(pool.stop)
  79. self.assertEqual(len(pool.threads), 0)
  80. pool = threadpool.ThreadPool(3, 10)
  81. self.assertEqual(len(pool.threads), 0)
  82. pool.start()
  83. self.addCleanup(pool.stop)
  84. self.assertEqual(len(pool.threads), 3)
  85. def test_adjustingWhenPoolStopped(self):
  86. """
  87. L{ThreadPool.adjustPoolsize} only modifies the pool size and does not
  88. start new workers while the pool is not running.
  89. """
  90. pool = threadpool.ThreadPool(0, 5)
  91. pool.start()
  92. pool.stop()
  93. pool.adjustPoolsize(2)
  94. self.assertEqual(len(pool.threads), 0)
  95. def test_threadCreationArguments(self):
  96. """
  97. Test that creating threads in the threadpool with application-level
  98. objects as arguments doesn't results in those objects never being
  99. freed, with the thread maintaining a reference to them as long as it
  100. exists.
  101. """
  102. tp = threadpool.ThreadPool(0, 1)
  103. tp.start()
  104. self.addCleanup(tp.stop)
  105. # Sanity check - no threads should have been started yet.
  106. self.assertEqual(tp.threads, [])
  107. # Here's our function
  108. def worker(arg):
  109. pass
  110. # weakref needs an object subclass
  111. class Dumb(object):
  112. pass
  113. # And here's the unique object
  114. unique = Dumb()
  115. workerRef = weakref.ref(worker)
  116. uniqueRef = weakref.ref(unique)
  117. # Put some work in
  118. tp.callInThread(worker, unique)
  119. # Add an event to wait completion
  120. event = threading.Event()
  121. tp.callInThread(event.set)
  122. event.wait(self.getTimeout())
  123. del worker
  124. del unique
  125. gc.collect()
  126. self.assertIsNone(uniqueRef())
  127. self.assertIsNone(workerRef())
  128. def test_threadCreationArgumentsCallInThreadWithCallback(self):
  129. """
  130. As C{test_threadCreationArguments} above, but for
  131. callInThreadWithCallback.
  132. """
  133. tp = threadpool.ThreadPool(0, 1)
  134. tp.start()
  135. self.addCleanup(tp.stop)
  136. # Sanity check - no threads should have been started yet.
  137. self.assertEqual(tp.threads, [])
  138. # this holds references obtained in onResult
  139. refdict = {} # name -> ref value
  140. onResultWait = threading.Event()
  141. onResultDone = threading.Event()
  142. resultRef = []
  143. # result callback
  144. def onResult(success, result):
  145. # Spin the GC, which should now delete worker and unique if it's
  146. # not held on to by callInThreadWithCallback after it is complete
  147. gc.collect()
  148. onResultWait.wait(self.getTimeout())
  149. refdict['workerRef'] = workerRef()
  150. refdict['uniqueRef'] = uniqueRef()
  151. onResultDone.set()
  152. resultRef.append(weakref.ref(result))
  153. # Here's our function
  154. def worker(arg, test):
  155. return Dumb()
  156. # weakref needs an object subclass
  157. class Dumb(object):
  158. pass
  159. # And here's the unique object
  160. unique = Dumb()
  161. onResultRef = weakref.ref(onResult)
  162. workerRef = weakref.ref(worker)
  163. uniqueRef = weakref.ref(unique)
  164. # Put some work in
  165. tp.callInThreadWithCallback(onResult, worker, unique, test=unique)
  166. del worker
  167. del unique
  168. # let onResult collect the refs
  169. onResultWait.set()
  170. # wait for onResult
  171. onResultDone.wait(self.getTimeout())
  172. gc.collect()
  173. self.assertIsNone(uniqueRef())
  174. self.assertIsNone(workerRef())
  175. # XXX There's a race right here - has onResult in the worker thread
  176. # returned and the locals in _worker holding it and the result been
  177. # deleted yet?
  178. del onResult
  179. gc.collect()
  180. self.assertIsNone(onResultRef())
  181. self.assertIsNone(resultRef[0]())
  182. # The callback shouldn't have been able to resolve the references.
  183. self.assertEqual(list(refdict.values()), [None, None])
  184. def test_persistence(self):
  185. """
  186. Threadpools can be pickled and unpickled, which should preserve the
  187. number of threads and other parameters.
  188. """
  189. pool = threadpool.ThreadPool(7, 20)
  190. self.assertEqual(pool.min, 7)
  191. self.assertEqual(pool.max, 20)
  192. # check that unpickled threadpool has same number of threads
  193. copy = pickle.loads(pickle.dumps(pool))
  194. self.assertEqual(copy.min, 7)
  195. self.assertEqual(copy.max, 20)
  196. def _threadpoolTest(self, method):
  197. """
  198. Test synchronization of calls made with C{method}, which should be
  199. one of the mechanisms of the threadpool to execute work in threads.
  200. """
  201. # This is a schizophrenic test: it seems to be trying to test
  202. # both the callInThread()/dispatch() behavior of the ThreadPool as well
  203. # as the serialization behavior of threadable.synchronize(). It
  204. # would probably make more sense as two much simpler tests.
  205. N = 10
  206. tp = threadpool.ThreadPool()
  207. tp.start()
  208. self.addCleanup(tp.stop)
  209. waiting = threading.Lock()
  210. waiting.acquire()
  211. actor = Synchronization(N, waiting)
  212. for i in range(N):
  213. method(tp, actor)
  214. self._waitForLock(waiting)
  215. self.assertFalse(
  216. actor.failures,
  217. "run() re-entered {} times".format(actor.failures))
  218. def test_callInThread(self):
  219. """
  220. Call C{_threadpoolTest} with C{callInThread}.
  221. """
  222. return self._threadpoolTest(
  223. lambda tp, actor: tp.callInThread(actor.run))
  224. def test_callInThreadException(self):
  225. """
  226. L{ThreadPool.callInThread} logs exceptions raised by the callable it
  227. is passed.
  228. """
  229. class NewError(Exception):
  230. pass
  231. def raiseError():
  232. raise NewError()
  233. tp = threadpool.ThreadPool(0, 1)
  234. tp.callInThread(raiseError)
  235. tp.start()
  236. tp.stop()
  237. errors = self.flushLoggedErrors(NewError)
  238. self.assertEqual(len(errors), 1)
  239. def test_callInThreadWithCallback(self):
  240. """
  241. L{ThreadPool.callInThreadWithCallback} calls C{onResult} with a
  242. two-tuple of C{(True, result)} where C{result} is the value returned
  243. by the callable supplied.
  244. """
  245. waiter = threading.Lock()
  246. waiter.acquire()
  247. results = []
  248. def onResult(success, result):
  249. waiter.release()
  250. results.append(success)
  251. results.append(result)
  252. tp = threadpool.ThreadPool(0, 1)
  253. tp.callInThreadWithCallback(onResult, lambda: "test")
  254. tp.start()
  255. try:
  256. self._waitForLock(waiter)
  257. finally:
  258. tp.stop()
  259. self.assertTrue(results[0])
  260. self.assertEqual(results[1], "test")
  261. def test_callInThreadWithCallbackExceptionInCallback(self):
  262. """
  263. L{ThreadPool.callInThreadWithCallback} calls C{onResult} with a
  264. two-tuple of C{(False, failure)} where C{failure} represents the
  265. exception raised by the callable supplied.
  266. """
  267. class NewError(Exception):
  268. pass
  269. def raiseError():
  270. raise NewError()
  271. waiter = threading.Lock()
  272. waiter.acquire()
  273. results = []
  274. def onResult(success, result):
  275. waiter.release()
  276. results.append(success)
  277. results.append(result)
  278. tp = threadpool.ThreadPool(0, 1)
  279. tp.callInThreadWithCallback(onResult, raiseError)
  280. tp.start()
  281. try:
  282. self._waitForLock(waiter)
  283. finally:
  284. tp.stop()
  285. self.assertFalse(results[0])
  286. self.assertIsInstance(results[1], failure.Failure)
  287. self.assertTrue(issubclass(results[1].type, NewError))
  288. def test_callInThreadWithCallbackExceptionInOnResult(self):
  289. """
  290. L{ThreadPool.callInThreadWithCallback} logs the exception raised by
  291. C{onResult}.
  292. """
  293. class NewError(Exception):
  294. pass
  295. waiter = threading.Lock()
  296. waiter.acquire()
  297. results = []
  298. def onResult(success, result):
  299. results.append(success)
  300. results.append(result)
  301. raise NewError()
  302. tp = threadpool.ThreadPool(0, 1)
  303. tp.callInThreadWithCallback(onResult, lambda: None)
  304. tp.callInThread(waiter.release)
  305. tp.start()
  306. try:
  307. self._waitForLock(waiter)
  308. finally:
  309. tp.stop()
  310. errors = self.flushLoggedErrors(NewError)
  311. self.assertEqual(len(errors), 1)
  312. self.assertTrue(results[0])
  313. self.assertIsNone(results[1])
  314. def test_callbackThread(self):
  315. """
  316. L{ThreadPool.callInThreadWithCallback} calls the function it is
  317. given and the C{onResult} callback in the same thread.
  318. """
  319. threadIds = []
  320. event = threading.Event()
  321. def onResult(success, result):
  322. threadIds.append(threading.currentThread().ident)
  323. event.set()
  324. def func():
  325. threadIds.append(threading.currentThread().ident)
  326. tp = threadpool.ThreadPool(0, 1)
  327. tp.callInThreadWithCallback(onResult, func)
  328. tp.start()
  329. self.addCleanup(tp.stop)
  330. event.wait(self.getTimeout())
  331. self.assertEqual(len(threadIds), 2)
  332. self.assertEqual(threadIds[0], threadIds[1])
  333. def test_callbackContext(self):
  334. """
  335. The context L{ThreadPool.callInThreadWithCallback} is invoked in is
  336. shared by the context the callable and C{onResult} callback are
  337. invoked in.
  338. """
  339. myctx = context.theContextTracker.currentContext().contexts[-1]
  340. myctx['testing'] = 'this must be present'
  341. contexts = []
  342. event = threading.Event()
  343. def onResult(success, result):
  344. ctx = context.theContextTracker.currentContext().contexts[-1]
  345. contexts.append(ctx)
  346. event.set()
  347. def func():
  348. ctx = context.theContextTracker.currentContext().contexts[-1]
  349. contexts.append(ctx)
  350. tp = threadpool.ThreadPool(0, 1)
  351. tp.callInThreadWithCallback(onResult, func)
  352. tp.start()
  353. self.addCleanup(tp.stop)
  354. event.wait(self.getTimeout())
  355. self.assertEqual(len(contexts), 2)
  356. self.assertEqual(myctx, contexts[0])
  357. self.assertEqual(myctx, contexts[1])
  358. def test_existingWork(self):
  359. """
  360. Work added to the threadpool before its start should be executed once
  361. the threadpool is started: this is ensured by trying to release a lock
  362. previously acquired.
  363. """
  364. waiter = threading.Lock()
  365. waiter.acquire()
  366. tp = threadpool.ThreadPool(0, 1)
  367. tp.callInThread(waiter.release) # Before start()
  368. tp.start()
  369. try:
  370. self._waitForLock(waiter)
  371. finally:
  372. tp.stop()
  373. def test_workerStateTransition(self):
  374. """
  375. As the worker receives and completes work, it transitions between
  376. the working and waiting states.
  377. """
  378. pool = threadpool.ThreadPool(0, 1)
  379. pool.start()
  380. self.addCleanup(pool.stop)
  381. # Sanity check
  382. self.assertEqual(pool.workers, 0)
  383. self.assertEqual(len(pool.waiters), 0)
  384. self.assertEqual(len(pool.working), 0)
  385. # Fire up a worker and give it some 'work'
  386. threadWorking = threading.Event()
  387. threadFinish = threading.Event()
  388. def _thread():
  389. threadWorking.set()
  390. threadFinish.wait(10)
  391. pool.callInThread(_thread)
  392. threadWorking.wait(10)
  393. self.assertEqual(pool.workers, 1)
  394. self.assertEqual(len(pool.waiters), 0)
  395. self.assertEqual(len(pool.working), 1)
  396. # Finish work, and spin until state changes
  397. threadFinish.set()
  398. while not len(pool.waiters):
  399. time.sleep(0.0005)
  400. # Make sure state changed correctly
  401. self.assertEqual(len(pool.waiters), 1)
  402. self.assertEqual(len(pool.working), 0)
  403. class RaceConditionTests(unittest.SynchronousTestCase):
  404. def setUp(self):
  405. self.threadpool = threadpool.ThreadPool(0, 10)
  406. self.event = threading.Event()
  407. self.threadpool.start()
  408. def done():
  409. self.threadpool.stop()
  410. del self.threadpool
  411. self.addCleanup(done)
  412. def getTimeout(self):
  413. """
  414. A reasonable number of seconds to time out.
  415. """
  416. return 5
  417. def test_synchronization(self):
  418. """
  419. If multiple threads are waiting on an event (via blocking on something
  420. in a callable passed to L{threadpool.ThreadPool.callInThread}), and
  421. there is spare capacity in the threadpool, sending another callable
  422. which will cause those to un-block to
  423. L{threadpool.ThreadPool.callInThread} will reliably run that callable
  424. and un-block the blocked threads promptly.
  425. @note: This is not really a unit test, it is a stress-test. You may
  426. need to run it with C{trial -u} to fail reliably if there is a
  427. problem. It is very hard to regression-test for this particular
  428. bug - one where the thread pool may consider itself as having
  429. "enough capacity" when it really needs to spin up a new thread if
  430. it possibly can - in a deterministic way, since the bug can only be
  431. provoked by subtle race conditions.
  432. """
  433. timeout = self.getTimeout()
  434. self.threadpool.callInThread(self.event.set)
  435. self.event.wait(timeout)
  436. self.event.clear()
  437. for i in range(3):
  438. self.threadpool.callInThread(self.event.wait)
  439. self.threadpool.callInThread(self.event.set)
  440. self.event.wait(timeout)
  441. if not self.event.isSet():
  442. self.event.set()
  443. self.fail(
  444. "'set' did not run in thread; timed out waiting on 'wait'."
  445. )
  446. class MemoryPool(threadpool.ThreadPool):
  447. """
  448. A deterministic threadpool that uses in-memory data structures to queue
  449. work rather than threads to execute work.
  450. """
  451. def __init__(self, coordinator, failTest, newWorker, *args, **kwargs):
  452. """
  453. Initialize this L{MemoryPool} with a test case.
  454. @param coordinator: a worker used to coordinate work in the L{Team}
  455. underlying this threadpool.
  456. @type coordinator: L{twisted._threads.IExclusiveWorker}
  457. @param failTest: A 1-argument callable taking an exception and raising
  458. a test-failure exception.
  459. @type failTest: 1-argument callable taking (L{Failure}) and raising
  460. L{unittest.FailTest}.
  461. @param newWorker: a 0-argument callable that produces a new
  462. L{twisted._threads.IWorker} provider on each invocation.
  463. @type newWorker: 0-argument callable returning
  464. L{twisted._threads.IWorker}.
  465. """
  466. self._coordinator = coordinator
  467. self._failTest = failTest
  468. self._newWorker = newWorker
  469. threadpool.ThreadPool.__init__(self, *args, **kwargs)
  470. def _pool(self, currentLimit, threadFactory):
  471. """
  472. Override testing hook to create a deterministic threadpool.
  473. @param currentLimit: A 1-argument callable which returns the current
  474. threadpool size limit.
  475. @param threadFactory: ignored in this invocation; a 0-argument callable
  476. that would produce a thread.
  477. @return: a L{Team} backed by the coordinator and worker passed to
  478. L{MemoryPool.__init__}.
  479. """
  480. def respectLimit():
  481. # The expression in this method copied and pasted from
  482. # twisted.threads._pool, which is unfortunately bound up
  483. # with lots of actual-threading stuff.
  484. stats = team.statistics()
  485. if ((stats.busyWorkerCount +
  486. stats.idleWorkerCount) >= currentLimit()):
  487. return None
  488. return self._newWorker()
  489. team = Team(coordinator=self._coordinator,
  490. createWorker=respectLimit,
  491. logException=self._failTest)
  492. return team
  493. class PoolHelper(object):
  494. """
  495. A L{PoolHelper} constructs a L{threadpool.ThreadPool} that doesn't actually
  496. use threads, by using the internal interfaces in L{twisted._threads}.
  497. @ivar performCoordination: a 0-argument callable that will perform one unit
  498. of "coordination" - work involved in delegating work to other threads -
  499. and return L{True} if it did any work, L{False} otherwise.
  500. @ivar workers: the workers which represent the threads within the pool -
  501. the workers other than the coordinator.
  502. @type workers: L{list} of 2-tuple of (L{IWorker}, C{workPerformer}) where
  503. C{workPerformer} is a 0-argument callable like C{performCoordination}.
  504. @ivar threadpool: a modified L{threadpool.ThreadPool} to test.
  505. @type threadpool: L{MemoryPool}
  506. """
  507. def __init__(self, testCase, *args, **kwargs):
  508. """
  509. Create a L{PoolHelper}.
  510. @param testCase: a test case attached to this helper.
  511. @type args: The arguments passed to a L{threadpool.ThreadPool}.
  512. @type kwargs: The arguments passed to a L{threadpool.ThreadPool}
  513. """
  514. coordinator, self.performCoordination = createMemoryWorker()
  515. self.workers = []
  516. def newWorker():
  517. self.workers.append(createMemoryWorker())
  518. return self.workers[-1][0]
  519. self.threadpool = MemoryPool(coordinator, testCase.fail, newWorker,
  520. *args, **kwargs)
  521. def performAllCoordination(self):
  522. """
  523. Perform all currently scheduled "coordination", which is the work
  524. involved in delegating work to other threads.
  525. """
  526. while self.performCoordination():
  527. pass
  528. class MemoryBackedTests(unittest.SynchronousTestCase):
  529. """
  530. Tests using L{PoolHelper} to deterministically test properties of the
  531. threadpool implementation.
  532. """
  533. def test_workBeforeStarting(self):
  534. """
  535. If a threadpool is told to do work before starting, then upon starting
  536. up, it will start enough workers to handle all of the enqueued work
  537. that it's been given.
  538. """
  539. helper = PoolHelper(self, 0, 10)
  540. n = 5
  541. for x in range(n):
  542. helper.threadpool.callInThread(lambda: None)
  543. helper.performAllCoordination()
  544. self.assertEqual(helper.workers, [])
  545. helper.threadpool.start()
  546. helper.performAllCoordination()
  547. self.assertEqual(len(helper.workers), n)
  548. def test_tooMuchWorkBeforeStarting(self):
  549. """
  550. If the amount of work before starting exceeds the maximum number of
  551. threads allowed to the threadpool, only the maximum count will be
  552. started.
  553. """
  554. helper = PoolHelper(self, 0, 10)
  555. n = 50
  556. for x in range(n):
  557. helper.threadpool.callInThread(lambda: None)
  558. helper.performAllCoordination()
  559. self.assertEqual(helper.workers, [])
  560. helper.threadpool.start()
  561. helper.performAllCoordination()
  562. self.assertEqual(len(helper.workers), helper.threadpool.max)