class TestKiddiePool(unittest.TestCase): def setUp(self): super(TestKiddiePool, self).setUp() self.pool = KiddiePool( ['foo:123', 'bar:321'], connection_factory=FakeConnection, connection_options={'tcp_keepalives': False}, max_size=2, pool_timeout=.1, connect_attempts=2, ) def test_max_size_and_pool_timeout(self): # Try to get more connections than max_size self.pool.get() self.pool.get() # Start timer start = time.time() # Make sure getting next connection throws KiddiePoolEmpty self.assertRaises( KiddiePoolEmpty, self.pool.get ) # Make sure it took at least the timeout we set seconds self.assertTrue(time.time() - start > self.pool.pool_timeout) def test_connect_attempts(self): # Make a kiddiepool and mock the connection conn = FakeConnection() with patch.object(conn, 'connect') as mock_connect: mock_connect.return_value = False with self.assertRaises(KiddiePoolMaxAttempts): self.pool._connect(conn) self.assertEqual(sorted(mock_connect.call_args_list), sorted([ call('foo', 123), call('foo', 123), call('bar', 321), call('bar', 321), ])) def test_connection_options(self): # Pass connection options into kiddiepool (in setUp) # Make sure they are applied to internal connections self.assertFalse(self.pool.connection_pool.get().tcp_keepalives) def test_reset_hosts(self): # Get connections orig_conn1 = self.pool.get() orig_conn2 = self.pool.get() self.pool.put(orig_conn1) self.pool.put(orig_conn2) # Change hosts self.pool.set_hosts(['baz:666']) # Get connections conn1 = self.pool.get() conn2 = self.pool.get() # make sure they changed to the new hosts self.assertEqual(conn1.host, 'baz') self.assertEqual(conn2.host, 'baz') self.assertEqual(conn1.port, 666) self.assertEqual(conn2.port, 666)
class TestKiddiePool(mimic.MimicTestBase): def setUp(self): super(TestKiddiePool, self).setUp() self.pool = KiddiePool( ['foo:123', 'bar:321'], connection_factory=FakeConnection, connection_options={'tcp_keepalives': False}, max_size=2, pool_timeout=.1, connect_attempts=2, ) def test_max_size_and_pool_timeout(self): # Try to get more connections than max_size self.pool.get() self.pool.get() # Start timer start = time.time() # Make sure getting next connection throws KiddiePoolEmpty self.assertRaises( KiddiePoolEmpty, self.pool.get ) # Make sure it took at least the timeout we set seconds self.assertTrue(time.time() - start > self.pool.pool_timeout) def test_connect_attempts(self): # Make a kiddiepool and mock the connection conn = FakeConnection() # Make sure it tries each host right number of times self.mimic.stub_out_with_mock(conn, 'connect') conn.connect('foo', 123).InAnyOrder().AndReturn(False) conn.connect('foo', 123).InAnyOrder().AndReturn(False) conn.connect('bar', 321).InAnyOrder().AndReturn(False) conn.connect('bar', 321).InAnyOrder().AndReturn(False) self.mimic.replay_all() # Make sure it raises KiddiePoolMaxAttempts as well self.assertRaises( KiddiePoolMaxAttempts, self.pool._connect, conn ) def test_connection_options(self): # Pass connection options into kiddiepool (in setUp) # Make sure they are applied to internal connections self.assertFalse(self.pool.connection_pool.get().tcp_keepalives) def test_reset_hosts(self): # Get connections orig_conn1 = self.pool.get() orig_conn2 = self.pool.get() self.pool.put(orig_conn1) self.pool.put(orig_conn2) # Change hosts self.pool.set_hosts(['baz:666']) # Get connections conn1 = self.pool.get() conn2 = self.pool.get() # make sure they changed to the new hosts self.assertEqual(conn1.host, 'baz') self.assertEqual(conn2.host, 'baz') self.assertEqual(conn1.port, 666) self.assertEqual(conn2.port, 666)