Skip to content

Commit 0687402

Browse files
author
Takashi Matsuo
authored
docs: add a note about infinite loops (#3553)
1 parent bfcc58b commit 0687402

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

AUTHORING_GUIDE.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ example](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/
287287
* We use parallel processing for tests, so tests should be capable of running in parallel with one another.
288288
* Use pytest's fixture for resource setup and teardown, instead of
289289
having them in the test itself.
290+
* Avoid infinite loops.
290291

291292
### Arrange, Act, Assert
292293

@@ -347,8 +348,43 @@ is expected. Strive to verify the content of the output rather than the syntax.
347348
For example, the test might verify that a string is included in the output,
348349
without taking a dependency on where that string occurs in the output.
349350

350-
### Running tests
351+
### Avoid infinite loops
352+
353+
Never put potential infinite loops in the test code path. A typical
354+
example is about gRPC's LongRunningOperations. Make sure you pass the
355+
timeout parameter to the `result()` call.
356+
357+
Good:
358+
359+
```python
360+
# will raise google.api_core.GoogleAPICallError after 60 seconds
361+
operation.result(60)
362+
```
363+
364+
Bad:
365+
366+
```python
367+
operation.result() # this could wait forever.
368+
```
351369

370+
We recommend the timeout parameter to be around the number that gives
371+
you more than 90% success rate. Don't put too long a timeout.
372+
373+
Now this test is inevitably flaky, so consider marking the test as
374+
`flaky` as follows:
375+
376+
```python
377+
378+
@pytest.mark.flaky(max_runs=3, min_passes=1)
379+
def my_flaky_test():
380+
# test that involves LRO poling with the timeout
381+
```
382+
383+
This combination will give you very high success rate with fixed test
384+
execution time (0.999 success rate and 180 seconds operation wait time
385+
in the worst case in this example).
386+
387+
### Running tests
352388

353389
Automated testing for samples in `python-docs-samples` is managed by
354390
[nox](https://nox.readthedocs.io). Nox allows us to run a variety of tests,
@@ -387,7 +423,6 @@ To run a specific test from a specific following:
387423
nox -s py-3.7 -- snippets_test.py:test_list_blobs
388424
```
389425

390-
391426
### Test Environment Setup
392427

393428
Because all tests are system tests that use live resources, running tests

0 commit comments

Comments
 (0)