From 3577bc56d72bacee5b03e2fa3b5f8152157a3eef Mon Sep 17 00:00:00 2001 From: littlefairy <2195134613@qq.com> Date: Tue, 19 Mar 2024 13:08:20 +0800 Subject: [PATCH] test child.js --- node_run/child.js | 57 ++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/node_run/child.js b/node_run/child.js index 00757e0..4387882 100644 --- a/node_run/child.js +++ b/node_run/child.js @@ -77,6 +77,27 @@ const cqd_name_list = [ 'Iwn7D3Ol7uWKIfTC@XJ联队' ] +function createJSDOMForOpponent(opponent, name, mode, testString, config, localStorageMock, path, results) { + return new Promise((resolve) => { + new JSDOM(fs.readFileSync(path.join(__dirname, 'static/md5.html'), 'utf-8'), { + url: 'file://' + path.join(__dirname, 'static/'), + runScripts: 'dangerously', + resources: 'usable', + beforeParse(window) { + window.FakelocalStorage = localStorageMock; + window.config = config[mode].thresholds; + window.name_input = testString[mode].replace(/\$name1/g, name).replace(/\$name2/g, opponent); + window.stage = 0; + window.skillData = []; + window.resolve = (...args) => { + results.push(args[2]); + window.close(); + resolve(); // 解决Promise,表示当前JSDOM操作完成 + } + } + }); + }); + } console.log(chalk`{gray child {bold ${id}} started.}`); process.on('message', ([mode, name]) => { @@ -102,32 +123,16 @@ process.on('message', ([mode, name]) => { }; } }); - } else if (mode == "CQD") { - var results = [] - for (i = 0; i < cqd_name_list.length; i++) { - var opponent = cqd_name_list[i] - new JSDOM(fs.readFileSync(path.join(__dirname, 'static/md5.html'), 'utf-8'), { - url: 'file://' + path.join(__dirname, 'static/'), - runScripts: 'dangerously', - resources: 'usable', - beforeParse(window) { - window.FakelocalStorage = localStorageMock; - window.config = config[mode].thresholds; - window.name_input = testString[mode].replace(/\$name1/g, name).replace(/\$name2/g, opponent); - window.stage = 0; - window.skillData = []; - window.resolve = (...args) => { - console.log(results) - results.push(args[2]); - window.close(); - }; - window.output = (...message) => { - console.log('page:', message); - }; - } - }); - } - + }else if(mode == "CQD"){ + var results=[]; + (async () => { + for (let i = 0; i < cqd_name_list.length; i++) { + var opponent = cqd_name_list[i]; + // 等待当前的JSDOM操作完成 + await createJSDOMForOpponent(opponent, name, mode, testString, config, localStorageMock, path, results); + } + })(); + console.log(results) } } catch (e) {