def test_lazy_connections(self): ctpe = mt.ConnectionThreadPoolExecutor(self._create_conn, 10) with ctpe as pool: # Submit multiple jobs sequentially - should only use 1 conn f = pool.submit(self._func, "succeed") f.result() f = pool.submit(self._func, "succeed") f.result() f = pool.submit(self._func, "succeed") f.result() expected_connections = [(0, "This is a connection")] expected_connections.extend([(x, None) for x in range(1, 10)]) self.assertQueueContains(pool._connections, expected_connections) ctpe = mt.ConnectionThreadPoolExecutor(self._create_conn, 10) with ctpe as pool: fs = [] f1 = pool.submit(self._func, "sleep") f2 = pool.submit(self._func, "sleep") f3 = pool.submit(self._func, "sleep") fs.extend([f1, f2, f3]) expected_connections = [(0, "This is a connection"), (1, "This is a connection"), (2, "This is a connection")] expected_connections.extend([(x, None) for x in range(3, 10)]) for f in as_completed(fs): f.result() self.assertQueueContains(pool._connections, expected_connections)
def test_submit_good_connection(self): ctpe = mt.ConnectionThreadPoolExecutor(self._create_conn, 1) with ctpe as pool: # Try submitting a job that should succeed f = pool.submit(self._func, "succeed") f.result() self.assertQueueContains(self.got_items, [("This is a connection", "succeed")]) # Now a job that fails went_boom = False try: f = pool.submit(self._func, "go boom") f.result() except Exception as e: went_boom = True self.assertEquals('I went boom!', str(e)) self.assertTrue(went_boom) # Has the connection been returned to the pool? f = pool.submit(self._func, "succeed") f.result() self.assertQueueContains(self.got_items, [("This is a connection", "go boom"), ("This is a connection", "succeed")])
def test_submit_bad_connection(self): ctpe = mt.ConnectionThreadPoolExecutor(self._create_conn_fail, 1) with ctpe as pool: # Now a connection that fails try: f = pool.submit(self._func, "succeed") f.result() except Exception as e: self.assertEqual('This is a failed connection', str(e)) else: self.fail('The connection did not fail') # Make sure we don't lock up on failed connections try: f = pool.submit(self._func, "go boom") f.result() except Exception as e: self.assertEqual('This is a failed connection', str(e)) else: self.fail('The connection did not fail')