|
| 1 | +import { expect } from 'vitest' |
| 2 | +import { sandboxTest, wait } from './setup' |
| 3 | + |
| 4 | +async function waitForHealth(sandbox: any, maxRetries = 10, intervalMs = 100) { |
| 5 | + for (let i = 0; i < maxRetries; i++) { |
| 6 | + try { |
| 7 | + const result = await sandbox.commands.run( |
| 8 | + 'curl -s -o /dev/null -w "%{http_code}" http://0.0.0.0:49999/health' |
| 9 | + ) |
| 10 | + if (result.stdout.trim() === '200') { |
| 11 | + return true |
| 12 | + } |
| 13 | + } catch { |
| 14 | + // Connection refused or other error, retry |
| 15 | + } |
| 16 | + await wait(intervalMs) |
| 17 | + } |
| 18 | + return false |
| 19 | +} |
| 20 | + |
| 21 | +sandboxTest('restart after jupyter kill', async ({ sandbox }) => { |
| 22 | + // Verify health is up initially |
| 23 | + const initialHealth = await waitForHealth(sandbox) |
| 24 | + expect(initialHealth).toBe(true) |
| 25 | + |
| 26 | + // Kill the jupyter process as root |
| 27 | + // The command handle may get killed too (since killing jupyter cascades to code-interpreter), |
| 28 | + // so we catch the error. |
| 29 | + try { |
| 30 | + await sandbox.commands.run("kill -9 $(pgrep -f 'jupyter server')", { |
| 31 | + user: 'root', |
| 32 | + }) |
| 33 | + } catch { |
| 34 | + // Expected — the kill cascade may terminate the command handle |
| 35 | + } |
| 36 | + |
| 37 | + // Wait for systemd to restart both services |
| 38 | + const recovered = await waitForHealth(sandbox, 60, 500) |
| 39 | + expect(recovered).toBe(true) |
| 40 | + |
| 41 | + // Verify code execution works after recovery |
| 42 | + const result = await sandbox.runCode('x = 1; x') |
| 43 | + expect(result.text).toEqual('1') |
| 44 | +}) |
| 45 | + |
| 46 | +sandboxTest('restart after code-interpreter kill', async ({ sandbox }) => { |
| 47 | + // Verify health is up initially |
| 48 | + const initialHealth = await waitForHealth(sandbox) |
| 49 | + expect(initialHealth).toBe(true) |
| 50 | + |
| 51 | + // Kill the code-interpreter process as root |
| 52 | + try { |
| 53 | + await sandbox.commands.run("kill -9 $(pgrep -f 'uvicorn main:app')", { |
| 54 | + user: 'root', |
| 55 | + }) |
| 56 | + } catch { |
| 57 | + // Expected — killing code-interpreter may terminate the command handle |
| 58 | + } |
| 59 | + |
| 60 | + // Wait for systemd to restart it and health to come back |
| 61 | + const recovered = await waitForHealth(sandbox, 60, 500) |
| 62 | + expect(recovered).toBe(true) |
| 63 | + |
| 64 | + // Verify code execution works after recovery |
| 65 | + const result = await sandbox.runCode('x = 1; x') |
| 66 | + expect(result.text).toEqual('1') |
| 67 | +}) |
0 commit comments