python - 如何模拟 aiohttp 客户端会话超时
问题描述
我有以下获取请求的实现
def function():
async with aiohttp.ClientSession() as session:
try:
async with session.get(
url="http://test-api/v1/radiation",
allow_redirects=False,
params={
"alpha": "a",
"beta": "b",
},
timeout=5
) as response:
return await self.json_response(
results=await response.json(),
status=response.status
)
except ClientError as e:
return await self.json_response(
results={"100": "error"},
status=408
)
我嘲笑aiohttp.ClientSession.get
如下
def r_val():
sleep(10)
return CoroutineMock(side_effect=[{}])
with asynctest.patch("aiohttp.ClientSession.get") as mocked_session:
mock = mocked_session.return_value.__aenter__.return_value
mock.json = r_val()
mock.status = 200
如何在这里模拟超时场景?
解决方案
推荐阅读
- r - r 中有两个 y 轴的图形
- powershell - wget -i URI 问题
- jenkins - 用于安全警告的 Jenkins groovy 设置
- xamarin - 找不到应用中心 CLI。使用 npm 安装 appcenter-cli
- unit-testing - 在 Visual Studio 中使用 Catch2 进行单元测试的最佳实践
- regex - sed 删除不包含带有特殊字符的特定字符串的行
- sql - 将查询结果放入 SQL Server 中的新表中
- javascript - 下载数据时,JqueryUI 如何读取您的 jquery 请求中的数据?
- c# - 如何参考其中的其他dll来制作dll
- mysql - MYSQL Where on Date 列返回重复项