@@ -133,3 +133,47 @@ async def on_event(data, topic):
133133 assert published .result
134134 # wait for finish trigger
135135 await asyncio .wait_for (finish .wait (), 5 )
136+
137+
138+ @pytest .mark .asyncio
139+ async def test_pub_sub_unsub (server ):
140+ """
141+ Check client can unsubscribe topic and subscribe again.
142+ """
143+ # finish trigger
144+ finish = asyncio .Event ()
145+ async with PubSubClient () as client :
146+
147+ async def on_event (data , topic ):
148+ assert data == DATA
149+ finish .set ()
150+
151+ # subscribe for the event
152+ client .subscribe (EVENT_TOPIC , on_event )
153+ # start listentining
154+ client .start_client (uri )
155+ # wait for the client to be ready to receive events
156+ await client .wait_until_ready ()
157+ # trigger the server via an HTTP route
158+ requests .get (trigger_url )
159+ # wait for finish trigger
160+ await asyncio .wait_for (finish .wait (), 5 )
161+ assert finish .is_set ()
162+
163+ # unsubscribe and see that we don't get a message
164+ finish .clear ()
165+ await client .unsubscribe (EVENT_TOPIC )
166+ requests .get (trigger_url )
167+ # wait for finish trigger which isn't coming
168+ with pytest .raises (asyncio .TimeoutError ) as excinfo :
169+ await asyncio .wait_for (finish .wait (), 5 )
170+ assert not finish .is_set ()
171+
172+ # subscribe again and observe that we get the trigger
173+ finish .clear ()
174+ await client .subscribe (EVENT_TOPIC , on_event )
175+ # trigger the server via an HTTP route
176+ requests .get (trigger_url )
177+ # wait for finish trigger
178+ await asyncio .wait_for (finish .wait (), 5 )
179+ assert finish .is_set ()
0 commit comments