This repository was archived by the owner on Aug 19, 2025. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1010
1111
1212class RedisBackend (BroadcastBackend ):
13- def __init__ (self , url : str ):
14- self ._conn = redis .Redis .from_url (url )
13+ _conn : redis .Redis
14+
15+ def __init__ (self , url : str | None = None , * , conn : redis .Redis | None = None ):
16+ if url is None :
17+ assert conn is not None , "conn must be provided if url is not"
18+ self ._conn = conn
19+ else :
20+ self ._conn = redis .Redis .from_url (url )
21+
1522 self ._pubsub = self ._conn .pubsub ()
1623 self ._ready = asyncio .Event ()
1724 self ._queue : asyncio .Queue [Event ] = asyncio .Queue ()
1825 self ._listener : asyncio .Task [None ] | None = None
1926
2027 async def connect (self ) -> None :
2128 self ._listener = asyncio .create_task (self ._pubsub_listener ())
22- await self ._pubsub .connect ()
29+ await self ._pubsub .connect () # type: ignore[no-untyped-call]
2330
2431 async def disconnect (self ) -> None :
25- await self ._pubsub .aclose ()
32+ await self ._pubsub .aclose () # type: ignore[no-untyped-call]
2633 await self ._conn .aclose ()
2734 if self ._listener is not None :
2835 self ._listener .cancel ()
Original file line number Diff line number Diff line change 44import typing
55
66import pytest
7+ from redis import asyncio as redis
78
89from broadcaster import Broadcast , BroadcastBackend , Event
910from broadcaster .backends .kafka import KafkaBackend
11+ from broadcaster .backends .redis import RedisBackend
1012
1113
1214class CustomBackend (BroadcastBackend ):
@@ -56,6 +58,23 @@ async def test_redis():
5658 assert event .message == "hello"
5759
5860
61+ @pytest .mark .asyncio
62+ async def test_redis_configured_client ():
63+ backend = RedisBackend (conn = redis .Redis .from_url ("redis://localhost:6379" ))
64+ async with Broadcast (backend = backend ) as broadcast :
65+ async with broadcast .subscribe ("chatroom" ) as subscriber :
66+ await broadcast .publish ("chatroom" , "hello" )
67+ event = await subscriber .get ()
68+ assert event .channel == "chatroom"
69+ assert event .message == "hello"
70+
71+
72+ @pytest .mark .asyncio
73+ async def test_redis_requires_url_or_connection ():
74+ with pytest .raises (AssertionError , match = "conn must be provided if url is not" ):
75+ RedisBackend ()
76+
77+
5978@pytest .mark .asyncio
6079async def test_redis_stream ():
6180 async with Broadcast ("redis-stream://localhost:6379" ) as broadcast :
You can’t perform that action at this time.
0 commit comments