mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Improve tests and connection class
This commit is contained in:
parent
c11808ecc5
commit
213139d4c6
@ -61,7 +61,9 @@ def connect(backend=None, host=None, port=None, name=None, max_tries=None,
|
||||
raise ConfigurationError('Error loading backend `{}`'.format(backend)) from exc
|
||||
|
||||
logger.debug('Connection: {}'.format(Class))
|
||||
return Class(host=host, port=port, dbname=dbname, replicaset=replicaset)
|
||||
return Class(host=host, port=port, dbname=dbname,
|
||||
max_tries=max_tries, connection_timeout=connection_timeout,
|
||||
replicaset=replicaset)
|
||||
|
||||
|
||||
class Connection:
|
||||
@ -142,3 +144,5 @@ class Connection:
|
||||
if attempt == self.max_tries:
|
||||
logger.critical('Cannot connect to the Database. Giving up.')
|
||||
raise ConnectionError() from exc
|
||||
else:
|
||||
break
|
||||
|
@ -3,6 +3,7 @@ import logging
|
||||
import rethinkdb as r
|
||||
|
||||
from bigchaindb import backend
|
||||
from bigchaindb.backend.exceptions import BackendError
|
||||
from bigchaindb.backend.changefeed import ChangeFeed
|
||||
from bigchaindb.backend.utils import module_dispatch_registrar
|
||||
from bigchaindb.backend.rethinkdb.connection import RethinkDBConnection
|
||||
@ -23,7 +24,7 @@ class RethinkDBChangeFeed(ChangeFeed):
|
||||
try:
|
||||
self.run_changefeed()
|
||||
break
|
||||
except (r.ReqlDriverError, r.ReqlOpFailedError) as exc:
|
||||
except BackendError as exc:
|
||||
logger.exception(exc)
|
||||
time.sleep(1)
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import rethinkdb as r
|
||||
|
||||
from bigchaindb.backend.connection import Connection
|
||||
from bigchaindb.backend.exceptions import ConnectionError
|
||||
from bigchaindb.backend.exceptions import ConnectionError, OperationError
|
||||
|
||||
|
||||
class RethinkDBConnection(Connection):
|
||||
@ -24,7 +24,10 @@ class RethinkDBConnection(Connection):
|
||||
:attr:`~.RethinkDBConnection.max_tries`.
|
||||
"""
|
||||
|
||||
return query.run(self.conn)
|
||||
try:
|
||||
return query.run(self.conn)
|
||||
except r.ReqlDriverError as exc:
|
||||
raise OperationError from exc
|
||||
|
||||
def _connect(self):
|
||||
"""Set a connection to RethinkDB.
|
||||
@ -39,4 +42,4 @@ class RethinkDBConnection(Connection):
|
||||
try:
|
||||
return r.connect(host=self.host, port=self.port, db=self.dbname)
|
||||
except r.ReqlDriverError as exc:
|
||||
raise ConnectionError() from exc
|
||||
raise ConnectionError from exc
|
||||
|
@ -1,6 +1,7 @@
|
||||
import time
|
||||
import multiprocessing as mp
|
||||
from threading import Thread
|
||||
from unittest import mock
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
@ -34,6 +35,7 @@ def test_run_a_simple_query():
|
||||
|
||||
def test_raise_exception_when_max_tries():
|
||||
from bigchaindb.backend import connect
|
||||
from bigchaindb.backend.exceptions import OperationError
|
||||
|
||||
class MockQuery:
|
||||
def run(self, conn):
|
||||
@ -41,28 +43,41 @@ def test_raise_exception_when_max_tries():
|
||||
|
||||
conn = connect()
|
||||
|
||||
with pytest.raises(r.ReqlDriverError):
|
||||
with pytest.raises(OperationError):
|
||||
conn.run(MockQuery())
|
||||
|
||||
|
||||
def test_reconnect_when_connection_lost():
|
||||
from bigchaindb.backend import connect
|
||||
|
||||
def raise_exception(*args, **kwargs):
|
||||
raise r.ReqlDriverError('mock')
|
||||
|
||||
conn = connect()
|
||||
original_connect = r.connect
|
||||
r.connect = raise_exception
|
||||
|
||||
def delayed_start():
|
||||
time.sleep(1)
|
||||
r.connect = original_connect
|
||||
with patch('rethinkdb.connect') as mock_connect:
|
||||
mock_connect.side_effect = [
|
||||
r.ReqlDriverError('mock'),
|
||||
original_connect()
|
||||
]
|
||||
|
||||
thread = Thread(target=delayed_start)
|
||||
query = r.expr('1')
|
||||
thread.start()
|
||||
assert conn.run(query) == '1'
|
||||
conn = connect()
|
||||
query = r.expr('1')
|
||||
assert conn.run(query) == '1'
|
||||
|
||||
|
||||
def test_reconnect_when_connection_lost_tries_n_times():
|
||||
from bigchaindb.backend import connect
|
||||
from bigchaindb.backend.exceptions import ConnectionError
|
||||
|
||||
with patch('rethinkdb.connect') as mock_connect:
|
||||
mock_connect.side_effect = [
|
||||
r.ReqlDriverError('mock'),
|
||||
r.ReqlDriverError('mock'),
|
||||
r.ReqlDriverError('mock')
|
||||
]
|
||||
|
||||
conn = connect(max_tries=3)
|
||||
query = r.expr('1')
|
||||
with pytest.raises(ConnectionError):
|
||||
assert conn.run(query) == '1'
|
||||
|
||||
|
||||
def test_changefeed_reconnects_when_connection_lost(monkeypatch):
|
||||
|
Loading…
x
Reference in New Issue
Block a user