From 5c436c3391e24a2fa0e203f2844e0d4e8bef3afe Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 26 Oct 2017 14:11:56 +0300 Subject: [PATCH] Improve node.js test runner: send response after each test, update timeout after each test. --- tests/src/test/js/client.js | 29 ++++++++---------- tests/src/test/js/src/run-tests.js | 49 +++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/tests/src/test/js/client.js b/tests/src/test/js/client.js index 58eb208c2..78219a5e9 100644 --- a/tests/src/test/js/client.js +++ b/tests/src/test/js/client.js @@ -34,29 +34,26 @@ function tryConnect() { function listen(ws) { ws.onmessage = (event) => { - let resultConsumer = []; let request = JSON.parse(event.data); console.log("Request #" + request.id + " received"); - runTests(request.tests, resultConsumer, 0, () => { - console.log("Sending response #" + request.id); - ws.send(JSON.stringify({ - id: request.id, - result: resultConsumer - })); - }); + runTests(ws, request.id, request.tests, 0); } } -function runTests(tests, consumer, index, callback) { +function runTests(ws, suiteId, tests, index) { if (index === tests.length) { - callback(); - } else { - let test = tests[index]; - runSingleTest(test, result => { - consumer.push(result); - runTests(tests, consumer, index + 1, callback); - }); + return; } + let test = tests[index]; + runSingleTest(test, result => { + console.log("Sending response #" + suiteId); + ws.send(JSON.stringify({ + id: suiteId, + index: index, + result: result + })); + runTests(ws, suiteId, tests, index + 1); + }); } function runSingleTest(test, callback) { diff --git a/tests/src/test/js/src/run-tests.js b/tests/src/test/js/src/run-tests.js index 2b3ce25d2..8d1e277af 100644 --- a/tests/src/test/js/src/run-tests.js +++ b/tests/src/test/js/src/run-tests.js @@ -127,10 +127,15 @@ class TestRunner { this.pendingRequests = Object.create(null); this.requestIdGen = 0; + this.timeout = null; + this.ws.on("message", (message) => { const response = JSON.parse(message.utf8Data); - const pendingRequest = this.pendingRequests[response.id]; + const pendingRequest = this.pendingRequests[response.id + "-" + response.index]; delete this.pendingRequests[response.id]; + if (this.timeout) { + this.timeout.refresh(); + } pendingRequest(response.result); }) } @@ -149,14 +154,17 @@ class TestRunner { }); this.testsRun += suite.testCases.length; - const resultPromise = new Promise(resolve => { - this.pendingRequests[request.id] = resolve; - }); - const timeoutPromise = new Promise((_, reject) => { - setTimeout(() => reject(new Error("connection timeout")), 120000); - }); + const resultPromises = []; + + this.timeout = createRefreshableTimeoutPromise(10000); + for (let i = 0; i < suite.testCases.length; ++i) { + resultPromises.push(new Promise(resolve => { + this.pendingRequests[request.id + "-" + i] = resolve; + })); + } + this.ws.send(JSON.stringify(request)); - const result = await Promise.race([resultPromise, timeoutPromise]); + const result = await Promise.race([Promise.all(resultPromises), this.timeout.promise]); for (let i = 0; i < suite.testCases.length; i++) { const testCase = suite.testCases[i]; @@ -264,6 +272,31 @@ function runTeaVM() { }) } + +function createRefreshableTimeoutPromise(timeout) { + const obj = { + currentId: 0, + resolveFun: () => {}, + refresh: () => { + const expectedId = ++obj.currentId; + setTimeout(() => { + if (expectedId === obj.currentId) { + obj.resolveFun(new Error("Connection timeout")) + } + }, timeout); + } + }; + + const result = { + promise: new Promise((_, reject) => { + obj.resolveFun = reject; + }), + refresh: () => obj.refresh() + }; + obj.refresh(); + return result; +} + runAll().catch(e => { console.log(e.stack); process.exit(1);