Skip to content

Commit

Permalink
Wait for supersim to be ready (#523)
Browse files Browse the repository at this point in the history
* Wait for supersim to be ready

* lint
  • Loading branch information
nitaliano authored Oct 22, 2024
1 parent f37b8fc commit 22ca398
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/viem/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
dist
src/test/*.txt
10 changes: 8 additions & 2 deletions packages/viem/src/test/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import { setupTicTacToe } from '@/test/setupTicTacToe.js'
import { startSupersim } from './startSupersim.js'

async function main() {
const shutdown = await startSupersim()
await setupTicTacToe()
let shutdown

try {
shutdown = await startSupersim()
await setupTicTacToe()
} catch (e) {
process.exit(1)
}

return () => {
shutdown()
Expand Down
29 changes: 28 additions & 1 deletion packages/viem/src/test/startSupersim.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { spawn } from 'child_process'
import fs from 'fs'
import path from 'path'

export type SupersimParameters = {
l1Port?: number
Expand All @@ -7,6 +9,10 @@ export type SupersimParameters = {

export type SupersimReturnArg = Promise<() => void>

const READY_TEXT = 'supersim is ready'
const STD_OUT_PATH = path.resolve(__dirname, './stdout.txt')
const STD_ERR_PATH = path.resolve(__dirname, './stderr.txt')

export async function startSupersim(
params?: SupersimParameters,
): SupersimReturnArg {
Expand All @@ -20,11 +26,32 @@ export async function startSupersim(
args.push(` --l2.starting.port ${params.l2StartingPort}`)
}

fs.writeFileSync(STD_OUT_PATH, '')
fs.writeFileSync(STD_ERR_PATH, '')

const supersimProcess = spawn('supersim', args, { shell: true })

return () => {
const cleanup = () => {
if (!supersimProcess.killed) {
supersimProcess.kill()
}
}

return new Promise((resolve, reject) => {
supersimProcess.stdout.on('data', (data) => {
const blob: string = data.toString()

fs.appendFileSync(STD_OUT_PATH, blob.replace(/\\n/g, '\n'))

if (blob.includes(READY_TEXT)) {
resolve(cleanup)
}
})

supersimProcess.stderr.on('data', (data) => {
const blob: string = data.toString()
fs.appendFileSync(STD_ERR_PATH, blob.replace(/\\n/g, '\n'))
reject(blob)
})
})
}

0 comments on commit 22ca398

Please sign in to comment.