Skip to content

Commit

Permalink
[bigtable] fix: wrap sample invocations with retries [(#3494)](Google…
Browse files Browse the repository at this point in the history
…CloudPlatform/python-docs-samples#3494)

fix #3070

Also added `BIGTABLE_INSTANCE` to testing/test-env.tmpl.sh
  • Loading branch information
Takashi Matsuo committed Apr 24, 2020
1 parent 43635d7 commit 59a8091
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions samples/snippets/writes/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
backoff==1.10.0
pytest==5.3.2
25 changes: 21 additions & 4 deletions samples/snippets/writes/writes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import uuid
import pytest

import backoff
from google.api_core.exceptions import DeadlineExceeded
from google.cloud import bigtable

from .write_batch import write_batch
Expand Down Expand Up @@ -55,22 +57,37 @@ def table_id(bigtable_instance):


def test_writes(capsys, table_id):
write_simple(PROJECT, BIGTABLE_INSTANCE, table_id)

# `row.commit()` sometimes ends up with DeadlineExceeded, so now
# we put retries with a hard deadline.
@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=60)
def _write_simple():
write_simple(PROJECT, BIGTABLE_INSTANCE, table_id)

_write_simple()
out, _ = capsys.readouterr()
assert 'Successfully wrote row' in out

write_increment(PROJECT, BIGTABLE_INSTANCE, table_id)
@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=60)
def _write_increment():
write_increment(PROJECT, BIGTABLE_INSTANCE, table_id)

_write_increment()
out, _ = capsys.readouterr()
assert 'Successfully updated row' in out

write_conditional(PROJECT, BIGTABLE_INSTANCE, table_id)
@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=60)
def _write_conditional():
write_conditional(PROJECT, BIGTABLE_INSTANCE, table_id)

_write_conditional()
out, _ = capsys.readouterr()
assert 'Successfully updated row\'s os_name' in out

write_batch(PROJECT, BIGTABLE_INSTANCE, table_id)
@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=60)
def _write_batch():
write_batch(PROJECT, BIGTABLE_INSTANCE, table_id)

_write_batch()
out, _ = capsys.readouterr()
assert 'Successfully wrote 2 rows' in out

0 comments on commit 59a8091

Please sign in to comment.