def testTooManyTasksThrows(self): l = Lock() with thread_pool(1, max_tasks=1) as tp: with l: t1 = thread_pool_fixture.blocking_call(l) t2 = thread_pool_fixture.blocking_call(l) t3 = thread_pool_fixture.blocking_call(l) tp.process(t1.process) # wait until the above process has started while(tp.task_count() != 0): pass # start a second process which fills our queue tp.process(t2.process) # test a third process (the second non-live one) # causes an exception self.assertRaises(thread_pool_full_exception, tp.process, t3.process) self.assertEqual(tp.task_count(), 1) self.assertEqual(tp.thread_count(), 1)
def testTooManyTasksCreatesANewThread(self): l = Lock() with thread_pool(1) as tp: with l: t1 = thread_pool_fixture.blocking_call(l) t2 = thread_pool_fixture.blocking_call(l) t3 = thread_pool_fixture.blocking_call(l) tp.process(t1.process) # wait until the above process has started while(tp.task_count() != 0): pass # start a second process which fills our queue tp.process(t2.process) # test a third process (the second non-live one) # causes a new thread to be generated tp.process(t3.process) sleep(1.0) # sleep so the monitor can now reduce the thread count # back to the minimum number sleep(1.0) tp.join() self.assertEqual(tp.stats()["MaxThreads"], 2) self.assertEqual(tp.thread_count(), 1)
def testConstruction(self): expected_threads = 10 expected_max_tasks = 23 with thread_pool(expected_threads, max_tasks=expected_max_tasks) as tp: self.assertEqual(expected_threads, tp.thread_count()) self.assertEqual(expected_max_tasks, tp.max_tasks())