Skip to content

Commit

Permalink
Merge pull request #2 from i2mint/broker_signature
Browse files Browse the repository at this point in the history
feat: add helpful signature to RedisBroker. Closes #1
  • Loading branch information
valentin-feron authored Mar 22, 2024
2 parents a814f7e + 5d212fa commit 43a8acc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
22 changes: 22 additions & 0 deletions redisposted/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,35 @@
from typing import Any, Callable
from posted import MsgBrokerBase, NoMsg
from redis import Redis
from i2 import Sig

# ----------------------------------------------------------------------------
# Signature for the `RedisBroker` class

broker_sig = Sig(MsgBrokerBase.__init__) # signature of __init__ includes the self
redis_sig = Sig(Redis)
redis_posted_sig = broker_sig + redis_sig
redis_posted_sig = redis_posted_sig.ch_kinds(
**{ # here, we change all arguments (but self and kwargs) to be keyword-only
name: Sig.KEYWORD_ONLY
for name in redis_posted_sig.names
if name not in {'self', 'kwargs'}
}
)

# ----------------------------------------------------------------------------
# Redis message broker


class RedisBroker(MsgBrokerBase):
"""
Message broker for Redis.
"""

@redis_posted_sig
def __init__(self, **kwargs):
super().__init__(**kwargs)

def write(self, channel: str, message: Any):
encoded_msg = self._encoder(message)
if not self._redis.publish(channel, encoded_msg):
Expand Down
15 changes: 13 additions & 2 deletions redisposted/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,25 @@ def init_testing_data(broker: RedisBroker):


@pytest.mark.parametrize(
'message, channel', list(gen_test_mk_msg_broker_args()),
'message, channel',
list(gen_test_mk_msg_broker_args()),
)
def test_on_demand_consumption(broker: RedisBroker, message, channel):
base_test_on_demand_consumption(broker, message, channel)


@pytest.mark.parametrize(
'message, channel', list(gen_test_mk_msg_broker_args()),
'message, channel',
list(gen_test_mk_msg_broker_args()),
)
def test_reactive_consumption(broker: RedisBroker, message, channel):
base_test_reactive_consumption(broker, message, channel)


def test_redis_broker_signature():
from i2 import Sig
from redisposted.base import MsgBrokerBase, Redis

# The signature of the `RedisBroker` class should be the signature of the base
# broker class, with the `Redis` arguments added to it.
assert set(Sig(RedisBroker)) == (set(Sig(MsgBrokerBase)) | set(Sig(Redis)))
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ zip_safe = False
install_requires =
posted
redis==5.0.2
i2

0 comments on commit 43a8acc

Please sign in to comment.