-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Make get_pings include locally defined services #83
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,7 @@ def scope(): | |
|
||
|
||
class TestServicePing: | ||
|
||
def test_ping(self, registry, container, close_me): | ||
""" | ||
Calling ping instantiates the service using its factory, appends it to | ||
|
@@ -137,3 +138,74 @@ def factory(): | |
assert close_me.is_closed | ||
assert not container._instantiated | ||
assert not container._on_close | ||
|
||
def test_local_pings_are_retrieved(self, registry, container): | ||
""" | ||
Registering a local factory with a ping defined should make it possible to | ||
invoke a ping for that service. | ||
""" | ||
|
||
def another_service_factory(): | ||
yield AnotherService() | ||
|
||
another_service_ping = Mock(spec_set=["__call__"]) | ||
container.register_local_factory( | ||
AnotherService, another_service_factory, ping=another_service_ping | ||
) | ||
|
||
(svc_ping,) = container.get_pings() | ||
svc_ping.ping() | ||
|
||
another_service_ping.assert_called_once() | ||
|
||
def test_local_pings_override_global_pings(self, registry, container): | ||
""" | ||
If a local factory overwrites an existing, global one, and the local factory has a ping | ||
defined, the local ping should be used. | ||
""" | ||
|
||
def another_service_factory(): | ||
yield AnotherService() | ||
|
||
another_service_ping = Mock(spec_set=["__call__"]) | ||
local_another_service_ping = Mock(spec_set=["__call__"]) | ||
registry.register_factory( | ||
AnotherService, another_service_factory, ping=another_service_ping | ||
) | ||
container.register_local_factory( | ||
AnotherService, | ||
another_service_factory, | ||
ping=local_another_service_ping, | ||
) | ||
|
||
(svc_ping,) = container.get_pings() | ||
svc_ping.ping() | ||
|
||
another_service_ping.assert_not_called() | ||
local_another_service_ping.assert_called_once() | ||
|
||
def test_local_services_without_pings_do_not_discard_global_pings( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assumed such behavior would be the most "predictable" from the end-user perspective, but I was not quite sure about this change. Would you find it valid to make it work like that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah hm this is hard. There's two contradicting issues here:
But more specifically, it's the decision of the user to add a new definition of a service with a ping. I feel like discarding follows the wish of the user? They can define a new type based on the old type if they want to preserve the old definition. Given that svcs core concept are types, I feel like we should respect it when a user says "Connection is now this new thing." But don't change anything, yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, so regardless of whether a new definition contains a ping, the latest defined service (could be local) must be used? |
||
self, registry, container | ||
): | ||
""" | ||
If a local factory overwrites an existing, global one, but the local factory does not have a ping | ||
defined, the global ping should be used. | ||
""" | ||
|
||
def another_service_factory(): | ||
yield AnotherService() | ||
|
||
another_service_ping = Mock(spec_set=["__call__"]) | ||
registry.register_factory( | ||
AnotherService, another_service_factory, ping=another_service_ping | ||
) | ||
container.register_local_factory( | ||
AnotherService, | ||
another_service_factory, | ||
ping=None, | ||
) | ||
|
||
(svc_ping,) = container.get_pings() | ||
svc_ping.ping() | ||
|
||
another_service_ping.assert_called_once() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would appreciate an input - whether adding this method is OK and if so, where / how should it get documented
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this looks like #66, it's worth adding, but it might better as a separate PR for the reason alone that I'm not sure how long this one will take and it seems less controversial/decision-heavy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this method only be included in another PR? Or should registered services inspection be more extensive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, adding
__iter__
in a separate PR is what I meant. Two changelog entries are a strong indicator that a PR is doing too much. 🤓I also do think that the inspection could use more knobs, but this would be a start.