failure-scenarios 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- failure_scenarios-0.1.0/.gitignore +12 -0
- failure_scenarios-0.1.0/CHANGELOG.md +13 -0
- failure_scenarios-0.1.0/LICENSE +191 -0
- failure_scenarios-0.1.0/PKG-INFO +252 -0
- failure_scenarios-0.1.0/README.md +219 -0
- failure_scenarios-0.1.0/examples/__init__.py +0 -0
- failure_scenarios-0.1.0/examples/circuit_breaker/__init__.py +0 -0
- failure_scenarios-0.1.0/examples/circuit_breaker/breaker.py +72 -0
- failure_scenarios-0.1.0/examples/circuit_breaker/test_breaker.py +75 -0
- failure_scenarios-0.1.0/examples/fastapi_checkout/__init__.py +0 -0
- failure_scenarios-0.1.0/examples/fastapi_checkout/app.py +63 -0
- failure_scenarios-0.1.0/examples/fastapi_checkout/test_checkout.py +82 -0
- failure_scenarios-0.1.0/pyproject.toml +81 -0
- failure_scenarios-0.1.0/src/failure_scenarios/__init__.py +77 -0
- failure_scenarios-0.1.0/src/failure_scenarios/_faults.py +235 -0
- failure_scenarios-0.1.0/src/failure_scenarios/_patching.py +79 -0
- failure_scenarios-0.1.0/src/failure_scenarios/_plans.py +140 -0
- failure_scenarios-0.1.0/src/failure_scenarios/_points.py +139 -0
- failure_scenarios-0.1.0/src/failure_scenarios/_report.py +57 -0
- failure_scenarios-0.1.0/src/failure_scenarios/_scenario.py +526 -0
- failure_scenarios-0.1.0/src/failure_scenarios/httpx.py +206 -0
- failure_scenarios-0.1.0/src/failure_scenarios/py.typed +0 -0
- failure_scenarios-0.1.0/src/failure_scenarios/pytest_plugin.py +115 -0
- failure_scenarios-0.1.0/src/failure_scenarios/requests.py +164 -0
- failure_scenarios-0.1.0/tests/conftest.py +61 -0
- failure_scenarios-0.1.0/tests/test_core.py +227 -0
- failure_scenarios-0.1.0/tests/test_httpx.py +111 -0
- failure_scenarios-0.1.0/tests/test_pytest_plugin.py +78 -0
- failure_scenarios-0.1.0/tests/test_requests.py +63 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes are documented here. The project follows [Semantic Versioning](https://semver.org/).
|
|
4
|
+
|
|
5
|
+
## 0.1.0 (unreleased)
|
|
6
|
+
|
|
7
|
+
- Fault outcomes: `Respond`, `Timeout`, `ConnectionFailure`, `LostResponse`, `Duplicate`, `Delay`, `Raise`, `Return`, `PASS`, with HTTP helpers.
|
|
8
|
+
- Plans: `always`, `sequence`, `fail_times`, `fail_after`, `cycle`, and seeded `intermittent`.
|
|
9
|
+
- `Scenario` with ordered rules, thread-safe call history, reset, nesting and strict unmatched mode.
|
|
10
|
+
- Assertions: call counts, retries, minimum intervals, backoff, untriggered rules.
|
|
11
|
+
- HTTPX sync and async transports, a Requests adapter, and process-wide transport patching.
|
|
12
|
+
- Generic fault points (`fault_point`, `inject`, `inject_async`).
|
|
13
|
+
- pytest plugin: `fault_scenario` fixture, marker, `--fault-seed`, `--fault-report`, JUnit properties, and a failure section with reproduction command.
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2026 Bsrat Mulugeta
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: failure-scenarios
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Deterministic, reproducible dependency-failure scenarios for testing retries, fallbacks, timeouts and recovery.
|
|
5
|
+
Author-email: Bsrat Mulugeta <bsratmulugetasergawie@gmail.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: fault-injection,httpx,pytest,requests,resilience,retry,testing
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Framework :: Pytest
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Software Development :: Testing
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Provides-Extra: all
|
|
23
|
+
Requires-Dist: httpx>=0.27; extra == 'all'
|
|
24
|
+
Requires-Dist: pytest>=8.0; extra == 'all'
|
|
25
|
+
Requires-Dist: requests>=2.31; extra == 'all'
|
|
26
|
+
Provides-Extra: httpx
|
|
27
|
+
Requires-Dist: httpx>=0.27; extra == 'httpx'
|
|
28
|
+
Provides-Extra: pytest
|
|
29
|
+
Requires-Dist: pytest>=8.0; extra == 'pytest'
|
|
30
|
+
Provides-Extra: requests
|
|
31
|
+
Requires-Dist: requests>=2.31; extra == 'requests'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# failure-scenarios
|
|
35
|
+
|
|
36
|
+
[](LICENSE)
|
|
37
|
+
[](https://www.python.org/downloads/)
|
|
38
|
+
|
|
39
|
+
Deterministic, reproducible dependency-failure scenarios for Python tests.
|
|
40
|
+
|
|
41
|
+
Your code calls payment providers, auth services, email APIs and queues. This
|
|
42
|
+
package lets a test say *"the first two charges return 503, the third times
|
|
43
|
+
out, then the provider recovers"*, and then check what your code did: how many
|
|
44
|
+
attempts it made, how far apart, with which idempotency key, and whether the
|
|
45
|
+
fallback kicked in.
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
import failure_scenarios as fs
|
|
49
|
+
|
|
50
|
+
def test_checkout_survives_provider_blips(fault_scenario, client):
|
|
51
|
+
provider = fault_scenario.http(
|
|
52
|
+
fs.sequence(fs.service_unavailable(), fs.Timeout("read"),
|
|
53
|
+
then=fs.Respond(200, json={"id": "ch_1"})),
|
|
54
|
+
method="POST", url="https://payments.example/v1/charges",
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
assert client.post("/checkout", json=ORDER).status_code == 200
|
|
58
|
+
provider.assert_retried(2)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
When a test fails, the report shows exactly what was injected. Here the provider never recovers within the app's three attempts:
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
------------------------------ failure scenario -------------------------------
|
|
65
|
+
scenario 'tests/test_checkout.py::test_checkout_recovers' (seed=340277690, unmatched=error)
|
|
66
|
+
rules:
|
|
67
|
+
charge 3 call(s) plan: sequence [HTTP 503 Service Unavailable, read timeout (simulated), read timeout (simulated)] then pass through
|
|
68
|
+
calls:
|
|
69
|
+
# t+s request rule injected result
|
|
70
|
+
0 0.454 POST https://payments.example/v1/charges charge[0] HTTP 503 Service Unavailable HTTP 503
|
|
71
|
+
1 0.516 POST https://payments.example/v1/charges charge[1] read timeout (simulated) raised ReadTimeout
|
|
72
|
+
2 0.625 POST https://payments.example/v1/charges charge[2] read timeout (simulated) raised ReadTimeout
|
|
73
|
+
|
|
74
|
+
reproduce: pytest 'tests/test_checkout.py::test_checkout_recovers'
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Where it fits
|
|
78
|
+
|
|
79
|
+
Mocking libraries such as respx, pytest-httpx, responses and requests-mock
|
|
80
|
+
replace a dependency with canned answers. They are the right tool for "return
|
|
81
|
+
this JSON". This package covers what they leave to hand-written callbacks:
|
|
82
|
+
|
|
83
|
+
- **Failure over time.** Ordered sequences, fail N times then recover, fail
|
|
84
|
+
after N successes, cycles, and seeded intermittent failure.
|
|
85
|
+
- **Transport-level errors raised as the client's own exceptions.** You get
|
|
86
|
+
`httpx.ReadTimeout` and `requests.ConnectTimeout`, the exceptions your retry
|
|
87
|
+
code actually catches.
|
|
88
|
+
- **Delivered-but-lost and duplicated requests.** For idempotency testing,
|
|
89
|
+
the request really reaches the upstream before the failure.
|
|
90
|
+
- **Behaviour assertions.** Attempt counts, retries, minimum gaps, exponential
|
|
91
|
+
backoff, rules that never matched.
|
|
92
|
+
- **Reproducibility.** Every random decision is seeded per test and per rule,
|
|
93
|
+
and the seed is printed with the failure.
|
|
94
|
+
|
|
95
|
+
It does not implement retries or circuit breakers. Use tenacity, stamina,
|
|
96
|
+
pybreaker or your own code for that. This package only lets you test them.
|
|
97
|
+
|
|
98
|
+
## Install
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
pip install failure-scenarios # core: no dependencies
|
|
102
|
+
pip install "failure-scenarios[httpx]" # + HTTPX sync/async support
|
|
103
|
+
pip install "failure-scenarios[requests]"
|
|
104
|
+
pip install "failure-scenarios[all]" # httpx, requests, pytest
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Python 3.10–3.14. The pytest plugin loads automatically when pytest is installed.
|
|
108
|
+
|
|
109
|
+
## Concepts
|
|
110
|
+
|
|
111
|
+
| Concept | What it is |
|
|
112
|
+
|---|---|
|
|
113
|
+
| **Fault** | One outcome for one call: `Respond`, `Timeout`, `ConnectionFailure`, `LostResponse`, `Duplicate`, `Delay`, `Raise`, `Return`, `PASS`. |
|
|
114
|
+
| **Plan** | Which fault the *n*-th matching call gets: `always`, `sequence`, `fail_times`, `fail_after`, `cycle`, `intermittent`. |
|
|
115
|
+
| **Rule** | A matcher (method, URL, host, path, predicate, or fault-point name) plus a plan. It records its calls and holds the assertions. |
|
|
116
|
+
| **Scenario** | Ordered rules (the first match wins), the call history, a seed, and activation. |
|
|
117
|
+
|
|
118
|
+
## Faults
|
|
119
|
+
|
|
120
|
+
| Fault | HTTPX | Requests | Fault point |
|
|
121
|
+
|---|---|---|---|
|
|
122
|
+
| `Respond(status, headers, body/json)` | `httpx.Response` | `requests.Response` | n/a |
|
|
123
|
+
| `Timeout("connect"/"read"/"write"/"pool")` | `ConnectTimeout` / `ReadTimeout` / `WriteTimeout` / `PoolTimeout` | `ConnectTimeout` / `ReadTimeout` / `Timeout` / `Timeout` | `TimeoutError` |
|
|
124
|
+
| `ConnectionFailure()` | `ConnectError` | `ConnectionError` | `ConnectionError` |
|
|
125
|
+
| `LostResponse()` | request sent, then `ReadError` | request sent, then `ConnectionError` | function runs, then `ConnectionError` |
|
|
126
|
+
| `Duplicate(times=2)` | request sent *n* times, last response returned | same | function runs *n* times |
|
|
127
|
+
| `Delay(seconds, then=...)` | sleeps, then applies `then` | same | same |
|
|
128
|
+
| `Raise(exc)` / `Return(value)` | raises `exc` / n/a | raises `exc` / n/a | raises / returns |
|
|
129
|
+
|
|
130
|
+
Helpers: `http_error(status)`, `rate_limited(retry_after=)`,
|
|
131
|
+
`service_unavailable(retry_after=)`, `malformed_json()`, `empty_body()`.
|
|
132
|
+
|
|
133
|
+
**About simulated errors.** `Timeout` and `ConnectionFailure` raise the
|
|
134
|
+
client's exception straight away. No socket is opened and no real network
|
|
135
|
+
latency happens. `Delay` really waits, using the scenario's `sleep` function,
|
|
136
|
+
which you can replace. The package does not throttle bandwidth, drop packets
|
|
137
|
+
or slow down TLS. For real network faults, use a proxy such as Toxiproxy.
|
|
138
|
+
|
|
139
|
+
## Using it
|
|
140
|
+
|
|
141
|
+
### With pytest
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
@pytest.mark.fault_scenario(unmatched="error") # optional: forbid unplanned real calls
|
|
145
|
+
def test_rates_fallback(fault_scenario):
|
|
146
|
+
rates = fault_scenario.http(fs.always(fs.Timeout("read")), url="https://shipping.example/v2/rates")
|
|
147
|
+
assert get_rate() == FALLBACK
|
|
148
|
+
rates.assert_called(times=3)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
`fault_scenario` is active for the test and removed afterwards. Each test
|
|
152
|
+
gets a fresh scenario and a stable seed taken from its test id.
|
|
153
|
+
|
|
154
|
+
| Option | Effect |
|
|
155
|
+
|---|---|
|
|
156
|
+
| `--fault-seed=N` | Change the base seed for every test (still reproducible). |
|
|
157
|
+
| `--fault-report=PATH` | Write a JSON report of every scenario in the run. |
|
|
158
|
+
| `--junitxml=...` | Each test gets a `fault_scenario` property with its seed and totals. |
|
|
159
|
+
|
|
160
|
+
### Without pytest
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
scenario = fs.Scenario("nightly-smoke", seed=42)
|
|
164
|
+
scenario.http(fs.intermittent(fs.service_unavailable(), 0.2), host="api.example.com")
|
|
165
|
+
with scenario: # patches HTTPX and Requests transports
|
|
166
|
+
run_job()
|
|
167
|
+
print(scenario.summary())
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Explicit transports (no global patching)
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
from failure_scenarios.httpx import FaultTransport, AsyncFaultTransport
|
|
174
|
+
from failure_scenarios.requests import FaultAdapter
|
|
175
|
+
|
|
176
|
+
client = httpx.Client(transport=FaultTransport(scenario)) # forwards to the real network
|
|
177
|
+
client = httpx.Client(transport=FaultTransport(scenario, httpx.MockTransport(handler))) # or to a mock
|
|
178
|
+
session.mount("https://", FaultAdapter(scenario))
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Activation patches `httpx.HTTPTransport`, `httpx.AsyncHTTPTransport` and
|
|
182
|
+
`requests.adapters.HTTPAdapter.send`. Clients built on custom transports, such
|
|
183
|
+
as `MockTransport`, respx routes or FastAPI's `TestClient`, are not
|
|
184
|
+
intercepted. Wrap those explicitly. If a request passes through both an
|
|
185
|
+
explicit transport and a patched one, the scenario is applied only once.
|
|
186
|
+
|
|
187
|
+
### Non-HTTP dependencies
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
@fs.fault_point("email.send")
|
|
191
|
+
def send_email(to, body): ...
|
|
192
|
+
|
|
193
|
+
fault_scenario.point("email.send", fs.fail_times(2, fs.ConnectionFailure()))
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
`fs.inject(name, func, *args)` and `fs.inject_async(...)` do the same without
|
|
197
|
+
a decorator. When no scenario is active, fault points just call the function.
|
|
198
|
+
|
|
199
|
+
### Matching
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
scenario.http(plan, method="POST", url="https://api.example.com/v1/*") # glob
|
|
203
|
+
scenario.http(plan, host="auth.example.com", path=re.compile(r"^/oauth")) # regex
|
|
204
|
+
scenario.http(plan, when=lambda req: req.headers.get("x-tenant") == "b") # predicate
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
An exact URL also matches the same URL with a query string.
|
|
208
|
+
|
|
209
|
+
### Assertions
|
|
210
|
+
|
|
211
|
+
`rule.assert_called(times= | at_least= | at_most=)`, `assert_not_called()`,
|
|
212
|
+
`assert_retried(n)`, `assert_min_interval(seconds)`,
|
|
213
|
+
`assert_backoff(factor=, min_first=)`, `rule.intervals()`, `rule.calls`
|
|
214
|
+
(with `.request.headers` and `.request.content`), and
|
|
215
|
+
`scenario.assert_all_triggered()`. Each failure message includes the call
|
|
216
|
+
table.
|
|
217
|
+
|
|
218
|
+
`scenario.reset()` clears the history and rewinds every plan.
|
|
219
|
+
`scenario.to_dict()` and `fs.to_json(scenario)` export the history.
|
|
220
|
+
|
|
221
|
+
## Examples
|
|
222
|
+
|
|
223
|
+
- [`examples/fastapi_checkout`](examples/fastapi_checkout): retries with
|
|
224
|
+
backoff, one idempotency key across attempts, giving up, and malformed
|
|
225
|
+
bodies.
|
|
226
|
+
- [`examples/circuit_breaker`](examples/circuit_breaker): a Requests client
|
|
227
|
+
with a breaker, covering open, half-open, reopen and fallback.
|
|
228
|
+
|
|
229
|
+
Both examples run as part of the test suite. Django needs nothing special.
|
|
230
|
+
The fixture intercepts outbound HTTPX and Requests calls from views and
|
|
231
|
+
services the same way.
|
|
232
|
+
|
|
233
|
+
## Development
|
|
234
|
+
|
|
235
|
+
```bash
|
|
236
|
+
uv sync
|
|
237
|
+
uv run pytest
|
|
238
|
+
uv run ruff check src tests examples && uv run mypy
|
|
239
|
+
uv run --group docs mkdocs serve
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Status
|
|
243
|
+
|
|
244
|
+
Version 0.1.0 is feature-complete for its scope. Licensed under Apache-2.0.
|
|
245
|
+
|
|
246
|
+
## Contributing
|
|
247
|
+
|
|
248
|
+
Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
249
|
+
|
|
250
|
+
## License
|
|
251
|
+
|
|
252
|
+
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.
|