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

api: Add consume_first for convenience #502

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions leapp/actors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,35 @@ def produce(self, *models):
'explicitely in the actor\'s "produces" tuple. The message will be ignored'.format(
type(model)))

def consume_first_default(self, model):
"""
Retrieves the first message found as specified in the actors :py:attr:`consumes` attribute, and filter message
types by model. This additionally returns as fallback value an instance of model with the default
initialization

:param model: Model to use as a filter for the messages to return
:type model: A derived class from :py:class:`leapp.models.Model`
:return: The first message of the specified model produced by other a fallback instance of model
:rtype: The message or a default initialized instance of the model type.
"""
return self.consume_first(model, default=model())

def consume_first(self, model, default=None):
"""
Retrieves the first message found as specified in the actors :py:attr:`consumes` attribute, and filter message
types by model.

:param model: Model to use as a filter for the messages to return
:type model: A derived class from :py:class:`leapp.models.Model`
:param default: Fallback value in case there are no messages
:type default: Any
:return: The first message of the specified model produced by other actors or the value passed as `default`
:rtype: The message or the value passed as ``default``
"""
# One cannot iterate over tuples, so we make a generator that iterates over the value returned by self.consume.
# This way we can workaround both cases.
return next((message for message in self.consume(model)), default)

def consume(self, *models):
"""
Retrieve messages specified in the actors :py:attr:`consumes` attribute, and filter message types by
Expand Down
29 changes: 29 additions & 0 deletions leapp/libraries/stdlib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ def produce(*model_instances):
return current_actor().produce(*model_instances)


def consume_first_default(model):
"""
Retrieves the first message found as specified in the actors :py:attr:`consumes` attribute, and filter message
types by model. This additionally returns as fallback value an instance of model with the default
initialization

:param model: Model to use as a filter for the messages to return
:type model: A derived class from :py:class:`leapp.models.Model`
:return: The first message of the specified model produced by other a fallback instance of model
:rtype: The message or a default initialized instance of the model type.
"""
return current_actor().consume_first_default(model)


def consume_first(model, default=None):
"""
Retrieves the first message found as specified in the actors :py:attr:`consumes` attribute, and filter message
types by model.

:param model: Model to use as a filter for the messages to return
:type model: A derived class from :py:class:`leapp.models.Model`
:param default: Fallback value in case there are no messages
:type default: Any
:return: The first message of the specified model produced by other actors or the value passed as `default`
:rtype: The message or the value passed as ``default``
"""
return current_actor().consume_first(model=model, default=default)


def consume(*models):
"""
Retrieve messages specified in the actors :py:attr:`consumes` attribute, and filter message types by
Expand Down
2 changes: 1 addition & 1 deletion tests/data/actor-api-tests/models/apitest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class ApiTest(Model):
topic = ApiTestTopic

data = fields.String()
data = fields.String(default='not-filled')


class ApiTestProduce(ApiTest):
Expand Down
11 changes: 11 additions & 0 deletions tests/scripts/test_actor_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,24 @@ def test_actor_messaging_paths(leapp_forked, repository, actor_name):
messaging = _TestableMessaging()
with _with_loaded_actor(repository, actor_name, messaging) as (_unused, actor):
from leapp.models import ApiTestConsume, ApiTestProduce
assert len(list(actor.consume(ApiTestConsume))) == 0
assert next(actor.consume(ApiTestConsume), None) is None
assert actor.consume_first(ApiTestConsume) is None
assert api.consume_first(ApiTestConsume) is None
assert api.consume_first_default(ApiTestConsume).data == 'not-filled'
assert actor.consume_first_default(ApiTestConsume).data == 'not-filled'
assert actor.consume_first(ApiTestConsume, default='default') == 'default'
assert api.consume_first(ApiTestConsume, default='default') == 'default'

messaging.feed(ApiTestConsume(data='prefilled'), actor)

assert len(list(actor.consume(ApiTestConsume))) == 1
assert next(actor.consume(ApiTestConsume)).data == 'prefilled'
assert actor.consume_first(ApiTestConsume).data == 'prefilled'

assert len(list(api.consume(ApiTestConsume))) == 1
assert next(api.consume(ApiTestConsume)).data == 'prefilled'
assert api.consume_first(ApiTestConsume).data == 'prefilled'

actor_message = 'Actor {} sent message via Actor'.format(actor_name)
api_message = 'Actor {} sent message via API'.format(actor_name)
Expand Down