【问题标题】:Locust - retry certain status code but default-handle all othersLocust - 重试某些状态码,但默认处理所有其他状态码
【发布时间】:2021-07-19 13:18:29
【问题描述】:

如果我的访问令牌过期,我的 Locust 任务可能会以 401 告终。在这种情况下,我将更新它,然后重试该任务。这一切都很完美(基于this):

@task
def list_claims(self):
    with self.client.get("/api/1.0/claim/", name="LIST claims", headers=self.headers, catch_response=True) as response:
        if response.status_code == 401:
            logging.info("401 - Need to fetch a new access token!") # usually happens when the access tokens expires after an hour
            self.aad_b2c_auth()
            raise RescheduleTaskImmediately() # re-run this task with the newly refreshed token

问题是这似乎吸收了所有其他状态代码,所以任务永远不会显示为失败,即使它,例如,返回 503 等。我认为这是因为 catch_response=True?!所以理想情况下,我只想对 401 进行特殊处理,并将其余部分留给默认处理程序(2xx = 成功,4xx/5xx = 失败)。有没有内置的方法来实现这一点?

【问题讨论】:

    标签: python locust


    【解决方案1】:

    您应该能够在 401 检查后添加 else 语句并触发请求事件。在测试其他系统时手动触发请求事件的上下文中,可以看到 here 的一个示例。 API 记录了here。基本上像这样的东西应该可以工作:

    else:
        environment.events.request.fire(**response.request_meta)
    

    或者,您可以尝试将请求标记为没有手动结果,而 Locust 可能会为您执行触发。

    else:
        response._manual_result = False
    

    不确定这样做是否在技术上得到支持,因此它可能无法始终正常运行,因此可能需要在未来进行额外的维护。

    【讨论】:

    • 谢谢我试试environment.events.request.fire
    猜你喜欢
    • 2020-07-14
    • 2019-08-26
    • 2016-01-16
    • 2020-02-10
    • 2022-06-21
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多