Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ testpaths = ["tests"]
# minutes, dumping every thread's stack. Thread mode works on Windows too.
timeout = 120
timeout_method = "thread"
# Async tests and fixtures share one event loop, so session-scoped async
# clients, like the e2e tests' shared client, work in every test.
asyncio_default_test_loop_scope = "session"
asyncio_default_fixture_loop_scope = "session"
56 changes: 33 additions & 23 deletions tests/sandbox/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,29 @@ class _Shared:
"""When the run started creating it."""


# One client of each kind serves every test, so a run mints a sandbox token per
# client instead of per test, staying under the token endpoint's rate limit.
@pytest.fixture(scope="session")
def shared() -> Iterator[_Shared]:
def sync_client() -> Iterator[SandboxClient]:
_require_env()
client = _client()
with _client() as client:
yield client


@pytest_asyncio.fixture(scope="session")
async def async_client() -> AsyncIterator[AsyncSandboxClient]:
_require_env()
async with _async_client() as client:
yield client


@pytest.fixture(scope="session")
def shared(sync_client: SandboxClient) -> Iterator[_Shared]:
name = _unique()
started = datetime.now(UTC)
# The name is known before creating, so even a partial create is removed.
try:
created = client.create(
created = sync_client.create(
name=name,
region=_REGION,
labels=_LABELS,
Expand All @@ -121,39 +135,34 @@ def shared() -> Iterator[_Shared]:
assert created.info.status == "DEPLOYED"
except BaseException:
try:
client.delete(name=name)
sync_client.delete(name=name)
except SandboxAPIError as err:
# A create that never got through leaves nothing to delete. The
# original error is raised either way.
if err.status != 404:
print(f"failed to delete sandbox {name} after setup failed: {err}")
client.close()
raise
try:
yield _Shared(name=name, info=created.info, started=started)
finally:
try:
if KEEP_SANDBOX:
print(f"BASETEN_E2E_TEST_KEEP_SANDBOX set; leaving sandbox {name}")
else:
deleted = client.delete(name=name)
assert (deleted.name, deleted.status) == (name, "DELETING")
finally:
client.close()
if KEEP_SANDBOX:
print(f"BASETEN_E2E_TEST_KEEP_SANDBOX set; leaving sandbox {name}")
else:
deleted = sync_client.delete(name=name)
assert (deleted.name, deleted.status) == (name, "DELETING")


# Each test runs on both clients: the sync one's calls go to a thread, so test
# bodies stay async for both.
@pytest_asyncio.fixture(params=["sync", "async"])
async def client(
@pytest.fixture(params=["sync", "async"])
def client(
request: pytest.FixtureRequest,
) -> AsyncIterator[SandboxClient | AsyncSandboxClient]:
sync_client: SandboxClient,
async_client: AsyncSandboxClient,
) -> SandboxClient | AsyncSandboxClient:
if request.param == "async":
async with _async_client() as async_client:
yield async_client
else:
with _client() as sync_client:
yield sync_client
return async_client
return sync_client


@pytest.mark.asyncio
Expand Down Expand Up @@ -297,13 +306,14 @@ async def test_get_missing_fails(
assert info.value.code is not None


# Built from the record in hand, so it costs no request.
@pytest_asyncio.fixture
async def sandbox(
client: SandboxClient | AsyncSandboxClient, shared: _Shared
) -> Sandbox | AsyncSandbox:
if isinstance(client, SandboxClient):
return await asyncio.to_thread(client.get, name=shared.name)
return await client.get(name=shared.name)
return client.get(info=shared.info)
return await client.get(info=shared.info)


@pytest.mark.asyncio
Expand Down
Loading