|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const cheerio = require('cheerio') |
| 4 | +const { spawn } = require('child_process') |
| 5 | +const path = require('path') |
4 | 6 | const test = require('ava') |
5 | 7 |
|
6 | 8 | const { loadIframe } = require('..') |
@@ -39,3 +41,45 @@ test('markup is correct', async t => { |
39 | 41 | ) |
40 | 42 | t.snapshot(normalizeTransistorAssetUrls($.html())) |
41 | 43 | }) |
| 44 | + |
| 45 | +test('worker does not keep process alive after resolving', async t => { |
| 46 | + const script = ` |
| 47 | + const cheerio = require('cheerio') |
| 48 | + const { loadIframe } = require('./src') |
| 49 | + ;(async () => { |
| 50 | + const src = 'data:text/html,<html><body><script>setInterval(() => {}, 1000)<\\\\/script></body></html>' |
| 51 | + const $ = cheerio.load(\`<iframe src="\${src}"></iframe>\`) |
| 52 | + await loadIframe('https://example.com', $, { timeout: 200 }) |
| 53 | + })().catch(error => { |
| 54 | + console.error(error) |
| 55 | + process.exit(1) |
| 56 | + }) |
| 57 | + ` |
| 58 | + |
| 59 | + await new Promise((resolve, reject) => { |
| 60 | + const child = spawn(process.execPath, ['-e', script], { |
| 61 | + cwd: path.resolve(__dirname, '..'), |
| 62 | + stdio: ['ignore', 'ignore', 'pipe'] |
| 63 | + }) |
| 64 | + |
| 65 | + let stderr = '' |
| 66 | + child.stderr.on('data', chunk => { |
| 67 | + stderr += String(chunk) |
| 68 | + }) |
| 69 | + |
| 70 | + const timeoutId = setTimeout(() => { |
| 71 | + child.kill('SIGKILL') |
| 72 | + reject( |
| 73 | + new Error('Child process did not exit in time after loadIframe resolve') |
| 74 | + ) |
| 75 | + }, 3000) |
| 76 | + |
| 77 | + child.once('exit', code => { |
| 78 | + clearTimeout(timeoutId) |
| 79 | + if (code === 0) return resolve() |
| 80 | + reject(new Error(`Child process failed with code ${code}: ${stderr}`)) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + t.pass() |
| 85 | +}) |
0 commit comments