Skip to content

Commit

Permalink
Merge pull request #45 from mraspaud/feature-donfig
Browse files Browse the repository at this point in the history
Use donfig for handling posttroll environment variables
  • Loading branch information
pnuu authored Oct 19, 2022
2 parents a2b6e69 + db33805 commit 66e1d80
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down
18 changes: 10 additions & 8 deletions posttroll/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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):
Expand Down
7 changes: 3 additions & 4 deletions posttroll/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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)
Expand Down
31 changes: 24 additions & 7 deletions posttroll/tests/test_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import versioneer


requirements = ['pyzmq', 'netifaces']
requirements = ['pyzmq', 'netifaces', "donfig"]


setup(name="posttroll",
Expand Down

0 comments on commit 66e1d80

Please sign in to comment.