Skip to content

docs: add a note about infinite loops #3553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions AUTHORING_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ example](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/
* We use parallel processing for tests, so tests should be capable of running in parallel with one another.
* Use pytest's fixture for resource setup and teardown, instead of
having them in the test itself.
* Avoid infinite loops.

### Arrange, Act, Assert

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

### Running tests
### Avoid infinite loops

Never put potential infinite loops in the test code path. A typical
example is about gRPC's LongRunningOperations. Make sure you pass the
timeout parameter to the `result()` call.

Good:

```python
# will raise google.api_core.GoogleAPICallError after 60 seconds
operation.result(60)
```

Bad:

```python
operation.result() # this could wait forever.
```

We recommend the timeout parameter to be around the number that gives
you more than 90% success rate. Don't put too long a timeout.

Now this test is inevitably flaky, so consider marking the test as
`flaky` as follows:

```python

@pytest.mark.flaky(max_runs=3, min_passes=1)
def my_flaky_test():
# test that involves LRO poling with the timeout
```

This combination will give you very high success rate with fixed test
execution time (0.999 success rate and 180 seconds operation wait time
in the worst case in this example).

### Running tests

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


### Test Environment Setup

Because all tests are system tests that use live resources, running tests
Expand Down