7
7
TYPE_CHECKING ,
8
8
Any ,
9
9
Dict ,
10
+ Generator ,
10
11
List ,
11
12
Tuple ,
12
13
Union ,
@@ -174,7 +175,7 @@ async def emit_contract_event(
174
175
acct : ChecksumAddress ,
175
176
contract_function : "AsyncContractFunction" ,
176
177
args : Any = (),
177
- delay : float = 0.1 ,
178
+ delay : float = 0.25 ,
178
179
) -> None :
179
180
await asyncio .sleep (delay )
180
181
tx_hash = await contract_function (* args ).transact ({"from" : acct })
@@ -213,7 +214,21 @@ def assert_no_subscriptions_left(sub_container: "SubscriptionContainer") -> None
213
214
assert len (sub_container .handler_subscriptions ) == 0
214
215
215
216
217
+ async def clean_up_task (task : "asyncio.Task[Any]" ) -> None :
218
+ task .cancel ()
219
+ try :
220
+ await task
221
+ except asyncio .CancelledError :
222
+ pass
223
+
224
+
216
225
class PersistentConnectionProviderTest :
226
+ @pytest .fixture (autouse = True )
227
+ def clear_caches (self , async_w3 : AsyncWeb3 ) -> Generator [None , None , None ]:
228
+ yield
229
+ async_w3 .provider ._request_processor .clear_caches ()
230
+ async_w3 .subscription_manager .total_handler_calls = 0
231
+
217
232
@staticmethod
218
233
async def seed_transactions_to_geth (
219
234
async_w3 : AsyncWeb3 ,
@@ -318,9 +333,6 @@ async def test_async_eth_subscribe_syncing_mocked(
318
333
async_w3 .subscription_manager ._subscription_container
319
334
)
320
335
321
- # cleanup
322
- async_w3 .provider ._request_processor .clear_caches ()
323
-
324
336
@pytest .mark .asyncio
325
337
async def test_async_eth_subscribe_new_heads (self , async_w3 : AsyncWeb3 ) -> None :
326
338
sub_id = await async_w3 .eth .subscribe ("newHeads" )
@@ -365,11 +377,8 @@ async def test_async_eth_subscribe_creates_and_handles_new_heads_subscription_ty
365
377
assert sub_manager .total_handler_calls == 1
366
378
assert sub .handler_call_count == 1
367
379
368
- # cleanup
369
- sub_manager .total_handler_calls = 0
370
-
371
380
@pytest .mark .asyncio
372
- async def test_async_eth_subscribe_new_and_process_pending_tx_true (
381
+ async def test_async_eth_subscribe_process_pending_tx_true (
373
382
self ,
374
383
async_w3 : AsyncWeb3 ,
375
384
) -> None :
@@ -409,7 +418,7 @@ async def test_async_eth_subscribe_new_and_process_pending_tx_true(
409
418
)
410
419
async_w3 .provider ._request_processor .clear_caches ()
411
420
await async_w3 .eth .wait_for_transaction_receipt (tx_hash )
412
- tx_seeder_task . cancel ( )
421
+ await clean_up_task ( tx_seeder_task )
413
422
414
423
@pytest .mark .asyncio
415
424
async def test_async_eth_subscribe_and_process_pending_tx_false (
@@ -446,7 +455,7 @@ async def test_async_eth_subscribe_and_process_pending_tx_false(
446
455
async_w3 .subscription_manager ._subscription_container
447
456
)
448
457
await async_w3 .eth .wait_for_transaction_receipt (tx_hash )
449
- tx_seeder_task . cancel ( )
458
+ await clean_up_task ( tx_seeder_task )
450
459
451
460
@pytest .mark .asyncio
452
461
async def test_async_eth_subscribe_creates_and_handles_pending_tx_subscription_type (
@@ -472,9 +481,8 @@ async def test_async_eth_subscribe_creates_and_handles_pending_tx_subscription_t
472
481
accts = await async_w3 .eth .accounts
473
482
acct = accts [0 ]
474
483
tx_seeder_task = asyncio .create_task (
475
- self .seed_transactions_to_geth (async_w3 , acct , num_txs = 1 , delay = 0.5 )
484
+ self .seed_transactions_to_geth (async_w3 , acct )
476
485
)
477
-
478
486
await sub_manager .handle_subscriptions ()
479
487
480
488
assert pending_tx_handler_test .passed
@@ -485,7 +493,7 @@ async def test_async_eth_subscribe_creates_and_handles_pending_tx_subscription_t
485
493
486
494
# cleanup
487
495
sub_manager .total_handler_calls = 0
488
- tx_seeder_task . cancel ( )
496
+ await clean_up_task ( tx_seeder_task )
489
497
490
498
@pytest .mark .asyncio
491
499
async def test_async_eth_subscribe_and_process_logs (
@@ -523,7 +531,7 @@ async def test_async_eth_subscribe_and_process_logs(
523
531
524
532
assert await async_w3 .eth .unsubscribe (sub_id )
525
533
assert len (async_w3 .subscription_manager .subscriptions ) == 0
526
- emit_event_task . cancel ( )
534
+ await clean_up_task ( emit_event_task )
527
535
528
536
@pytest .mark .asyncio
529
537
async def test_async_eth_subscribe_creates_and_handles_logs_subscription_type (
@@ -571,7 +579,7 @@ async def test_async_eth_subscribe_creates_and_handles_logs_subscription_type(
571
579
572
580
# cleanup
573
581
sub_manager .total_handler_calls = 0
574
- emit_event_task . cancel ( )
582
+ await clean_up_task ( emit_event_task )
575
583
576
584
@pytest .mark .asyncio
577
585
async def test_async_extradata_poa_middleware_on_eth_subscription (
@@ -731,7 +739,6 @@ async def test_async_subscription_manager_subscribes_to_many_subscriptions(
731
739
subs = sub_manager .subscriptions
732
740
733
741
await sub_manager .handle_subscriptions ()
734
- emit_event_task .cancel ()
735
742
736
743
# assert unsubscribed and removed subscriptions
737
744
assert len (sub_manager .subscriptions ) == 0
@@ -745,6 +752,7 @@ async def test_async_subscription_manager_subscribes_to_many_subscriptions(
745
752
746
753
# cleanup
747
754
sub_manager .total_handler_calls = 0
755
+ await clean_up_task (emit_event_task )
748
756
749
757
@pytest .mark .asyncio
750
758
async def test_subscription_handler_context (self , async_w3 : AsyncWeb3 ) -> None :
@@ -764,9 +772,10 @@ async def test_sub_handler(
764
772
assert handler_context .str2 == "bar"
765
773
766
774
handler_context .handler_test .passed = True
767
- await handler_context .subscription .unsubscribe ()
775
+ unsubscribed = await handler_context .subscription .unsubscribe ()
776
+ assert unsubscribed
768
777
769
- await async_w3 .eth .subscribe (
778
+ subscribed = await async_w3 .eth .subscribe (
770
779
"newHeads" ,
771
780
label = "foo" ,
772
781
handler = test_sub_handler ,
@@ -779,6 +788,7 @@ async def test_sub_handler(
779
788
"handler_test" : handler_test ,
780
789
},
781
790
)
791
+ assert is_hexstr (subscribed )
782
792
783
793
sub_manager = async_w3 .subscription_manager
784
794
@@ -853,13 +863,17 @@ async def unsubscribe_subs(
853
863
sub_manager = async_w3 .subscription_manager
854
864
sub1 = NewHeadsSubscription (label = "foo" , handler = idle_handler )
855
865
sub2 = LogsSubscription (label = "bar" , handler = idle_handler )
866
+
856
867
await sub_manager .subscribe ([sub1 , sub2 ])
868
+
857
869
assert sub_manager .subscriptions == [sub1 , sub2 ]
858
870
859
- asyncio .create_task (unsubscribe_subs ([sub1 , sub2 ]))
871
+ unsubscribe_task = asyncio .create_task (unsubscribe_subs ([sub1 , sub2 ]))
860
872
# With no subscriptions in the queue, ``handle_subscriptions`` should hang
861
873
# indefinitely. Test that when the last subscription is unsubscribed from,
862
874
# the method breaks out of the loop. This is done via a raised
863
875
# ``SubscriptionProcessingFinished`` within the ``TaskReliantQueue``.
864
876
await sub_manager .handle_subscriptions ()
877
+
865
878
assert_no_subscriptions_left (sub_manager ._subscription_container )
879
+ await clean_up_task (unsubscribe_task )
0 commit comments