From 238887b03dffccd8e71eecf7cf7510a4195fa912 Mon Sep 17 00:00:00 2001 From: Jakub Novak Date: Fri, 17 Jan 2025 14:23:48 -0800 Subject: [PATCH] Add retry block to host and stdin tests --- .../tests/sandbox/commands/sendStdin.test.ts | 23 +++++++++++++++++++ packages/js-sdk/tests/sandbox/host.test.ts | 12 ++++++++-- .../tests/async/sandbox_async/test_host.py | 17 ++++++++------ .../tests/sync/sandbox_sync/test_host.py | 11 ++++++--- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts b/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts index 550c04a15..56bcd025a 100644 --- a/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts +++ b/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts @@ -7,6 +7,14 @@ sandboxTest('send stdin to process', async ({ sandbox }) => { await sandbox.commands.sendStdin(cmd.pid, text) + + for (let i = 0; i < 5; i++) { + if (cmd.stdout === text) { + break + } + await new Promise((r) => setTimeout(r, 500)) + } + await cmd.kill() assert.equal(cmd.stdout, text) @@ -29,6 +37,13 @@ sandboxTest('send special characters to stdin', async ({ sandbox }) => { await sandbox.commands.sendStdin(cmd.pid, text) + for (let i = 0; i < 5; i++) { + if (cmd.stdout === text) { + break + } + await new Promise((r) => setTimeout(r, 500)) + } + await cmd.kill() assert.equal(cmd.stdout, text) @@ -40,7 +55,15 @@ sandboxTest('send multiline string to stdin', async ({ sandbox }) => { await sandbox.commands.sendStdin(cmd.pid, text) + for (let i = 0; i < 5; i++) { + if (cmd.stdout === text) { + break + } + await new Promise((r) => setTimeout(r, 500)) + } + await cmd.kill() + assert.equal(cmd.stdout, text) }) diff --git a/packages/js-sdk/tests/sandbox/host.test.ts b/packages/js-sdk/tests/sandbox/host.test.ts index f499f7d3e..b0a176362 100644 --- a/packages/js-sdk/tests/sandbox/host.test.ts +++ b/packages/js-sdk/tests/sandbox/host.test.ts @@ -10,8 +10,16 @@ sandboxTest('ping server in sandbox', async ({ sandbox }) => { const host = sandbox.getHost(8000) - const res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + let res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + for (let i = 0; i < 10; i++) { + if (res.status === 200) { + break + } + + res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + await wait(500) + } assert.equal(res.status, 200) } finally { try { @@ -20,4 +28,4 @@ sandboxTest('ping server in sandbox', async ({ sandbox }) => { console.error(e) } } -}) +}, { timeout: 60000 }) diff --git a/packages/python-sdk/tests/async/sandbox_async/test_host.py b/packages/python-sdk/tests/async/sandbox_async/test_host.py index a8bd3c74c..6637029af 100644 --- a/packages/python-sdk/tests/async/sandbox_async/test_host.py +++ b/packages/python-sdk/tests/async/sandbox_async/test_host.py @@ -1,7 +1,6 @@ -import httpx -import pytest +import asyncio -from time import sleep +import httpx from e2b import AsyncSandbox @@ -13,12 +12,16 @@ async def test_ping_server(async_sandbox: AsyncSandbox, debug): ) try: - sleep(1) host = async_sandbox.get_host(8000) + status_code = None async with httpx.AsyncClient() as client: - res = await client.get(f"{'http' if debug else 'https'}://{host}") - assert res.status_code == 200 - + for _ in range(5): + res = await client.get(f"{'http' if debug else 'https'}://{host}") + status_code = res.status_code + if res.status_code == 200: + break + await asyncio.sleep(0.5) + assert status_code == 200 finally: await cmd.kill() diff --git a/packages/python-sdk/tests/sync/sandbox_sync/test_host.py b/packages/python-sdk/tests/sync/sandbox_sync/test_host.py index 18de6d5c1..461b98b62 100644 --- a/packages/python-sdk/tests/sync/sandbox_sync/test_host.py +++ b/packages/python-sdk/tests/sync/sandbox_sync/test_host.py @@ -7,10 +7,15 @@ def test_ping_server(sandbox, debug): cmd = sandbox.commands.run("python -m http.server 8001", background=True) try: - sleep(1) host = sandbox.get_host(8001) - res = httpx.get(f"{'http' if debug else 'https'}://{host}") - assert res.status_code == 200 + status_code = None + for _ in range(5): + res = httpx.get(f"{'http' if debug else 'https'}://{host}") + status_code = res.status_code + if res.status_code == 200: + break + sleep(0.5) + assert status_code == 200 finally: cmd.kill()