Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a possibility to create subscribers using a dictionary of configuration options #39

Merged
merged 8 commits into from
Apr 25, 2022

Conversation

pnuu
Copy link
Member

@pnuu pnuu commented Apr 8, 2022

Similarly to #35 , this PR adds the possibility to configure the subscriber using a dictionary of config options. In this way it is possible to disable nameserver connection in the ListenerContainer when it is not needed, or is otherwise undesirable.

The subscriber class without nameserver connection is selected by setting nameserver=False and giving a non-empty list of connection addresses. In all other cases the default NSSubscriber will be used.

This PR is required by pytroll/pytroll-collectors#102

@pnuu pnuu requested a review from mraspaud April 8, 2022 07:46
@pnuu pnuu self-assigned this Apr 8, 2022
@codecov
Copy link

codecov bot commented Apr 8, 2022

Codecov Report

Merging #39 (a8abf2f) into main (6f81e94) will increase coverage by 0.85%.
The diff coverage is 99.27%.

@@            Coverage Diff             @@
##             main      #39      +/-   ##
==========================================
+ Coverage   79.50%   80.36%   +0.85%     
==========================================
  Files          15       15              
  Lines        1888     1991     +103     
==========================================
+ Hits         1501     1600      +99     
- Misses        387      391       +4     
Impacted Files Coverage Δ
posttroll/subscriber.py 90.75% <96.77%> (-1.84%) ⬇️
posttroll/listener.py 89.47% <100.00%> (+1.97%) ⬆️
posttroll/publisher.py 97.67% <100.00%> (+0.77%) ⬆️
posttroll/tests/test_pubsub.py 96.11% <100.00%> (+0.89%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6f81e94...a8abf2f. Read the comment docs.

@pnuu
Copy link
Member Author

pnuu commented Apr 8, 2022

Found couple bugs when running tests locally. Fixing and adding some more tests.

@pnuu
Copy link
Member Author

pnuu commented Apr 8, 2022

Ok, the bugs were fixed. I've also tested this with segment_gatherer.py and it works as expected.

Copy link
Member

@mraspaud mraspaud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition, and thanks for taking the time to make flake8 happy. I have a few comments inline.

posttroll/subscriber.py Outdated Show resolved Hide resolved
posttroll/subscriber.py Outdated Show resolved Hide resolved
Comment on lines +452 to +478
def _get_subscriber_instance(settings):
addresses = settings['addresses']
topics = settings.get('topics', '')
message_filter = settings.get('message_filter', None)
translate = settings.get('translate', False)

return Subscriber(addresses, topics=topics, message_filter=message_filter, translate=translate)


def _get_nssubscriber_instance(settings):
services = settings.get('services', '')
topics = settings.get('topics', _MAGICK)
addr_listener = settings.get('addr_listener', False)
addresses = settings.get('addresses', None)
timeout = settings.get('timeout', 10)
translate = settings.get('translate', False)
nameserver = settings.get('nameserver', 'localhost') or 'localhost'

return NSSubscriber(
services=services,
topics=topics,
addr_listener=addr_listener,
addresses=addresses,
timeout=timeout,
translate=translate,
nameserver=nameserver
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about making these class method call eg from_dict_config?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm, what? I don't understand.

Copy link
Member

@mraspaud mraspaud Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of calling _get_substriber_instance, you would call Subscriber.from_dict_config. This can be implemented lik this:

# In the Subscriber class definition

@classmethod
def from_dict_config(cls, settings):
    addresses = settings['addresses']
    topics = settings.get('topics', '')
    message_filter = settings.get('message_filter', None)
    translate = settings.get('translate', False)

    return cls(addresses, topics=topics, message_filter=message_filter, translate=translate)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing that only relevant arguments are passed to the class init seems to be getting too complicate, so I'll keep these as separate methods.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the tests simpler?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. if the names of the kwargs in the Subscriber or NSSubscriber constructors changes, the tests you are referring to don't catch it, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, that's right. How about I change the tests so that I don't mock them, but neither do I assert anything. That way we see that the kwargs truly match. I'll just mock NSSubscriber.start() so that it isn't started for nothing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That wouldn't catch if new kwargs were added, but better that than the current version of the tests...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First iteration pushed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. indeed, since the replacement of the calls to the NSSubscriber with create_subscriber_from_dict_config, the latter is tested end-to-end in behaviour with the tests you mention in your last comment. That means that the new/refactored code is effectively tested already.
    The other tests that you mentionned earlier still only test implementation IMO. They rely on the fact that the new functions end up calling a given constructor, while we might change that to a unpickling (yaml loading) or class methods in the future. In other words, according to me, these tests are not useful and should be deleted.

That would then open for having class methods as mentioned earlier.

posttroll/listener.py Show resolved Hide resolved
posttroll/listener.py Outdated Show resolved Hide resolved
Copy link
Member

@mraspaud mraspaud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pnuu pnuu merged commit a3a180e into pytroll:main Apr 25, 2022
@pnuu pnuu deleted the feature-subscriber-dict-config branch April 25, 2022 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants