Skip to content

Commit b4a3a9d

Browse files
committed
chore: use time.sleep in rest tests
1 parent 202e0e8 commit b4a3a9d

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

ably/scripts/unasync.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,50 @@ def _replace_import(self, tokens, token_counter, new_tokens: list):
182182

183183
full_lib_name = ''
184184
lib_name_counter = token_counter + 2
185-
if len(_IMPORTS_REPLACE.keys()) == 0:
186-
return lib_name_counter
187-
188-
while True:
189-
if tokens[lib_name_counter].src == " ":
190-
break
185+
186+
# Handle special case for "from asyncio import sleep" -> "from time import sleep"
187+
# First check if this is an asyncio import with just sleep
188+
if (lib_name_counter < len(tokens) and
189+
tokens[lib_name_counter].src == "asyncio"):
190+
191+
# Look ahead to see if it's "import sleep" specifically
192+
check_counter = lib_name_counter + 1
193+
# Skip whitespace
194+
while check_counter < len(tokens) and tokens[check_counter].src == " ":
195+
check_counter += 1
196+
# Check if next is "import"
197+
if check_counter < len(tokens) and tokens[check_counter].src == "import":
198+
check_counter += 1
199+
# Skip whitespace after "import"
200+
while check_counter < len(tokens) and tokens[check_counter].src == " ":
201+
check_counter += 1
202+
# Check if next is exactly "sleep"
203+
if (check_counter < len(tokens) and tokens[check_counter].src == "sleep"):
204+
# Check what comes after "sleep" to determine if this is a standalone sleep import
205+
next_check = check_counter + 1
206+
is_sleep_only = True
207+
208+
# Skip any whitespace
209+
while next_check < len(tokens) and tokens[next_check].src == " ":
210+
next_check += 1
211+
212+
if next_check < len(tokens):
213+
next_token = tokens[next_check]
214+
# If there's a comma, "as", or other import items, it's not a standalone sleep import
215+
if next_token.src in [",", "as"] or next_token.name == "NAME":
216+
is_sleep_only = False
217+
218+
if is_sleep_only:
219+
# Replace "asyncio" with "time" only for standalone "sleep" import
220+
new_tokens.append(tokenize_rt.Token("NAME", "time"))
221+
return lib_name_counter + 1 # Skip past "asyncio"
222+
223+
# Parse the full module name for other cases
224+
while lib_name_counter < len(tokens) and tokens[lib_name_counter].src != " ":
191225
full_lib_name = full_lib_name + tokens[lib_name_counter].src
192226
lib_name_counter = lib_name_counter + 1
193-
227+
228+
# Handle existing _IMPORTS_REPLACE logic
194229
for key, value in _IMPORTS_REPLACE.items():
195230
if key in full_lib_name:
196231
updated_lib_name = full_lib_name.replace(key, value)
@@ -201,7 +236,14 @@ def _replace_import(self, tokens, token_counter, new_tokens: list):
201236
new_tokens.pop()
202237
return lib_name_counter
203238

204-
lib_name_counter = token_counter + 2
239+
# For all other cases, add the original module name tokens
240+
for lib_name_part in full_lib_name.split("."):
241+
lib_name_part = self._class_rename(lib_name_part)
242+
new_tokens.append(tokenize_rt.Token("NAME", lib_name_part))
243+
new_tokens.append(tokenize_rt.Token("OP", "."))
244+
if full_lib_name: # Only remove the last dot if we added tokens
245+
new_tokens.pop()
246+
205247
return lib_name_counter
206248

207249
def _class_rename(self, name):

test/ably/rest/restchannelpublish_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import mock
1010
import msgpack
1111
import pytest
12-
import asyncio
1312

1413
from ably import api_version
1514
from ably import AblyException, IncompatibleClientIdException
@@ -18,6 +17,7 @@
1817
from ably.types.tokendetails import TokenDetails
1918
from ably.util import case
2019
from test.ably import utils
20+
from asyncio import sleep
2121

2222
from test.ably.testapp import TestApp
2323
from test.ably.utils import VaryByProtocolTestsMetaclass, dont_vary_protocol, BaseAsyncTestCase
@@ -404,7 +404,7 @@ async def test_interoperability(self):
404404
# 1)
405405
await channel.publish(data=expected_value)
406406
# temporary added delay, we need to investigate why messages don't appear immediately
407-
await asyncio.sleep(1)
407+
await sleep(1)
408408
async with httpx.AsyncClient(http2=True) as client:
409409
r = await client.get(url, auth=auth)
410410
item = r.json()[0]

0 commit comments

Comments
 (0)