How do you reproduce a transient failure followed by a normal response offline?

A Mock can consume a side_effect sequence call by call and raise an exception at the position where one was supplied.

Python example and interpretation

The first call times out and the next returns ok. This fixture tests consumer behavior for those outcomes. Measure real service latency and availability separately.

from unittest.mock import Mock
service=Mock(side_effect=[TimeoutError(),'ok'])
try: service()
except TimeoutError: pass
[service(),service.call_count]

Result ['ok', 2]

中文

如何离线重现一次临时失败后的正常模型响应?

Mock 可以从 side_effect 序列按次取值,序列中的异常会在对应调用上抛出。

Python 算例与适用条件

首轮响应超时,第二轮得到 ok。此例测试调用方如何处理两种结果;真实服务的时延和可用性须另外测量。

from unittest.mock import Mock
service=Mock(side_effect=[TimeoutError(),'ok'])
try: service()
except TimeoutError: pass
[service(),service.call_count]

结果 ['ok', 2]

References / 参考来源

Keep reading / 继续阅读