Skip to content

Commit 50bfe69

Browse files
committed
Make unsubscribe test more comprehensive; add newsfragment
1 parent b187f8e commit 50bfe69

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

newsfragments/3595.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bugs related to subscription manager: ``run_forever`` can start with ``0`` subscriptions and remains alive, ``unsubscribe`` accepts single or multiple subs as objects or hexstrs, ``subscribe`` for many subs returns a list of hexstr ids.

tests/core/subscriptions/test_subscription_manager.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
AsyncMock,
55
)
66

7+
from eth_typing import (
8+
HexStr,
9+
)
710
import pytest_asyncio
811

912
from web3 import (
@@ -137,20 +140,51 @@ async def test_unsubscribe_all_clears_all_subscriptions(subscription_manager):
137140

138141

139142
@pytest.mark.asyncio
140-
async def test_unsubscribe_with_hex_ids(subscription_manager):
143+
async def test_unsubscribe_with_one_or_multiple(subscription_manager):
141144
sub1 = NewHeadsSubscription()
142145
sub2 = PendingTxSubscription()
143146
sub3 = NewHeadsSubscription()
144147
sub4 = LogsSubscription()
148+
sub5 = NewHeadsSubscription()
149+
sub6 = PendingTxSubscription()
150+
151+
(
152+
sub_id1,
153+
sub_id2,
154+
sub_id3,
155+
sub_id4,
156+
sub_id5,
157+
sub_id6,
158+
) = await subscription_manager.subscribe([sub1, sub2, sub3, sub4, sub5, sub6])
159+
assert sub_id1 == "0x0"
160+
assert sub_id2 == "0x1"
161+
assert sub_id3 == "0x2"
162+
assert sub_id4 == "0x3"
163+
assert sub_id5 == "0x4"
164+
assert sub_id6 == "0x5"
165+
166+
assert subscription_manager.subscriptions == [sub1, sub2, sub3, sub4, sub5, sub6]
167+
168+
# unsubscribe single by hex id
169+
assert await subscription_manager.unsubscribe(sub_id1) is True
170+
assert subscription_manager.subscriptions == [sub2, sub3, sub4, sub5, sub6]
145171

146-
sub_id1, sub_id2, sub_id3, sub_id4 = await subscription_manager.subscribe(
147-
[sub1, sub2, sub3, sub4]
148-
)
172+
# unsubscribe many by hex id
173+
assert await subscription_manager.unsubscribe([sub_id2, sub_id3]) is True
174+
assert subscription_manager.subscriptions == [sub4, sub5, sub6]
149175

150-
assert subscription_manager.subscriptions == [sub1, sub2, sub3, sub4]
176+
# unsubscribe non-existent hex id
177+
with pytest.raises(Web3ValueError, match=f"Subscription not found|{0x7}"):
178+
await subscription_manager.unsubscribe(HexStr("0x7"))
151179

152-
assert await subscription_manager.unsubscribe(sub_id1) is True
153-
assert subscription_manager.subscriptions == [sub2, sub3, sub4]
180+
# unsubscribe by subscription object
181+
assert await subscription_manager.unsubscribe(sub4) is True
182+
assert subscription_manager.subscriptions == [sub5, sub6]
154183

155-
assert await subscription_manager.unsubscribe([sub_id2, sub_id3]) is True
156-
assert subscription_manager.subscriptions == [sub4]
184+
# unsubscribe many by subscription object
185+
assert await subscription_manager.unsubscribe([sub5, sub6]) is True
186+
assert subscription_manager.subscriptions == []
187+
188+
# unsubscribe non-existent subscription object
189+
with pytest.raises(Web3ValueError, match=f"Subscription not found|{sub5.id}"):
190+
await subscription_manager.unsubscribe(sub5)

0 commit comments

Comments
 (0)