Improve node.js test runner: send response after each test, update timeout after each test.

This commit is contained in:
Alexey Andreev 2017-10-26 14:11:56 +03:00
parent 6848984a10
commit 5c436c3391
2 changed files with 54 additions and 24 deletions

View File

@ -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) {

View File

@ -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);