|
| 1 | +import { test } from 'vitest' |
| 2 | + |
| 3 | +import Sandbox from '../../src/index.js' |
| 4 | +import { isIntegrationTest, wait } from '../setup.js' |
| 5 | + |
| 6 | +const heavyArray = new ArrayBuffer(256 * 1024 * 1024) // 256 MiB = 256 * 1024 * 1024 bytes |
| 7 | +const view = new Uint8Array(heavyArray) |
| 8 | +for (let i = 0; i < view.length; i++) { |
| 9 | + view[i] = Math.floor(Math.random() * 256) |
| 10 | +} |
| 11 | + |
| 12 | +const integrationTestTemplate = 'integration-test-v1' |
| 13 | +const sanboxCount = 10 |
| 14 | + |
| 15 | +test.skipIf(!isIntegrationTest)( |
| 16 | + 'stress test heavy file writes and reads', |
| 17 | + async () => { |
| 18 | + const promises: Array<Promise<string | void>> = [] |
| 19 | + for (let i = 0; i < sanboxCount; i++) { |
| 20 | + promises.push( |
| 21 | + Sandbox.create(integrationTestTemplate, { timeoutMs: 60 }) |
| 22 | + .then((sbx) => { |
| 23 | + console.log(sbx.sandboxId) |
| 24 | + return sbx.files |
| 25 | + .write('heavy-file', heavyArray) |
| 26 | + .then(() => sbx.files.read('heavy-file')) |
| 27 | + }) |
| 28 | + .catch(console.error) |
| 29 | + ) |
| 30 | + } |
| 31 | + await wait(10_000) |
| 32 | + await Promise.all(promises) |
| 33 | + } |
| 34 | +) |
| 35 | + |
| 36 | +test.skipIf(!isIntegrationTest)('stress requests to nextjs app', async ({}) => { |
| 37 | + const hostPromises: Array<Promise<string | void>> = [] |
| 38 | + |
| 39 | + for (let i = 0; i < sanboxCount; i++) { |
| 40 | + hostPromises.push( |
| 41 | + Sandbox.create(integrationTestTemplate, { timeoutMs: 60_000 }).then( |
| 42 | + (sbx) => { |
| 43 | + console.log('created sandbox', sbx.sandboxId) |
| 44 | + return new Promise((resolve, reject) => { |
| 45 | + try { |
| 46 | + resolve(sbx.getHost(3000)) |
| 47 | + } catch (e) { |
| 48 | + console.error('error getting sbx host', e) |
| 49 | + reject(e) |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | + ) |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + await wait(10_000) |
| 58 | + const hosts = await Promise.all(hostPromises) |
| 59 | + |
| 60 | + const fetchPromises: Array<Promise<string | void>> = [] |
| 61 | + |
| 62 | + for (let i = 0; i < 100; i++) { |
| 63 | + for (const host of hosts) { |
| 64 | + fetchPromises.push( |
| 65 | + new Promise((resolve) => { |
| 66 | + fetch('https://' + host) |
| 67 | + .then((res) => { |
| 68 | + console.log(`response for ${host}: ${res.status}`) |
| 69 | + }) |
| 70 | + .then(resolve) |
| 71 | + }) |
| 72 | + ) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + await Promise.all(fetchPromises) |
| 77 | +}) |
0 commit comments