Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
51 changes: 44 additions & 7 deletions src/cli/owner.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { readFile, rm, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { makeTmpDataFolder } from '../../test/helpers/tmp-data-folder.js'
import type { TmpDataFolder } from '../../test/helpers/tmp-data-folder.js'
import { AtomicCore } from '../core/index.js'
Expand All @@ -16,6 +17,25 @@ afterEach(async () => {
await data.cleanup()
})

const alive = (pid: number): boolean => {
try {
process.kill(pid, 0)
return true
} catch {
return false
}
}

async function until<T>(probe: () => Promise<T | undefined>, timeoutMs = 5_000): Promise<T> {
const deadline = Date.now() + timeoutMs
for (;;) {
const value = await probe()
if (value !== undefined) return value
if (Date.now() > deadline) throw new Error('timed out')
await new Promise((resolve) => setTimeout(resolve, 10))
}
}

async function runningCore(): Promise<AtomicCore> {
const core = await AtomicCore.create({ dataFolder: data.root, controlPort: 0 })
cores.push(core)
Expand Down Expand Up @@ -233,19 +253,36 @@ describe('attachToOwner', () => {

it('attaches to the winning owner when its own launcher exits after losing the race', async () => {
const core = await runningCore()
const ready = await readFile(data.layout.core.instanceLock, 'utf8')
await rm(data.layout.core.instanceLock)
const script = `require('node:fs').writeFileSync(${JSON.stringify(
data.layout.core.instanceLock
)}, ${JSON.stringify(ready)}); process.exit(3)`
const owner = await attachToOwner({
const lock = data.layout.core.instanceLock
const ready = await readFile(lock, 'utf8')
// The winner still starts when the loser exits, so only the loser's exit can end the first wait
// and the launcher always takes the path that waits for the winner. With a ready record in place
// before the exit, either side could win that race, and the path ran on some machines only.
const starting = JSON.stringify({ ...(JSON.parse(ready) as Record<string, unknown>), state: 'starting' })
const loserPidFile = join(data.root, 'loser.pid')
await rm(lock)
const script = [
`const fs = require('node:fs')`,
`fs.writeFileSync(${JSON.stringify(lock)}, ${JSON.stringify(starting)})`,
`fs.writeFileSync(${JSON.stringify(loserPidFile)}, String(process.pid))`,
`process.exit(3)`,
].join('; ')
const attaching = attachToOwner({
layout: data.layout,
clientName: 'race-loser',
launch: true,
selfCommand: [process.execPath, '-e', script],
timeoutMs: 2000,
timeoutMs: 5000,
})
// The winner becomes ready only once the loser is gone: reaped, so its exit has been seen here.
const loser = await until(
async () => Number(await readFile(loserPidFile, 'utf8').catch(() => '')) || undefined
)
await until(async () => (alive(loser) ? undefined : true))
await writeFile(lock, ready)
const owner = await attaching
expect(owner.record.instance_id).toBe(core.instanceId)
expect(owner.record.state).toBe('ready')
expect(owner.launched).toBe(false)
})

Expand Down
3 changes: 2 additions & 1 deletion src/runtime/fastflowlm/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ beforeEach(async () => {
})
afterEach(async () => {
await runtime.shutdown()
await rm(root, { recursive: true, force: true })
// Windows keeps a stopped child's files locked for a moment (EPERM on rmdir): retried.
await rm(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 })
})
async function load(modelId = id, overrides: Record<string, unknown> = {}) {
const pending = runtime.load(modelId, { timeoutSecs: 10, overrides })
Expand Down
4 changes: 3 additions & 1 deletion test/helpers/tmp-data-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export async function makeTmpDataFolder(prefix = 'atomic-core-data-'): Promise<T
layout,
writeModel,
writeBackend,
cleanup: () => rm(root, { recursive: true, force: true }),
// A child the test just stopped may still be closing a file in the folder: retried, as rm does on
// ENOTEMPTY / EBUSY / EPERM (a macOS gate failed on ENOTEMPTY after a diffusion owner shut down).
cleanup: () => rm(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }),
}
}
Loading