我对aiohttp应用程序测试有一个奇怪的问题。
简而言之,我使用的项目是:
App是graphql API提供商。 我正在执行简单的用户列表请求。
当我手动测试API或使用不涉及pytest,unittest等的任何其他工具时,一切正常。我无法重现问题。
但是,一旦我开始运行测试,第一个测试就会成功,并且所有中断后响应都会返回错误:“池关闭”。首先,我使用aiopg并发现了与我类似的问题,并决定尝试使用其他工具并遇到同样的问题。
重要细节值得一提。
我没有向app['db']
分配引擎,我已经编写了该类来处理连接以及拆分读写数据库。
这是它的外观:
class DatabaseHandler:
"""Keeps track of databases used in app"""
__write_db_list__: List[Engine] = []
__read_db_list__: List[Engine] = []
def __init__(self,
write_dbs: Union[Iterable[Engine], None] = None,
read_dbs: Union[Iterable[Engine], None] = None) \
-> None:
"""Constructor. Accepts list of write and read database engines"""
super().__init__()
if write_dbs:
for url in write_dbs:
self.add_write_db(url)
if read_dbs:
for url in read_dbs:
self.add_read_db(url)
@staticmethod
async def url_to_engine(url):
"""Builds db engine from url"""
return await gino.create_engine(url)
@staticmethod
async def build(write_urls: Iterable[str], read_urls: Iterable[str]):
return DatabaseHandler(
[await DatabaseHandler.url_to_engine(u) for u in write_urls],
[await DatabaseHandler.url_to_engine(u) for u in read_urls]
)
def add_read_db(self, engine: Engine):
"""Adds db engine to read list"""
self.__read_db_list__.append(engine)
def add_write_db(self, engine: Engine):
"""Adds db engine to write list"""
self.__write_db_list__.append(engine)
def get_for_read(self) -> Union[Engine, None]:
"""Returns engine for reading requests (SELECT)"""
if len(self.__read_db_list__) > 0:
result = self.__read_db_list__[0]
else:
result = self.get_for_write()
return result
def get_for_write(self) -> Union[Engine, None]:
"""Returns engine for writing requests (INSERT, UPDATE)"""
result = None
if len(self.__write_db_list__) > 0:
result = self.__write_db_list__[0]
return result
async def disconnect(self) -> None:
"""Method to disconect from db"""
read_db_engines: List[Engine] = self.__read_db_list__
for db_engine in read_db_engines:
await db_engine.close()
write_db_engine: List[Engine] = self.__write_db_list__
for db_engine in write_db_engine:
await db_engine.close()
和启动功能中的数据库初始化:
async def init_db(app: web.Application) -> None:
"""Initialize db connections"""
app['db'] = await DatabaseHandler.build(
[app.get('config', {}).get('WRITE_DB')],
[app.get('config', {}).get('READ_DB')]
)
测试的外观:
class UserListTestCase(AioHTTPTestCase):
async def get_application(self) -> web.Application:
return init_app()
@unittest_run_loop
async def test_get_all_users(self):
client: TestClient = self.client
resp: ClientResponse = await client.post(
ADMIN_BASE_URI,
json=await build_gql_request(USERS_LIST, {
"query": ""
})
)
assert resp.status == 200
raise Exception(await resp.json())
data = (await resp.json()).get('data', {}).get('users', {})
assert data.get('count') == 7
assert len(data.get('result')) == 7
resp.close()
@unittest_run_loop
async def test_get_users_by_status(self):
client: TestClient = self.client
resp: ClientResponse = await client.post(
ADMIN_BASE_URI,
json=await build_gql_request(USERS_LIST, {
"query": "",
"isActive": True
})
)
assert resp.status == 200
raise Exception(await resp.json())
data = (await resp.json()).get('data', {}).get('user', {})
assert data.get('count') == 4
assert len(data.get('result')) == 4
assert set([u.get('id') for u in data.get('result', [])]) \
== {1, 2, 5, 7}
resp: ClientResponse = await client.post(
ADMIN_BASE_URI,
json=await build_gql_request(USERS_LIST, {
"query": "",
"is_active": False
})
)
assert resp.status == 200
data = (await resp.json()).get('data', {}).get('user', {})
assert data.get('count') == 3
assert len(data.get('result')) == 3
assert [u.get('id') for u in data.get('result', [])] == [3, 4, 6]
我尝试通过功能进行测试的结果相同。 不管第一个测试将运行什么,它都将成功运行,并且所有其他测试都将失败,并显示“关闭池”错误。
我想了解为什么会发生这种情况,并且有可能在生产中遇到相同的问题。
我想我错过了异步和基于异步框架的工作方式。
将很高兴获得任何帮助。