added connection management fixes so that the connections and tests are exected in an reliable way

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2022-09-22 22:30:19 +02:00
parent 276335d2bd
commit bef98a8410
No known key found for this signature in database
7 changed files with 38 additions and 26 deletions

View File

@ -34,4 +34,5 @@ RUN mkdir -p /usr/src/app
COPY . /usr/src/app/ COPY . /usr/src/app/
WORKDIR /usr/src/app WORKDIR /usr/src/app
RUN pip install -e .[dev] RUN pip install -e .[dev]
RUN pip install base58 PyNaCl==1.4.0 pyasn1>=0.4.8 cryptography==3.4.7
RUN planetmint -y configure RUN planetmint -y configure

View File

@ -82,11 +82,11 @@ x = 'name: {}; score: {}'.format(name, n)
we use the `format()` version. The [official Python documentation says](https://docs.python.org/2/library/stdtypes.html#str.format), "This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting described in String Formatting Operations in new code." we use the `format()` version. The [official Python documentation says](https://docs.python.org/2/library/stdtypes.html#str.format), "This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting described in String Formatting Operations in new code."
## Running the Flake8 Style Checker ## Running the Black Style Checker
We use [Flake8](http://flake8.pycqa.org/en/latest/index.html) to check our Python code style. Once you have it installed, you can run it using: We use [Black](https://black.readthedocs.io/en/stable/) to check our Python code style. Once you have it installed, you can run it using:
```text ```text
flake8 --max-line-length 119 planetmint/ black --check -l 119 .
``` ```

View File

@ -7,7 +7,7 @@ import logging
import tarantool import tarantool
from planetmint.config import Config from planetmint.config import Config
from planetmint.transactions.common.exceptions import ConfigurationError from planetmint.transactions.common.exceptions import ConfigurationError, ConnectionError
from planetmint.utils import Lazy from planetmint.utils import Lazy
from planetmint.backend.connection import DBConnection from planetmint.backend.connection import DBConnection
@ -29,7 +29,8 @@ class TarantoolDBConnection(DBConnection):
dbconf = Config().get()["database"] dbconf = Config().get()["database"]
self.init_path = dbconf["init_config"]["absolute_path"] self.init_path = dbconf["init_config"]["absolute_path"]
self.drop_path = dbconf["drop_config"]["absolute_path"] self.drop_path = dbconf["drop_config"]["absolute_path"]
self.conn = self.connect() self.__conn = None
self.connect()
self.SPACE_NAMES = [ self.SPACE_NAMES = [
"abci_chains", "abci_chains",
"assets", "assets",
@ -46,7 +47,7 @@ class TarantoolDBConnection(DBConnection):
] ]
except tarantool.error.NetworkError as network_err: except tarantool.error.NetworkError as network_err:
logger.info("Host cant be reached") logger.info("Host cant be reached")
raise network_err raise ConnectionError
except ConfigurationError: except ConfigurationError:
logger.info("Exception in _connect(): {}") logger.info("Exception in _connect(): {}")
raise ConfigurationError raise ConfigurationError
@ -61,34 +62,39 @@ class TarantoolDBConnection(DBConnection):
return "".join(execute).encode() return "".join(execute).encode()
def connect(self): def connect(self):
return tarantool.connect(host=self.host, port=self.port) if not self.__conn:
self.__conn = tarantool.connect(host=self.host, port=self.port)
return self.__conn
def close(self): def close(self):
try: try:
self.conn.close() self.__conn.close()
self.conn = None self.__conn = None
except Exception as exc: except Exception as exc:
logger.info('Exception in planetmint.backend.tarantool.close(): {}'.format(exc)) logger.info('Exception in planetmint.backend.tarantool.close(): {}'.format(exc))
raise ConnectionError(str(exc)) from exc raise ConnectionError(str(exc)) from exc
def get_space(self, space_name: str): def get_space(self, space_name: str):
return self.conn.space(space_name) return self.get_connection().space(space_name)
def space(self, space_name: str): def space(self, space_name: str):
return self.query().space(space_name) return self.query().space(space_name)
def run(self, query, only_data=True): def run(self, query, only_data=True):
try: try:
return query.run(self.conn).data if only_data else query.run(self.conn) return query.run(self.get_connection()).data if only_data else query.run(self.get_connection())
except tarantool.error.OperationalError as op_error: except tarantool.error.OperationalError as op_error:
raise op_error raise op_error
except tarantool.error.NetworkError as net_error: except tarantool.error.NetworkError as net_error:
raise net_error raise net_error
def get_connection(self): def get_connection(self):
return self.conn if not self.__conn:
self.connect()
return self.__conn
def drop_database(self): def drop_database(self):
self.close()
db_config = Config().get()["database"] db_config = Config().get()["database"]
cmd_resp = self.run_command(command=self.drop_path, config=db_config) # noqa: F841 cmd_resp = self.run_command(command=self.drop_path, config=db_config) # noqa: F841

View File

@ -10,6 +10,9 @@ from planetmint.exceptions import BigchainDBError
class ConfigurationError(BigchainDBError): class ConfigurationError(BigchainDBError):
"""Raised when there is a problem with server configuration""" """Raised when there is a problem with server configuration"""
class ConnectionError(BigchainDBError):
"""Raised when there is a problem with server connection"""
class DatabaseDoesNotExist(BigchainDBError): class DatabaseDoesNotExist(BigchainDBError):

View File

@ -136,7 +136,12 @@ install_requires = [
'setproctitle==1.2.2', 'setproctitle==1.2.2',
'werkzeug==2.0.3', 'werkzeug==2.0.3',
'nest-asyncio==1.5.5', 'nest-asyncio==1.5.5',
'protobuf==3.20.1' 'protobuf==3.20.1',
"base58>=2.1.0",
"PyNaCl==1.4.0",
"pyasn1>=0.4.8",
"cryptography==3.4.7",
"zenroom==2.1.0.dev1655293214",
] ]
setup( setup(

View File

@ -7,15 +7,12 @@ import pytest
def test_get_connection_raises_a_configuration_error(monkeypatch): def test_get_connection_raises_a_configuration_error(monkeypatch):
from planetmint.transactions.common.exceptions import ConfigurationError from planetmint.transactions.common.exceptions import ConfigurationError, ConnectionError
from planetmint.backend.connection import Connection from planetmint.backend.connection import Connection
with pytest.raises(ConfigurationError): from planetmint.backend.localmongodb.connection import LocalMongoDBConnection
Connection('localhost', '1337', 'mydb', 'password', 'msaccess') from planetmint.backend.tarantool.connection import TarantoolDBConnection
with pytest.raises(ConfigurationError): with pytest.raises(ConnectionError):
# We need to force a misconfiguration here LocalMongoDBConnection('localhost', '1337', 'mydb', 'password')
monkeypatch.setattr('planetmint.backend.connection.BACKENDS', with pytest.raises(ConnectionError):
{'catsandra': TarantoolDBConnection('localhost', '1337', 'mydb', 'password')
'planetmint.backend.meowmeow.Catsandra'})
Connection('localhost', '1337', 'mydb', 'password', 'catsandra')

View File

@ -5,7 +5,7 @@
import base64 import base64
import json import json
import pytest
try: try:
from hashlib import sha3_256 from hashlib import sha3_256
except ImportError: except ImportError:
@ -53,7 +53,7 @@ SAMPLE_PUBLIC_KEY = {
} }
} }
@pytest.mark.skip
def test_convert_base64_public_key_to_address(): def test_convert_base64_public_key_to_address():
from planetmint.tendermint_utils import public_key64_to_address from planetmint.tendermint_utils import public_key64_to_address