1- import asyncio
2- from unittest .mock import AsyncMock , patch , MagicMock
1+ from unittest .mock import MagicMock , patch
32from bsv .fee_models .live_policy import LivePolicy
43
54# Reset the singleton instance before each test
@@ -10,10 +9,10 @@ def setup_function(_):
109def teardown_function (_ ):
1110 LivePolicy ._instance = None
1211
13- @patch ("bsv.fee_models.live_policy.default_http_client " , autospec = True )
12+ @patch ("bsv.fee_models.live_policy.default_sync_http_client " , autospec = True )
1413def test_parses_mining_fee (mock_http_client_factory ):
15- # Prepare the mocked DefaultHttpClient instance
16- mock_http_client = AsyncMock ()
14+ # Prepare the mocked SyncHttpClient instance
15+ mock_http_client = MagicMock ()
1716 mock_http_client_factory .return_value = mock_http_client
1817
1918 # Set up a mock response
@@ -35,15 +34,15 @@ def test_parses_mining_fee(mock_http_client_factory):
3534 )
3635
3736 # Execute and verify the result
38- rate = asyncio . run ( policy .current_rate_sat_per_kb () )
37+ rate = policy .current_rate_sat_per_kb ()
3938 assert rate == 20
4039 mock_http_client .get .assert_called_once ()
4140
4241
43- @patch ("bsv.fee_models.live_policy.default_http_client " , autospec = True )
42+ @patch ("bsv.fee_models.live_policy.default_sync_http_client " , autospec = True )
4443def test_cache_reused_when_valid (mock_http_client_factory ):
45- # Prepare the mocked DefaultHttpClient instance
46- mock_http_client = AsyncMock ()
44+ # Prepare the mocked SyncHttpClient instance
45+ mock_http_client = MagicMock ()
4746 mock_http_client_factory .return_value = mock_http_client
4847
4948 # Set up a mock response
@@ -60,25 +59,25 @@ def test_cache_reused_when_valid(mock_http_client_factory):
6059 )
6160
6261 # Call multiple times within the cache validity period
63- first_rate = asyncio . run ( policy .current_rate_sat_per_kb () )
64- second_rate = asyncio . run ( policy .current_rate_sat_per_kb () )
62+ first_rate = policy .current_rate_sat_per_kb ()
63+ second_rate = policy .current_rate_sat_per_kb ()
6564
6665 # Verify the results
6766 assert first_rate == 50
6867 assert second_rate == 50
6968 mock_http_client .get .assert_called_once ()
7069
7170
72- @patch ("bsv.fee_models.live_policy.default_http_client " , autospec = True )
71+ @patch ("bsv.fee_models.live_policy.default_sync_http_client " , autospec = True )
7372@patch ("bsv.fee_models.live_policy.logger.warning" )
7473def test_uses_cached_value_when_fetch_fails (mock_log , mock_http_client_factory ):
75- # Prepare the mocked DefaultHttpClient instance
76- mock_http_client = AsyncMock ()
74+ # Prepare the mocked SyncHttpClient instance
75+ mock_http_client = MagicMock ()
7776 mock_http_client_factory .return_value = mock_http_client
7877
7978 # Set up mock responses (success first, then failure)
8079 mock_http_client .get .side_effect = [
81- AsyncMock (json_data = {"data" : {"policy" : {"satPerKb" : 75 }}}),
80+ MagicMock (json_data = {"data" : {"policy" : {"satPerKb" : 75 }}}),
8281 Exception ("Network down" )
8382 ]
8483
@@ -89,15 +88,15 @@ def test_uses_cached_value_when_fetch_fails(mock_log, mock_http_client_factory):
8988 )
9089
9190 # The first execution succeeds
92- first_rate = asyncio . run ( policy .current_rate_sat_per_kb () )
91+ first_rate = policy .current_rate_sat_per_kb ()
9392 assert first_rate == 75
9493
9594 # Force invalidation of the cache
9695 with policy ._cache_lock :
9796 policy ._cache .fetched_at_ms -= 10
9897
9998 # The second execution uses the cache
100- second_rate = asyncio . run ( policy .current_rate_sat_per_kb () )
99+ second_rate = policy .current_rate_sat_per_kb ()
101100 assert second_rate == 75
102101
103102 # Verify that a log is recorded for cache usage
@@ -107,11 +106,11 @@ def test_uses_cached_value_when_fetch_fails(mock_log, mock_http_client_factory):
107106 mock_http_client .get .assert_called ()
108107
109108
110- @patch ("bsv.fee_models.live_policy.default_http_client " , autospec = True )
109+ @patch ("bsv.fee_models.live_policy.default_sync_http_client " , autospec = True )
111110@patch ("bsv.fee_models.live_policy.logger.warning" )
112111def test_falls_back_to_default_when_no_cache (mock_log , mock_http_client_factory ):
113- # Prepare the mocked DefaultHttpClient instance
114- mock_http_client = AsyncMock ()
112+ # Prepare the mocked SyncHttpClient instance
113+ mock_http_client = MagicMock ()
115114 mock_http_client_factory .return_value = mock_http_client
116115
117116 # Set up a mock response (always failing)
@@ -124,7 +123,7 @@ def test_falls_back_to_default_when_no_cache(mock_log, mock_http_client_factory)
124123 )
125124
126125 # Fallback value is returned during execution
127- rate = asyncio . run ( policy .current_rate_sat_per_kb () )
126+ rate = policy .current_rate_sat_per_kb ()
128127 assert rate == 9
129128
130129 # Verify that a log is recorded
@@ -135,11 +134,11 @@ def test_falls_back_to_default_when_no_cache(mock_log, mock_http_client_factory)
135134 mock_http_client .get .assert_called ()
136135
137136
138- @patch ("bsv.fee_models.live_policy.default_http_client " , autospec = True )
137+ @patch ("bsv.fee_models.live_policy.default_sync_http_client " , autospec = True )
139138@patch ("bsv.fee_models.live_policy.logger.warning" )
140139def test_invalid_response_triggers_fallback (mock_log , mock_http_client_factory ):
141- # Prepare the mocked DefaultHttpClient instance
142- mock_http_client = AsyncMock ()
140+ # Prepare the mocked SyncHttpClient instance
141+ mock_http_client = MagicMock ()
143142 mock_http_client_factory .return_value = mock_http_client
144143
145144 # Set up an invalid response
@@ -154,7 +153,7 @@ def test_invalid_response_triggers_fallback(mock_log, mock_http_client_factory):
154153 )
155154
156155 # Fallback value is returned due to the invalid response
157- rate = asyncio . run ( policy .current_rate_sat_per_kb () )
156+ rate = policy .current_rate_sat_per_kb ()
158157 assert rate == 3
159158
160159 # Verify that a log is recorded
0 commit comments