Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test new error handling indicating when the sandbox is not found #540

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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: 3 additions & 1 deletion packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert } from 'vitest'
import { sandboxTest } from '../../setup.js'
import { sandboxTest, wait } from '../../setup.js'

sandboxTest('send stdin to process', async ({ sandbox }) => {
const text = 'Hello, World!'
Expand Down Expand Up @@ -37,6 +37,8 @@ sandboxTest('send special characters to stdin', async ({ sandbox }) => {

await sandbox.commands.sendStdin(cmd.pid, text)

await wait(5_000)

for (let i = 0; i < 5; i++) {
if (cmd.stdout === text) {
break
Expand Down
24 changes: 23 additions & 1 deletion packages/js-sdk/tests/sandbox/connect.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, assert } from 'vitest'
import { assert, test } from 'vitest'

import { Sandbox } from '../../src'
import { isDebug, template } from '../setup.js'
Expand All @@ -19,3 +19,25 @@ test('connect', async () => {
}
}
})

test('connect to non-running sandbox', async () => {
const sbx = await Sandbox.create(template, { timeoutMs: 10_000 })
let isKilled = false

try {
const isRunning = await sbx.isRunning()
assert.isTrue(isRunning)
await sbx.kill()
isKilled = true

const sbxConnection = await Sandbox.connect(sbx.sandboxId)
const isRunning2 = await sbxConnection.isRunning()
assert.isFalse(isRunning2)

await sbxConnection.commands.run('echo "hello"')
} finally {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this run command always fail? It looks to me like there is a missing assert on the behavior what is expected here

if (!isKilled) {
await sbx.kill()
}
}
})
66 changes: 48 additions & 18 deletions packages/js-sdk/tests/sandbox/host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,60 @@ import { assert } from 'vitest'

import { isDebug, sandboxTest, wait } from '../setup.js'

sandboxTest('ping server in sandbox', async ({ sandbox }) => {
const cmd = await sandbox.commands.run('python -m http.server 8000', { background: true })
sandboxTest.skipIf(isDebug)(
'ping server in running sandbox',
async ({ sandbox }) => {
const cmd = await sandbox.commands.run('python -m http.server 8000', {
background: true,
})

try {
await wait(1000)
try {
await wait(10_000)

const host = sandbox.getHost(8000)
const host = sandbox.getHost(8000)

let res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`)
const res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`)

for (let i = 0; i < 10; i++) {
if (res.status === 200) {
break
assert.equal(res.status, 200)
} finally {
try {
await cmd.kill()
} catch (e) {
console.error(e)
}

res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`)
await wait(500)
}
assert.equal(res.status, 200)
} finally {
}
)

sandboxTest.skipIf(isDebug)(
'ping server in non-running sandbox',
async ({ sandbox }) => {
const cmd = await sandbox.commands.run('python -m http.server 8000', {
background: true,
})

try {
await cmd.kill()
} catch (e) {
console.error(e)
await wait(10_000)

const host = sandbox.getHost(8000)

const res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`)

assert.equal(res.status, 200)

await sandbox.kill()

const res2 = await fetch(`${isDebug ? 'http' : 'https'}://${host}`)
assert.equal(res2.status, 502)

const text = await res2.text()
assert.equal(text, 'Sandbox does not exist.')
} finally {
try {
await cmd.kill()
} catch (e) {
console.error(e)
}
}
}
}, { timeout: 60000 })
)
Loading