From a377fd53050e208d195dbea89cbd7a977b2e5cfb Mon Sep 17 00:00:00 2001 From: Noah Levitt Date: Tue, 22 Sep 2015 21:34:12 +0000 Subject: [PATCH] only retry r.ReqlTimeoutError because creating a database with too many replicas or shards raises a r.ReqlAvailabilityError, which resulted in an infinite loop; add test for this case --- rethinkstuff/__init__.py | 8 ++++---- tests/test_rethinker.py | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/rethinkstuff/__init__.py b/rethinkstuff/__init__.py index c6b23ed..fb50612 100644 --- a/rethinkstuff/__init__.py +++ b/rethinkstuff/__init__.py @@ -21,7 +21,7 @@ class RethinkerWrapper: conn = self.rethinker._random_server_connection() is_iter = False try: - result = self.wrapped.run(conn, db=db or self.rethinker.db) + result = self.wrapped.run(conn, db=db or self.rethinker.dbname) if hasattr(result, "__next__"): is_iter = True def gen(): @@ -41,8 +41,8 @@ class RethinkerWrapper: return g else: return result - except (r.ReqlAvailabilityError, r.ReqlTimeoutError) as e: - pass + except r.ReqlTimeoutError as e: + time.sleep(0.5) finally: if not is_iter: conn.close(noreply_wait=False) @@ -56,7 +56,7 @@ class Rethinker(object): def __init__(self, servers=['localhost'], db=None): self.servers = servers - self.db = db + self.dbname = db # https://github.com/rethinkdb/rethinkdb-example-webpy-blog/blob/master/model.py # "Best practices: Managing connections: a connection per request" diff --git a/tests/test_rethinker.py b/tests/test_rethinker.py index 33f9749..85877af 100644 --- a/tests/test_rethinker.py +++ b/tests/test_rethinker.py @@ -3,6 +3,8 @@ import logging import sys import types import gc +import pytest +import rethinkdb logging.basicConfig(stream=sys.stderr, level=logging.INFO, format="%(asctime)s %(process)d %(levelname)s %(threadName)s %(name)s.%(funcName)s(%(filename)s:%(lineno)d) %(message)s") @@ -64,3 +66,8 @@ def test_rethinker(): # connection should be closed after result is garbage-collected assert not r.last_conn.is_open() + with pytest.raises(rethinkdb.errors.ReqlOpFailedError): + r.table_create("too_many_replicas", replicas=99).run() + with pytest.raises(rethinkdb.errors.ReqlOpFailedError): + r.table_create("too_many_shards", shards=99).run() +