def setUp(self): super(TestQueueFunctionManager, self).setUp() self.thread_manager = mock.create_autospec( mt.MultiThreadingManager, spec_set=True, instance=True) self.thread_count = 4 self.error_counter = [0] self.got_items = Queue() self.stored_results = [] self.qfq = mt.QueueFunctionManager( self._func, self.thread_count, self.thread_manager, thread_args=('1arg', '2arg'), thread_kwargs={'a': 'b', 'store_results': self.stored_results}, error_counter=self.error_counter, connection_maker=self.connection_maker)
def test_context_manager_without_error_counter(self): self.qfq = mt.QueueFunctionManager( self._func, self.thread_count, self.thread_manager, thread_args=('1arg', '2arg'), thread_kwargs={'a': 'b', 'store_results': self.stored_results}, connection_maker=self.connection_maker) with self.qfq as input_queue: self.assertEqual(self.starting_thread_count + self.thread_count, threading.active_count()) input_queue.put('go boom') self.assertEqual(self.starting_thread_count, threading.active_count()) error_strs = map(str, self.thread_manager.error.call_args_list) self.assertEqual(1, len(error_strs)) self.assertTrue('Exception: I went boom!' in error_strs[0])
def test_context_manager_without_conn_maker_or_error_counter(self): self.qfq = mt.QueueFunctionManager( self._func, self.thread_count, self.thread_manager, thread_args=('1arg', '2arg'), thread_kwargs={'a': 'b'}) with self.qfq as input_queue: self.assertEqual(self.starting_thread_count + self.thread_count, threading.active_count()) for i in xrange(20): input_queue.put('slap%d' % i) self.assertEqual(self.starting_thread_count, threading.active_count()) self.assertEqual([], self.thread_manager.error.call_args_list) self.assertEqual(0, self.error_counter[0]) self.assertQueueContains(self.got_items, set(['slap%d' % i for i in xrange(20)])) self.assertQueueContains( self.got_args_kwargs, [(('1arg', '2arg'), {'a': 'b'})] * 20) self.assertEqual(self.stored_results, [])