From cc797a82311682c6f377ec4c8f9af53e313722ca Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 5 Nov 2024 22:56:35 +1100 Subject: [PATCH 01/15] teardown-3 --- lib/worker.js | 11 +- test/02-teardown.test.js | 186 +++++++++--------- test/fixtures/teardown-exit-code/index.js | 11 ++ test/fixtures/teardown-exit-code/package.json | 8 + test/fixtures/teardown-os-kill/index.js | 10 + test/fixtures/teardown-os-kill/package.json | 8 + test/fixtures/teardown/index.js | 9 + test/fixtures/teardown/package.json | 8 + test/helper.js | 59 +++++- 9 files changed, 216 insertions(+), 94 deletions(-) create mode 100644 test/fixtures/teardown-exit-code/index.js create mode 100644 test/fixtures/teardown-exit-code/package.json create mode 100644 test/fixtures/teardown-os-kill/index.js create mode 100644 test/fixtures/teardown-os-kill/package.json create mode 100644 test/fixtures/teardown/index.js create mode 100644 test/fixtures/teardown/package.json diff --git a/lib/worker.js b/lib/worker.js index c1e5cbaa3..c146a298f 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -1,6 +1,7 @@ 'use strict' const { isElectronRenderer, isWindows, isBare } = require('which-runtime') const fs = isBare ? require('bare-fs') : require('fs') +const teardown = isBare ? require('./teardown') : (fn) => fn() const { spawn } = isBare ? require('bare-subprocess') : require('child_process') const { command } = require('paparam') const Pipe = isBare @@ -16,6 +17,7 @@ class Worker { #ref = null #unref = null #ipc = null + static RUNTIME = constants.RUNTIME constructor ({ ref = noop, unref = noop, ipc = null } = {}) { this.#ref = ref this.#unref = unref @@ -41,7 +43,7 @@ class Worker { run (link, args = []) { if (isElectronRenderer) return this.#ipc.workerRun(link, args) args = [...this.#args(link), ...args] - const sp = spawn(constants.RUNTIME, args, { + const sp = spawn(this.constructor.RUNTIME, args, { stdio: ['inherit', 'inherit', 'inherit', 'overlapped'], windowsHide: true }) @@ -67,11 +69,12 @@ class Worker { return null } const pipe = new Pipe(fd) - pipe.on('end', () => pipe.end()) + pipe.on('end', () => { + teardown(() => pipe.end(), Number.MAX_SAFE_INTEGER) + }) this.#pipe = pipe pipe.once('close', () => { - // allow close event to propagate between processes before exiting: - setImmediate(() => program.exit()) + teardown(() => program.exit(), Number.MAX_SAFE_INTEGER) }) return pipe } diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index 13213d04f..a710d4658 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -1,149 +1,157 @@ 'use strict' +const { isWindows } = require('which-runtime') const test = require('brittle') const path = require('bare-path') +const os = require('bare-os') const hypercoreid = require('hypercore-id-encoding') const Helper = require('./helper') -const harness = path.join(Helper.localDir, 'test', 'fixtures', 'harness') +const teardownDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown') +const teardownOsKillDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-os-kill') +const teardownExitCodeDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-exit-code') -test('teardown', async function ({ is, ok, plan, comment, teardown, timeout }) { +test('teardown on pipe end', async function ({ ok, is, plan, comment, teardown, timeout }) { + if (isWindows) return + timeout(180000) + plan(4) - plan(5) - - const stager = new Helper() - teardown(() => stager.close(), { order: Infinity }) - await stager.ready() + const dir = teardownDir - const dir = harness + const helper = new Helper() + teardown(() => helper.close(), { order: Infinity }) + await helper.ready() const id = Math.floor(Math.random() * 10000) comment('staging') - const stage = stager.stage({ channel: `test-${id}`, name: `test-${id}`, dir, dryRun: false, bare: true }) - const final = await Helper.pick(stage, { tag: 'final' }) - ok(final.success, 'stage succeeded') + const staging = helper.stage({ channel: `test-${id}`, name: `test-${id}`, dir, dryRun: false, bare: true }) + teardown(() => Helper.teardownStream(staging)) + const staged = await Helper.pick(staging, { tag: 'final' }) + ok(staged.success, 'stage succeeded') comment('seeding') - const seeder = new Helper() - teardown(() => seeder.close(), { order: Infinity }) - await seeder.ready() - const seed = seeder.seed({ channel: `test-${id}`, name: `test-${id}`, dir }) - teardown(() => Helper.teardownStream(seed)) - const until = await Helper.pick(seed, [{ tag: 'key' }, { tag: 'announced' }]) - const key = await until.key + const seeding = helper.seed({ channel: `test-${id}`, name: `test-${id}`, dir, key: null, cmdArgs: [] }) + teardown(() => Helper.teardownStream(seeding)) + const until = await Helper.pick(seeding, [{ tag: 'key' }, { tag: 'announced' }]) const announced = await until.announced + ok(announced, 'seeding is announced') + const key = await until.key ok(hypercoreid.isValid(key), 'app key is valid') - ok(announced, 'seeding is announced') - comment('running') - const link = 'pear://' + key - const running = await Helper.open(link, { tags: ['teardown', 'exit'] }) + const link = `pear://${key}` + const run = await Helper.run({ link }) + const { pipe } = run - await running.inspector.evaluate('Pear.teardown(() => console.log(\'teardown\'))') - await running.inspector.evaluate('Pear.shutdown()') - await running.inspector.close() + const teardownPromise = Helper.untilResult(run.pipe) - const td = await running.until.teardown - is(td, 'teardown', 'teardown has been triggered') + pipe.end() - const { code } = await running.until.exit - is(code, 0, 'exit code is 0') + const td = await teardownPromise + is(td, 'teardown', 'teardown executed') }) -test('teardown during teardown', async function ({ is, ok, plan, comment, teardown, timeout }) { +test('teardown on os kill', async function ({ ok, is, plan, comment, teardown, timeout }) { + if (isWindows) return + timeout(180000) plan(5) - const stager = new Helper() - teardown(() => stager.close(), { order: Infinity }) - await stager.ready() + const dir = teardownOsKillDir - const dir = harness + const helper = new Helper() + teardown(() => helper.close(), { order: Infinity }) + await helper.ready() const id = Math.floor(Math.random() * 10000) comment('staging') - const stage = stager.stage({ channel: `test-${id}`, name: `test-${id}`, dir, dryRun: false, bare: true }) - const final = await Helper.pick(stage, { tag: 'final' }) - ok(final.success, 'stage succeeded') + const staging = helper.stage({ channel: `test-${id}`, name: `test-${id}`, dir, dryRun: false, bare: true }) + teardown(() => Helper.teardownStream(staging)) + const staged = await Helper.pick(staging, { tag: 'final' }) + ok(staged.success, 'stage succeeded') comment('seeding') - const seeder = new Helper() - teardown(() => seeder.close(), { order: Infinity }) - await seeder.ready() - const seed = seeder.seed({ channel: `test-${id}`, name: `test-${id}`, dir }) - teardown(() => Helper.teardownStream(seed)) - const until = await Helper.pick(seed, [{ tag: 'key' }, { tag: 'announced' }]) - const key = await until.key + const seeding = helper.seed({ channel: `test-${id}`, name: `test-${id}`, dir, key: null, cmdArgs: [] }) + teardown(() => Helper.teardownStream(seeding)) + const until = await Helper.pick(seeding, [{ tag: 'key' }, { tag: 'announced' }]) const announced = await until.announced + ok(announced, 'seeding is announced') + const key = await until.key ok(hypercoreid.isValid(key), 'app key is valid') - ok(announced, 'seeding is announced') - comment('running') - const link = 'pear://' + key - const running = await Helper.open(link, { tags: ['teardown', 'exit'] }) + const link = `pear://${key}` + const run = await Helper.run({ link }) + const { pipe } = run - await running.inspector.evaluate( - `(() => { - const { teardown } = Pear - const a = () => { b() } - const b = () => { teardown(() => console.log('teardown from b')) } - teardown( () => a() ) - })()`) + const pidPromise = Helper.untilResult(pipe) - await running.inspector.evaluate('Pear.shutdown()') - await running.inspector.close() + pipe.write('start') - const td = await running.until.teardown - is(td, 'teardown from b', 'teardown from b has been triggered') + const pid = +(await pidPromise) + ok(pid > 0, 'worker pid is valid') - const { code } = await running.until.exit - is(code, 0, 'exit code is 0') + const teardownPromise = Helper.untilResult(pipe, 5000, () => os.kill(pid)) + + const td = await teardownPromise + ok(td, 'teardown executed') }) -// TODO: fixme -test.skip('exit with non-zero code in teardown', async function ({ is, ok, plan, comment, teardown }) { - plan(4) +test('teardown on os kill with exit code', async function ({ ok, is, plan, comment, teardown, timeout }) { + if (isWindows) return + + timeout(180000) + plan(6) - const stager = new Helper() - teardown(() => stager.close(), { order: Infinity }) - await stager.ready() + const dir = teardownExitCodeDir - const dir = harness + const helper = new Helper() + teardown(() => helper.close(), { order: Infinity }) + await helper.ready() const id = Math.floor(Math.random() * 10000) comment('staging') - const stage = stager.stage({ channel: `test-${id}`, name: `test-${id}`, dir, dryRun: false, bare: true }) - const final = await Helper.pick(stage, { tag: 'final' }) - ok(final.success, 'stage succeeded') + const staging = helper.stage({ channel: `test-${id}`, name: `test-${id}`, dir, dryRun: false, bare: true }) + teardown(() => Helper.teardownStream(staging)) + const staged = await Helper.pick(staging, { tag: 'final' }) + ok(staged.success, 'stage succeeded') comment('seeding') - const seeder = new Helper() - teardown(() => seeder.close()) - teardown(() => seeder.close(), { order: Infinity }) - await seeder.ready() - const seed = seeder.seed({ channel: `test-${id}`, name: `test-${id}`, dir }) - teardown(() => Helper.teardownStream(seed)) - const until = await Helper.pick(seed, [{ tag: 'key' }, { tag: 'announced' }]) - const key = await until.key + const seeding = helper.seed({ channel: `test-${id}`, name: `test-${id}`, dir, key: null, cmdArgs: [] }) + teardown(() => Helper.teardownStream(seeding)) + const until = await Helper.pick(seeding, [{ tag: 'key' }, { tag: 'announced' }]) const announced = await until.announced + ok(announced, 'seeding is announced') + const key = await until.key ok(hypercoreid.isValid(key), 'app key is valid') - ok(announced, 'seeding is announced') - comment('running') - const link = 'pear://' + key - const running = await Helper.open(link, { tags: ['teardown', 'exit'] }) + const link = `pear://${key}` + const run = await Helper.run({ link }) + const { pipe } = run + + const pidPromise = Helper.untilResult(pipe) + + pipe.write('start') + + const pid = +(await pidPromise) + ok(pid > 0, 'worker pid is valid') - await running.inspector.evaluate('Pear.teardown(() => Pear.exit(124))') + const exitCodePromise = new Promise((resolve, reject) => { + const timeoutId = setTimeout(() => reject(new Error('timed out')), 5000) + pipe.on('crash', (data) => { + clearTimeout(timeoutId) + resolve(data.exitCode) + }) + }) + + const teardownPromise = Helper.untilResult(pipe, 5000, () => os.kill(pid)) - await running.inspector.evaluate('__PEAR_TEST__.close()') - await running.inspector.close() - // running.subprocess.kill('SIGINT') <-- this was forcing the exit code, which false-positives the test + const td = await teardownPromise + ok(td, 'teardown executed') - const { code } = await running.until.exit - is(code, 124, 'exit code is 124') + const exitCode = await exitCodePromise + is(exitCode, 124, 'exit code is 124') }) diff --git a/test/fixtures/teardown-exit-code/index.js b/test/fixtures/teardown-exit-code/index.js new file mode 100644 index 000000000..b55b32d6c --- /dev/null +++ b/test/fixtures/teardown-exit-code/index.js @@ -0,0 +1,11 @@ +const program = global.Bare || global.process + +const pipe = Pear.worker.pipe() +pipe.on('data', () => pipe.write(`${program.pid}`)) + +Pear.teardown(async () => { + await new Promise((resolve) => { + pipe.write('teardown', resolve) + }) + Pear.exit(124) +}) diff --git a/test/fixtures/teardown-exit-code/package.json b/test/fixtures/teardown-exit-code/package.json new file mode 100644 index 000000000..f18581e31 --- /dev/null +++ b/test/fixtures/teardown-exit-code/package.json @@ -0,0 +1,8 @@ +{ + "name": "teardown-exit-code", + "main": "index.js", + "pear": { + "name": "teardown-exit-code", + "type": "terminal" + } +} diff --git a/test/fixtures/teardown-os-kill/index.js b/test/fixtures/teardown-os-kill/index.js new file mode 100644 index 000000000..9002591b0 --- /dev/null +++ b/test/fixtures/teardown-os-kill/index.js @@ -0,0 +1,10 @@ +const program = global.Bare || global.process + +const pipe = Pear.worker.pipe() +pipe.on('data', () => pipe.write(`${program.pid}`)) + +Pear.teardown(async () => { + await new Promise((resolve) => { + pipe.write('teardown', resolve) + }) +}) diff --git a/test/fixtures/teardown-os-kill/package.json b/test/fixtures/teardown-os-kill/package.json new file mode 100644 index 000000000..a4c47ab80 --- /dev/null +++ b/test/fixtures/teardown-os-kill/package.json @@ -0,0 +1,8 @@ +{ + "name": "teardown-os-kill", + "main": "index.js", + "pear": { + "name": "teardown-os-kill", + "type": "terminal" + } +} diff --git a/test/fixtures/teardown/index.js b/test/fixtures/teardown/index.js new file mode 100644 index 000000000..026f6cf8c --- /dev/null +++ b/test/fixtures/teardown/index.js @@ -0,0 +1,9 @@ +const program = global.Bare || global.process + +const pipe = Pear.worker.pipe() + +Pear.teardown(async () => { + await new Promise((resolve) => { + pipe.write('teardown', resolve) + }) +}) diff --git a/test/fixtures/teardown/package.json b/test/fixtures/teardown/package.json new file mode 100644 index 000000000..679785645 --- /dev/null +++ b/test/fixtures/teardown/package.json @@ -0,0 +1,8 @@ +{ + "name": "teardown", + "main": "index.js", + "pear": { + "name": "teardown", + "type": "terminal" + } +} diff --git a/test/helper.js b/test/helper.js index 0e7f64cb9..8414f8886 100644 --- a/test/helper.js +++ b/test/helper.js @@ -16,12 +16,14 @@ const updaterBootstrap = require('pear-updater-bootstrap') const b4a = require('b4a') const HOST = platform + '-' + arch const BY_ARCH = path.join('by-arch', HOST, 'bin', `pear-runtime${isWindows ? '.exe' : ''}`) -const { PLATFORM_DIR } = require('../constants') +const constants = require('../constants') +const { PLATFORM_DIR, RUNTIME } = constants const { pathname } = new URL(global.Pear.config.applink) const NO_GC = global.Pear.config.args.includes('--no-tmp-gc') const MAX_OP_STEP_WAIT = env.CI ? 360000 : 120000 const tmp = fs.realpathSync(os.tmpdir()) Error.stackTraceLimit = Infinity +const program = global.Bare || global.process const rigPear = path.join(tmp, 'rig-pear') @@ -143,6 +145,61 @@ class Helper extends IPC { // ONLY ADD STATICS, NEVER ADD PUBLIC METHODS OR PROPERTIES (see pear-ipc) static localDir = isWindows ? path.normalize(pathname.slice(1)) : pathname + static async run ({ link, encryptionKey, platformDir }) { + if (encryptionKey) program.argv.splice(2, 0, '--encryption-key', encryptionKey) + if (platformDir) Pear.worker.constructor.RUNTIME = path.join(platformDir, 'current', BY_ARCH) + + const pipe = Pear.worker.run(link) + + if (platformDir) Pear.worker.constructor.RUNTIME = RUNTIME + if (encryptionKey) program.argv.splice(2, 2) + + return { pipe } + } + + static async untilResult (pipe, timeout = 5000, runFn) { + const res = new Promise((resolve, reject) => { + const timeoutId = setTimeout(() => reject(new Error('timed out')), timeout) + pipe.on('data', (data) => { + clearTimeout(timeoutId) + resolve(data.toString()) + }) + pipe.on('close', () => { + clearTimeout(timeoutId) + reject(new Error('unexpected closed')) + }) + pipe.on('end', () => { + clearTimeout(timeoutId) + reject(new Error('unexpected ended')) + }) + }) + if (runFn) { + await runFn() + } else { + pipe.write('start') + } + return res + } + + static async untilClose (pipe, timeout = 5000) { + // TODO: fix the "Error: RPC destroyed" when calling pipe.end() too fast, then remove this hack delay + await new Promise((resolve) => setTimeout(resolve, 1000)) + + const res = new Promise((resolve, reject) => { + const timeoutId = setTimeout(() => reject(new Error('timed out')), timeout) + pipe.on('close', () => { + clearTimeout(timeoutId) + resolve('closed') + }) + pipe.on('end', () => { + clearTimeout(timeoutId) + resolve('ended') + }) + }) + pipe.end() + return res + } + static async open (link, { tags = [] } = {}, opts = {}) { if (!link) throw new Error('Key is missing') From 9a90bf9ef18b82d690db61420b505d788d213aa9 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 5 Nov 2024 23:11:01 +1100 Subject: [PATCH 02/15] clean --- lib/worker.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/worker.js b/lib/worker.js index c146a298f..e964eff75 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -17,7 +17,6 @@ class Worker { #ref = null #unref = null #ipc = null - static RUNTIME = constants.RUNTIME constructor ({ ref = noop, unref = noop, ipc = null } = {}) { this.#ref = ref this.#unref = unref @@ -43,7 +42,7 @@ class Worker { run (link, args = []) { if (isElectronRenderer) return this.#ipc.workerRun(link, args) args = [...this.#args(link), ...args] - const sp = spawn(this.constructor.RUNTIME, args, { + const sp = spawn(constants.RUNTIME, args, { stdio: ['inherit', 'inherit', 'inherit', 'overlapped'], windowsHide: true }) From 8ad2832f4679a8d310f96adda685b471aee29bdc Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 5 Nov 2024 23:33:18 +1100 Subject: [PATCH 03/15] enable win --- test/02-teardown.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index a710d4658..f67034cb5 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -10,8 +10,6 @@ const teardownOsKillDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardo const teardownExitCodeDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-exit-code') test('teardown on pipe end', async function ({ ok, is, plan, comment, teardown, timeout }) { - if (isWindows) return - timeout(180000) plan(4) From 7ba795f7ef7180525c2247787d2e937cfb38649a Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 5 Nov 2024 23:35:05 +1100 Subject: [PATCH 04/15] clean --- test/02-teardown.test.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index f67034cb5..38f889e2b 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -83,11 +83,7 @@ test('teardown on os kill', async function ({ ok, is, plan, comment, teardown, t const run = await Helper.run({ link }) const { pipe } = run - const pidPromise = Helper.untilResult(pipe) - - pipe.write('start') - - const pid = +(await pidPromise) + const pid = +(await Helper.untilResult(pipe)) ok(pid > 0, 'worker pid is valid') const teardownPromise = Helper.untilResult(pipe, 5000, () => os.kill(pid)) @@ -130,11 +126,7 @@ test('teardown on os kill with exit code', async function ({ ok, is, plan, comme const run = await Helper.run({ link }) const { pipe } = run - const pidPromise = Helper.untilResult(pipe) - - pipe.write('start') - - const pid = +(await pidPromise) + const pid = +(await Helper.untilResult(pipe)) ok(pid > 0, 'worker pid is valid') const exitCodePromise = new Promise((resolve, reject) => { From b0d9a35296371e67b4545fa0252f84d2362fec78 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 5 Nov 2024 23:36:33 +1100 Subject: [PATCH 05/15] lint --- test/02-teardown.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index 38f889e2b..f1db79a8b 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -51,7 +51,7 @@ test('teardown on pipe end', async function ({ ok, is, plan, comment, teardown, test('teardown on os kill', async function ({ ok, is, plan, comment, teardown, timeout }) { if (isWindows) return - + timeout(180000) plan(5) @@ -136,7 +136,7 @@ test('teardown on os kill with exit code', async function ({ ok, is, plan, comme resolve(data.exitCode) }) }) - + const teardownPromise = Helper.untilResult(pipe, 5000, () => os.kill(pid)) const td = await teardownPromise From 5ba70fd997359cf5c6ffe88f2f5cea81f07f5dd6 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 5 Nov 2024 23:38:05 +1100 Subject: [PATCH 06/15] pipe --- test/02-teardown.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index f1db79a8b..61a08319b 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -41,7 +41,7 @@ test('teardown on pipe end', async function ({ ok, is, plan, comment, teardown, const run = await Helper.run({ link }) const { pipe } = run - const teardownPromise = Helper.untilResult(run.pipe) + const teardownPromise = Helper.untilResult(pipe) pipe.end() From ee4e8ede1feaf5cc08119168a2c6b43dd9dc04cc Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 5 Nov 2024 23:44:35 +1100 Subject: [PATCH 07/15] disable win --- test/02-teardown.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index 61a08319b..e4e354829 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -10,6 +10,8 @@ const teardownOsKillDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardo const teardownExitCodeDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-exit-code') test('teardown on pipe end', async function ({ ok, is, plan, comment, teardown, timeout }) { + if (isWindows) return + timeout(180000) plan(4) From 2fa2bd1b2a163c5a9877d49767292853b463b477 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 6 Nov 2024 12:00:13 +1100 Subject: [PATCH 08/15] teardown-3 --- test/02-teardown.test.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index e4e354829..0f404b4d7 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -9,9 +9,7 @@ const teardownDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown') const teardownOsKillDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-os-kill') const teardownExitCodeDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-exit-code') -test('teardown on pipe end', async function ({ ok, is, plan, comment, teardown, timeout }) { - if (isWindows) return - +test('teardown on pipe end', { skip: isWindows }, async function ({ ok, is, plan, comment, teardown, timeout }) { timeout(180000) plan(4) @@ -51,9 +49,7 @@ test('teardown on pipe end', async function ({ ok, is, plan, comment, teardown, is(td, 'teardown', 'teardown executed') }) -test('teardown on os kill', async function ({ ok, is, plan, comment, teardown, timeout }) { - if (isWindows) return - +test('teardown on os kill', { skip: isWindows }, async function ({ ok, is, plan, comment, teardown, timeout }) { timeout(180000) plan(5) @@ -94,9 +90,7 @@ test('teardown on os kill', async function ({ ok, is, plan, comment, teardown, t ok(td, 'teardown executed') }) -test('teardown on os kill with exit code', async function ({ ok, is, plan, comment, teardown, timeout }) { - if (isWindows) return - +test('teardown on os kill with exit code', { skip: isWindows }, async function ({ ok, is, plan, comment, teardown, timeout }) { timeout(180000) plan(6) From 635b57ad08f0934ce29495c5aa75abcda0345281 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 6 Nov 2024 12:03:26 +1100 Subject: [PATCH 09/15] teardown-3 --- test/02-teardown.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index 0f404b4d7..ac99575aa 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -49,7 +49,7 @@ test('teardown on pipe end', { skip: isWindows }, async function ({ ok, is, plan is(td, 'teardown', 'teardown executed') }) -test('teardown on os kill', { skip: isWindows }, async function ({ ok, is, plan, comment, teardown, timeout }) { +test('teardown on os kill', { skip: isWindows }, async function ({ ok, is, plan, comment, teardown, timeout }) { timeout(180000) plan(5) From 1fd1d31c49cf69975659b281df88355512d72344 Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 7 Nov 2024 00:29:21 +1100 Subject: [PATCH 10/15] teardown-3 --- test/02-teardown.test.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index ac99575aa..359caee73 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -84,9 +84,7 @@ test('teardown on os kill', { skip: isWindows }, async function ({ ok, is, plan, const pid = +(await Helper.untilResult(pipe)) ok(pid > 0, 'worker pid is valid') - const teardownPromise = Helper.untilResult(pipe, 5000, () => os.kill(pid)) - - const td = await teardownPromise + const td = await Helper.untilResult(pipe, 5000, () => os.kill(pid)) ok(td, 'teardown executed') }) @@ -133,9 +131,7 @@ test('teardown on os kill with exit code', { skip: isWindows }, async function ( }) }) - const teardownPromise = Helper.untilResult(pipe, 5000, () => os.kill(pid)) - - const td = await teardownPromise + const td = await Helper.untilResult(pipe, 5000, () => os.kill(pid)) ok(td, 'teardown executed') const exitCode = await exitCodePromise From 7bfe0c5c86216c21152ff9e20dbc632a25c02a58 Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 7 Nov 2024 00:30:40 +1100 Subject: [PATCH 11/15] teardown-3 --- test/02-teardown.test.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index 359caee73..68ba3a28f 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -41,11 +41,7 @@ test('teardown on pipe end', { skip: isWindows }, async function ({ ok, is, plan const run = await Helper.run({ link }) const { pipe } = run - const teardownPromise = Helper.untilResult(pipe) - - pipe.end() - - const td = await teardownPromise + const td = await Helper.untilResult(pipe, 5000, () => pipe.end()) is(td, 'teardown', 'teardown executed') }) From bcda8792c8ddffa7a77feaf377c9b4ee287ddfe0 Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 8 Nov 2024 00:40:53 +1100 Subject: [PATCH 12/15] bare pid --- test/fixtures/teardown-exit-code/index.js | 4 +--- test/fixtures/teardown-os-kill/index.js | 4 +--- test/fixtures/teardown/index.js | 2 -- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/test/fixtures/teardown-exit-code/index.js b/test/fixtures/teardown-exit-code/index.js index b55b32d6c..04a351c33 100644 --- a/test/fixtures/teardown-exit-code/index.js +++ b/test/fixtures/teardown-exit-code/index.js @@ -1,7 +1,5 @@ -const program = global.Bare || global.process - const pipe = Pear.worker.pipe() -pipe.on('data', () => pipe.write(`${program.pid}`)) +pipe.on('data', () => pipe.write(`${Bare.pid}`)) Pear.teardown(async () => { await new Promise((resolve) => { diff --git a/test/fixtures/teardown-os-kill/index.js b/test/fixtures/teardown-os-kill/index.js index 9002591b0..3df111bec 100644 --- a/test/fixtures/teardown-os-kill/index.js +++ b/test/fixtures/teardown-os-kill/index.js @@ -1,7 +1,5 @@ -const program = global.Bare || global.process - const pipe = Pear.worker.pipe() -pipe.on('data', () => pipe.write(`${program.pid}`)) +pipe.on('data', () => pipe.write(`${Bare.pid}`)) Pear.teardown(async () => { await new Promise((resolve) => { diff --git a/test/fixtures/teardown/index.js b/test/fixtures/teardown/index.js index 026f6cf8c..ef1f86798 100644 --- a/test/fixtures/teardown/index.js +++ b/test/fixtures/teardown/index.js @@ -1,5 +1,3 @@ -const program = global.Bare || global.process - const pipe = Pear.worker.pipe() Pear.teardown(async () => { From 9ff6c8dea8f866cb13012970a3731fdf56e769c5 Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 8 Nov 2024 00:52:18 +1100 Subject: [PATCH 13/15] support win for pipe end --- test/02-teardown.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index 68ba3a28f..906634249 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -9,7 +9,7 @@ const teardownDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown') const teardownOsKillDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-os-kill') const teardownExitCodeDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-exit-code') -test('teardown on pipe end', { skip: isWindows }, async function ({ ok, is, plan, comment, teardown, timeout }) { +test('teardown on pipe end', async function ({ ok, is, plan, comment, teardown, timeout }) { timeout(180000) plan(4) From 4f8169ec659111ca5bba360500a3029e1d1a8f48 Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 8 Nov 2024 00:57:47 +1100 Subject: [PATCH 14/15] skip win on pipe end --- test/02-teardown.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index 906634249..68ba3a28f 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -9,7 +9,7 @@ const teardownDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown') const teardownOsKillDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-os-kill') const teardownExitCodeDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-exit-code') -test('teardown on pipe end', async function ({ ok, is, plan, comment, teardown, timeout }) { +test('teardown on pipe end', { skip: isWindows }, async function ({ ok, is, plan, comment, teardown, timeout }) { timeout(180000) plan(4) From 8cf226551e17de4c1fbe31ee19c530c1022289a2 Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 8 Nov 2024 01:41:14 +1100 Subject: [PATCH 15/15] rename --- test/02-teardown.test.js | 12 ++++++------ test/fixtures/teardown-exit-code/package.json | 8 -------- test/fixtures/teardown-os-kill/package.json | 8 -------- .../index.js | 0 test/fixtures/worker-teardown-exit-code/package.json | 8 ++++++++ .../index.js | 0 test/fixtures/worker-teardown-os-kill/package.json | 8 ++++++++ test/fixtures/{teardown => worker-teardown}/index.js | 0 .../{teardown => worker-teardown}/package.json | 4 ++-- 9 files changed, 24 insertions(+), 24 deletions(-) delete mode 100644 test/fixtures/teardown-exit-code/package.json delete mode 100644 test/fixtures/teardown-os-kill/package.json rename test/fixtures/{teardown-exit-code => worker-teardown-exit-code}/index.js (100%) create mode 100644 test/fixtures/worker-teardown-exit-code/package.json rename test/fixtures/{teardown-os-kill => worker-teardown-os-kill}/index.js (100%) create mode 100644 test/fixtures/worker-teardown-os-kill/package.json rename test/fixtures/{teardown => worker-teardown}/index.js (100%) rename test/fixtures/{teardown => worker-teardown}/package.json (52%) diff --git a/test/02-teardown.test.js b/test/02-teardown.test.js index 68ba3a28f..591970098 100644 --- a/test/02-teardown.test.js +++ b/test/02-teardown.test.js @@ -5,15 +5,15 @@ const path = require('bare-path') const os = require('bare-os') const hypercoreid = require('hypercore-id-encoding') const Helper = require('./helper') -const teardownDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown') -const teardownOsKillDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-os-kill') -const teardownExitCodeDir = path.join(Helper.localDir, 'test', 'fixtures', 'teardown-exit-code') +const workerTeardownDir = path.join(Helper.localDir, 'test', 'fixtures', 'worker-teardown') +const workerTeardownOsKillDir = path.join(Helper.localDir, 'test', 'fixtures', 'worker-teardown-os-kill') +const workerTeardownExitCodeDir = path.join(Helper.localDir, 'test', 'fixtures', 'worker-teardown-exit-code') test('teardown on pipe end', { skip: isWindows }, async function ({ ok, is, plan, comment, teardown, timeout }) { timeout(180000) plan(4) - const dir = teardownDir + const dir = workerTeardownDir const helper = new Helper() teardown(() => helper.close(), { order: Infinity }) @@ -49,7 +49,7 @@ test('teardown on os kill', { skip: isWindows }, async function ({ ok, is, plan, timeout(180000) plan(5) - const dir = teardownOsKillDir + const dir = workerTeardownOsKillDir const helper = new Helper() teardown(() => helper.close(), { order: Infinity }) @@ -88,7 +88,7 @@ test('teardown on os kill with exit code', { skip: isWindows }, async function ( timeout(180000) plan(6) - const dir = teardownExitCodeDir + const dir = workerTeardownExitCodeDir const helper = new Helper() teardown(() => helper.close(), { order: Infinity }) diff --git a/test/fixtures/teardown-exit-code/package.json b/test/fixtures/teardown-exit-code/package.json deleted file mode 100644 index f18581e31..000000000 --- a/test/fixtures/teardown-exit-code/package.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "teardown-exit-code", - "main": "index.js", - "pear": { - "name": "teardown-exit-code", - "type": "terminal" - } -} diff --git a/test/fixtures/teardown-os-kill/package.json b/test/fixtures/teardown-os-kill/package.json deleted file mode 100644 index a4c47ab80..000000000 --- a/test/fixtures/teardown-os-kill/package.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "teardown-os-kill", - "main": "index.js", - "pear": { - "name": "teardown-os-kill", - "type": "terminal" - } -} diff --git a/test/fixtures/teardown-exit-code/index.js b/test/fixtures/worker-teardown-exit-code/index.js similarity index 100% rename from test/fixtures/teardown-exit-code/index.js rename to test/fixtures/worker-teardown-exit-code/index.js diff --git a/test/fixtures/worker-teardown-exit-code/package.json b/test/fixtures/worker-teardown-exit-code/package.json new file mode 100644 index 000000000..b0a017614 --- /dev/null +++ b/test/fixtures/worker-teardown-exit-code/package.json @@ -0,0 +1,8 @@ +{ + "name": "worker-teardown-exit-code", + "main": "index.js", + "pear": { + "name": "worker-teardown-exit-code", + "type": "terminal" + } +} diff --git a/test/fixtures/teardown-os-kill/index.js b/test/fixtures/worker-teardown-os-kill/index.js similarity index 100% rename from test/fixtures/teardown-os-kill/index.js rename to test/fixtures/worker-teardown-os-kill/index.js diff --git a/test/fixtures/worker-teardown-os-kill/package.json b/test/fixtures/worker-teardown-os-kill/package.json new file mode 100644 index 000000000..110c14a67 --- /dev/null +++ b/test/fixtures/worker-teardown-os-kill/package.json @@ -0,0 +1,8 @@ +{ + "name": "worker-teardown-os-kill", + "main": "index.js", + "pear": { + "name": "worker-teardown-os-kill", + "type": "terminal" + } +} diff --git a/test/fixtures/teardown/index.js b/test/fixtures/worker-teardown/index.js similarity index 100% rename from test/fixtures/teardown/index.js rename to test/fixtures/worker-teardown/index.js diff --git a/test/fixtures/teardown/package.json b/test/fixtures/worker-teardown/package.json similarity index 52% rename from test/fixtures/teardown/package.json rename to test/fixtures/worker-teardown/package.json index 679785645..22b52079e 100644 --- a/test/fixtures/teardown/package.json +++ b/test/fixtures/worker-teardown/package.json @@ -1,8 +1,8 @@ { - "name": "teardown", + "name": "worker-teardown", "main": "index.js", "pear": { - "name": "teardown", + "name": "worker-teardown", "type": "terminal" } }