def test_acquire_retry(self): pool = Pool(Mock(), max_connections=1) conn1 = pool.acquire() # make the pool reach the max connection gevent.spawn_later(0, pool.release, conn1) conn = pool.acquire(retry=1) eq_(conn, conn1)
def test_start_connection_reaper(self, mock_reaper): mock_reaper_ins = Mock() mock_reaper.return_value = mock_reaper_ins pool = Pool(Mock(), reap_expired_connections=True, reap_interval=3) mock_reaper.assert_called_once_with(pool, 3) mock_reaper_ins.start.assert_called_once_with()
def test_drop(self): pool = Pool(Mock()) conn1 = pool.acquire() pool.release(conn1) ok_(conn1 in pool._pool) pool.drop(conn1) ok_(conn1 not in pool._pool) conn1.close.assert_called_once_with()
def test_connection_context_manager(self): mock_factory = Mock() mock_connection = Mock() mock_factory.return_value = mock_connection pool = Pool(mock_factory) with pool.connection() as conn: eq_(mock_connection, conn) ok_(mock_connection in pool._using) ok_(not mock_connection in pool._using) ok_(mock_connection in pool._pool)
def test_drop_closed_connection(self): pool = Pool(Mock()) conn1 = pool.acquire() conn1.is_connected = Mock() conn1.is_connected.return_value = False pool.release(conn1) ok_(conn1 in pool._pool) pool.drop(conn1) ok_(conn1 not in pool._pool) ok_(not conn1.close.called)
def __init__(self, server_address: Tuple[str, int], *args, **kwargs): super().__init__(server_address, *args, **kwargs) self.methods = [ self.get_prices_pfx, self.get_prices_nano, self.get_prices_try, ] self.pfx_handle = '' self.nano_handle = '' self.try_handle = '' self.pool = Pool(PoolClient, dict(name=self.name, server_address=server_address)) lock = RLock() def find_pfx(): pool = gsocketpool.pool.Pool( PoolClient, dict(name=self.name, server_address=server_address)) while not self.pfx_handle: with lock: with pool.connection() as client: self.pfx_handle = client.find_window('パートナーズFX$') gevent.sleep(10) def find_nano(): pool = gsocketpool.pool.Pool( PoolClient, dict(name=self.name, server_address=server_address)) while not self.nano_handle: with lock: with pool.connection() as client: self.nano_handle = client.find_window('パートナーズFX nano') gevent.sleep(10) def find_try(): pool = gsocketpool.pool.Pool( PoolClient, dict(name=self.name, server_address=server_address)) while not self.try_handle: with lock: with pool.connection() as client: self.try_handle = client.find_window('レート') gevent.sleep(10) gevent.spawn(find_pfx) gevent.spawn(find_nano) gevent.spawn(find_try)
def test_acquire_and_release(self): mock_factory = Mock() mock_conn1 = Mock() mock_conn2 = Mock() mock_factory.side_effect = [mock_conn1, mock_conn2] pool = Pool(mock_factory, initial_connections=0) eq_(0, pool.size) # acquire conn1 conn1 = pool.acquire() eq_(mock_conn1, conn1) eq_(1, pool.size) ok_(conn1 not in pool._pool) ok_(conn1 in pool._using) # acquire conn2 conn2 = pool.acquire() eq_(mock_conn2, conn2) eq_(2, pool.size) ok_(conn2 in pool._using) # release conn1 pool.release(conn1) ok_(conn1 in pool._pool) ok_(conn1 not in pool._using) # acquire conn1 again conn1_2 = pool.acquire() eq_(mock_conn1, conn1_2) eq_(2, pool.size) ok_(conn1_2 not in pool._pool) ok_(conn1_2 in pool._using) # release conn2 pool.release(conn2) ok_(conn2 in pool._pool) ok_(conn2 not in pool._using) # release conn1 pool.release(conn1_2) ok_(conn1_2 in pool._pool) ok_(conn1_2 not in pool._using)
def test_drop_expired_connections(self): mock_factory = Mock() mock_conn1 = Mock() mock_conn2 = Mock() mock_conn3 = Mock() mock_conn1.is_expired.return_value = True mock_conn2.is_expired.return_value = False mock_conn3.is_expired.return_value = True mock_factory.side_effect = [mock_conn1, mock_conn2, mock_conn3] pool = Pool(mock_factory, initial_connections=3) pool.drop_expired() ok_(mock_conn1 not in pool._pool) mock_conn1.close.assert_called_once_with() ok_(mock_conn2 in pool._pool) ok_(mock_conn3 not in pool._pool) mock_conn1.close.assert_called_once_with()
def test_create_initial_connections(self): mock_factory = Mock() pool = Pool(mock_factory, initial_connections=10) eq_(10, mock_factory.call_count) eq_(10, pool.size)
def test_drop_invalid_connection(self): pool = Pool(Mock()) pool.drop(Mock())
def test_drop_using_connection(self): pool = Pool(Mock()) conn1 = pool.acquire() pool.drop(conn1)
def test_max_connections(self): pool = Pool(Mock(), max_connections=1) for _ in range(2): pool.acquire()
def test_release_already_released_connection(self): pool = Pool(Mock()) conn1 = pool.acquire() pool.release(conn1) pool.release(conn1)
def test_release_invalid_connection(self): pool = Pool(Mock()) pool.release(Mock())