diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 51ad046..d460683 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -21,7 +21,7 @@ jobs: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | - pip install -U pytest pytest-cov pyzmq netifaces + pip install -U pytest pytest-cov pyzmq netifaces donfig - name: Install posttroll run: | pip install --no-deps -e . diff --git a/posttroll/__init__.py b/posttroll/__init__.py index db127d0..b77c34b 100644 --- a/posttroll/__init__.py +++ b/posttroll/__init__.py @@ -24,15 +24,17 @@ """Posttroll packages.""" +import logging +import os import sys - from datetime import datetime -import os -import logging -from .version import get_versions import zmq +from donfig import Config + +from .version import get_versions +config = Config('posttroll') context = {} logger = logging.getLogger(__name__) @@ -74,10 +76,10 @@ def strp_isoformat(strg): def _set_tcp_keepalive(socket): - _set_int_sockopt(socket, zmq.TCP_KEEPALIVE, os.environ.get("POSTTROLL_TCP_KEEPALIVE")) - _set_int_sockopt(socket, zmq.TCP_KEEPALIVE_CNT, os.environ.get("POSTTROLL_TCP_KEEPALIVE_CNT")) - _set_int_sockopt(socket, zmq.TCP_KEEPALIVE_IDLE, os.environ.get("POSTTROLL_TCP_KEEPALIVE_IDLE")) - _set_int_sockopt(socket, zmq.TCP_KEEPALIVE_INTVL, os.environ.get("POSTTROLL_TCP_KEEPALIVE_INTVL")) + _set_int_sockopt(socket, zmq.TCP_KEEPALIVE, config.get("tcp_keepalive", None)) + _set_int_sockopt(socket, zmq.TCP_KEEPALIVE_CNT, config.get("tcp_keepalive_cnt", None)) + _set_int_sockopt(socket, zmq.TCP_KEEPALIVE_IDLE, config.get("tcp_keepalive_idle", None)) + _set_int_sockopt(socket, zmq.TCP_KEEPALIVE_INTVL, config.get("tcp_keepalive_intvl", None)) def _set_int_sockopt(socket, param, value): diff --git a/posttroll/publisher.py b/posttroll/publisher.py index 0ec6216..05ffd21 100644 --- a/posttroll/publisher.py +++ b/posttroll/publisher.py @@ -35,6 +35,7 @@ from posttroll import _set_tcp_keepalive from posttroll.message import Message from posttroll.message_broadcaster import sendaddressservice +from posttroll import config LOGGER = logging.getLogger(__name__) @@ -99,10 +100,8 @@ def __init__(self, address, name="", min_port=None, max_port=None): # Limit port range or use the defaults when no port is defined # by the user - min_port = min_port or int(os.environ.get('POSTTROLL_PUB_MIN_PORT', - 49152)) - max_port = max_port or int(os.environ.get('POSTTROLL_PUB_MAX_PORT', - 65536)) + min_port = min_port or int(config.get('pub_min_port', 49152)) + max_port = max_port or int(config.get('pub_max_port', 65536)) # Check for port 0 (random port) u__ = urlsplit(self.destination) diff --git a/posttroll/tests/test_pubsub.py b/posttroll/tests/test_pubsub.py index 6b905b9..483e2ad 100644 --- a/posttroll/tests/test_pubsub.py +++ b/posttroll/tests/test_pubsub.py @@ -28,8 +28,11 @@ from datetime import timedelta from threading import Thread, Lock import time +from contextlib import contextmanager +import posttroll import pytest +from donfig import Config test_lock = Lock() @@ -312,13 +315,14 @@ def test_pub_minmax_port(self): # Set the port range to environment variables os.environ['POSTTROLL_PUB_MIN_PORT'] = str(port) os.environ['POSTTROLL_PUB_MAX_PORT'] = str(port + 1) - res = _get_port(min_port=None, max_port=None) - if res is False: - # The port wasn't free, try again - continue - # Port was selected, make sure it's within the "range" of one - self.assertEqual(res, port) - break + with posttroll.config.set(pub_min_port=str(port), pub_max_port=str(port + 1)): + res = _get_port(min_port=None, max_port=None) + if res is False: + # The port wasn't free, try another one + continue + # Port was selected, make sure it's within the "range" of one + self.assertEqual(res, port) + break # Using range of ports defined at instantation time, this # should override environment variables @@ -636,6 +640,17 @@ def tcp_keepalive_settings(monkeypatch): monkeypatch.setenv("POSTTROLL_TCP_KEEPALIVE_CNT", "10") monkeypatch.setenv("POSTTROLL_TCP_KEEPALIVE_IDLE", "1") monkeypatch.setenv("POSTTROLL_TCP_KEEPALIVE_INTVL", "1") + with reset_config_for_tests(): + yield + + +@contextmanager +def reset_config_for_tests(): + """Reset the config for testing.""" + old_config = posttroll.config + posttroll.config = Config("posttroll") + yield + posttroll.config = old_config @pytest.fixture @@ -645,6 +660,8 @@ def tcp_keepalive_no_settings(monkeypatch): monkeypatch.delenv("POSTTROLL_TCP_KEEPALIVE_CNT", raising=False) monkeypatch.delenv("POSTTROLL_TCP_KEEPALIVE_IDLE", raising=False) monkeypatch.delenv("POSTTROLL_TCP_KEEPALIVE_INTVL", raising=False) + with reset_config_for_tests(): + yield def test_publisher_tcp_keepalive(tcp_keepalive_settings): diff --git a/setup.py b/setup.py index b13e9e1..e80d8d6 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ import versioneer -requirements = ['pyzmq', 'netifaces'] +requirements = ['pyzmq', 'netifaces', "donfig"] setup(name="posttroll",