From 3001009d3fe8b6f4efd1e2f73313a945309355e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Mon, 11 Aug 2014 11:27:50 +0200 Subject: [PATCH] Auto retry twice if test failed --- app/lib/phantomasWrapper.js | 25 ++++++- app/lib/testQueue.js | 5 +- app/node_controllers/launchTestController.js | 3 +- app/node_controllers/waitingQueueSocket.js | 7 ++ app/node_views/launchTest.html | 5 ++ app/node_views/results.html | 6 +- .../{resultsController.js => resultsCtrl.js} | 0 phantomas_custom/modules/domQYLT/domQYLT.js | 68 +++++++++++-------- 8 files changed, 85 insertions(+), 34 deletions(-) rename app/public/scripts/{resultsController.js => resultsCtrl.js} (100%) diff --git a/app/lib/phantomasWrapper.js b/app/lib/phantomasWrapper.js index a33401c..b866fe2 100644 --- a/app/lib/phantomasWrapper.js +++ b/app/lib/phantomasWrapper.js @@ -2,7 +2,7 @@ * Yellow Lab Tools main file */ -var q = require ('q'); +var async = require('async'); var phantomas = require('phantomas'); var PhantomasWrapper = function() { @@ -59,7 +59,28 @@ var PhantomasWrapper = function() { }; // It's time to launch the test!!! - phantomas(task.url, options, callback); + var triesNumber = 3; + + async.retry(triesNumber, function(cb) { + phantomas(task.url, options, function(err, json, results) { + console.log('Returning from Phantomas'); + + // Adding some YellowLabTools errors here + if (!json.metrics.javascriptExecutionTree) { + err = 1001; + } + + if (err) { + console.log('Attempt failed for test id ' + task.testId + '. Error code ' + err); + } + cb(err, {json: json, results: results}); + }); + }, function(err, data) { + if (err) { + console.log('All ' + triesNumber + ' attemps failed for test id ' + task.testId); + } + callback(err, data.json, data.results); + }); }; }; diff --git a/app/lib/testQueue.js b/app/lib/testQueue.js index 82ebd3e..3e32370 100644 --- a/app/lib/testQueue.js +++ b/app/lib/testQueue.js @@ -51,10 +51,13 @@ var testQueue = function() { return position; }; - // Forward testComplete this.testComplete = function(testId) { self.emit('testComplete', testId); }; + + this.testFailed = function(testId) { + self.emit('testFailed', testId); + } }; // extend the EventEmitter class using our Radio class diff --git a/app/node_controllers/launchTestController.js b/app/node_controllers/launchTestController.js index 2b97359..d0c04be 100644 --- a/app/node_controllers/launchTestController.js +++ b/app/node_controllers/launchTestController.js @@ -65,7 +65,7 @@ var launchTestController = function(req, res, testQueue) { ], function(err) { if (err) { - console.log('An error occured while launching the phantomas test : ', err); + console.log('An error occured in the phantomas test: ', err); fs.writeFile(phantomasResultsPath, JSON.stringify({url: url, error: err}, null, 4), function(err) { if (err) { @@ -73,6 +73,7 @@ var launchTestController = function(req, res, testQueue) { console.log(err); } }); + testQueue.testFailed(testId); } else { testQueue.testComplete(testId); } diff --git a/app/node_controllers/waitingQueueSocket.js b/app/node_controllers/waitingQueueSocket.js index 5fdf919..b8fd30f 100644 --- a/app/node_controllers/waitingQueueSocket.js +++ b/app/node_controllers/waitingQueueSocket.js @@ -18,6 +18,13 @@ var waitingQueueSocket = function(socket, testQueue) { } }); + testQueue.on('testFailed', function(id) { + if (testId === id) { + socket.emit('failed'); + console.log('Sending failed event to test id ' + testId); + } + }); + testQueue.on('queueMoving', function() { var positionInQueue = testQueue.indexOf(testId); if (positionInQueue >= 0) { diff --git a/app/node_views/launchTest.html b/app/node_views/launchTest.html index a46c48f..4c005cc 100644 --- a/app/node_views/launchTest.html +++ b/app/node_views/launchTest.html @@ -35,6 +35,11 @@ window.location.replace('/results/' + testId); }); + socket.on('failed', function() { + statusElement.innerHTML = 'Test failed'; + window.location.replace('/results/' + testId); + }); + socket.on('404', function() { statusElement.innerHTML = 'Test not found'; }); diff --git a/app/node_views/results.html b/app/node_views/results.html index 12166fb..2a31763 100644 --- a/app/node_views/results.html +++ b/app/node_views/results.html @@ -7,7 +7,7 @@ - + @@ -18,13 +18,13 @@
Tested url: {{phantomasResults.url}}
-
+

Error: {{phantomasResults.error}}

Phantomas timed out
Phantomas config error
Phantomas failed to load page
Phantomas internal error
-
Javascript execution tree error
+
Javascript execution tree error
diff --git a/app/public/scripts/resultsController.js b/app/public/scripts/resultsCtrl.js similarity index 100% rename from app/public/scripts/resultsController.js rename to app/public/scripts/resultsCtrl.js diff --git a/phantomas_custom/modules/domQYLT/domQYLT.js b/phantomas_custom/modules/domQYLT/domQYLT.js index 34a095e..0d1c9fc 100644 --- a/phantomas_custom/modules/domQYLT/domQYLT.js +++ b/phantomas_custom/modules/domQYLT/domQYLT.js @@ -20,13 +20,13 @@ exports.module = function(phantomas) { phantomas.once('init', function() { phantomas.evaluate(function() { (function(phantomas) { - function querySpy(type, query, fnName) { - phantomas.emit('domQuery', type, query, fnName); // @desc DOM query has been made + function querySpy(type, query, fnName, context) { + phantomas.emit('domQuery', type, query, fnName, context); // @desc DOM query has been made } phantomas.spy(Document.prototype, 'getElementById', function(id) { phantomas.incrMetric('DOMqueriesById'); - querySpy('id', '#' + id, 'getElementById'); + querySpy('id', '#' + id, 'getElementById', '#document'); phantomas.enterContext({ type: 'getElementById', @@ -46,15 +46,17 @@ exports.module = function(phantomas) { function selectorClassNameSpyBefore(className) { /*jshint validthis: true */ + var context = phantomas.getDOMPath(this); + phantomas.incrMetric('DOMqueriesByClassName'); phantomas.addOffender('DOMqueriesByClassName', '.' + className); - querySpy('class', '.' + className, 'getElementsByClassName'); + querySpy('class', '.' + className, 'getElementsByClassName', context); phantomas.enterContext({ type: 'getElementsByClassName', callDetails: { context: { - domElement: phantomas.getDOMPath(this) + domElement: context }, arguments: ['.' + className] }, @@ -70,15 +72,17 @@ exports.module = function(phantomas) { function selectorTagNameSpyBefore(tagName) { /*jshint validthis: true */ + var context = phantomas.getDOMPath(this); + phantomas.incrMetric('DOMqueriesByTagName'); phantomas.addOffender('DOMqueriesByTagName', tagName); - querySpy('tag name', tagName, 'getElementsByTagName'); + querySpy('tag name', tagName, 'getElementsByTagName', context); phantomas.enterContext({ type: 'getElementsByTagName', callDetails: { context: { - domElement: phantomas.getDOMPath(this) + domElement: context }, arguments: [tagName] }, @@ -91,7 +95,7 @@ exports.module = function(phantomas) { phantomas.spy(Element.prototype, 'getElementsByTagName', selectorTagNameSpyBefore, phantomas.leaveContext); // selector queries - function selectorQuerySpy(selector) { + function selectorQuerySpy(selector, context) { phantomas.incrMetric('DOMqueriesByQuerySelectorAll'); phantomas.addOffender('DOMqueriesByQuerySelectorAll', selector); querySpy('selector', selector, 'querySelectorAll'); @@ -100,13 +104,14 @@ exports.module = function(phantomas) { function selectorQuerySpyBefore(selector) { /*jshint validthis: true */ - selectorQuerySpy(selector); + var context = phantomas.getDOMPath(this); + selectorQuerySpy(selector, context); phantomas.enterContext({ type: 'querySelector', callDetails: { context: { - domElement: phantomas.getDOMPath(this) + domElement: context }, arguments: [selector] }, @@ -118,13 +123,14 @@ exports.module = function(phantomas) { function selectorAllQuerySpyBefore(selector) { /*jshint validthis: true */ - selectorQuerySpy(selector); + var context = phantomas.getDOMPath(this); + selectorQuerySpy(selector, context); phantomas.enterContext({ type: 'querySelectorAll', callDetails: { context: { - domElement: phantomas.getDOMPath(this) + domElement: context }, arguments: [selector] }, @@ -140,7 +146,7 @@ exports.module = function(phantomas) { // count DOM inserts - function appendChild(child) { + function appendChild(child, context, appended) { /*jshint validthis: true */ // ignore appending to the node that's not yet added to DOM tree @@ -148,9 +154,6 @@ exports.module = function(phantomas) { return; } - var destNodePath = phantomas.getDOMPath(this), - appendedNodePath = phantomas.getDOMPath(child); - // don't count elements added to fragments as a DOM inserts (issue #350) // DocumentFragment > div[0] if (destNodePath.indexOf('DocumentFragment') === 0) { @@ -158,23 +161,25 @@ exports.module = function(phantomas) { } phantomas.incrMetric('DOMinserts'); - phantomas.addOffender('DOMinserts', '"%s" appended to "%s"', appendedNodePath, destNodePath); + phantomas.addOffender('DOMinserts', '"%s" appended to "%s"', appended, context); - phantomas.log('DOM insert: node "%s" appended to "%s"', appendedNodePath, destNodePath); + phantomas.log('DOM insert: node "%s" appended to "%s"', appended, context); } function appendChildSpyBefore(child) { /*jshint validthis: true */ - appendChild(child); + var context = phantomas.getDOMPath(this); + var appended = phantomas.getDOMPath(child); + appendChild(child, context, appended); phantomas.enterContext({ type: 'appendChild', callDetails: { context: { - domElement: phantomas.getDOMPath(this) + domElement: context }, - arguments: [phantomas.getDOMPath(child)] + arguments: [appended] }, caller: phantomas.getCaller(1), backtrace: phantomas.getBacktrace() @@ -184,15 +189,17 @@ exports.module = function(phantomas) { function insertBeforeSpyBefore(child) { /*jshint validthis: true */ - appendChild(child); + var context = phantomas.getDOMPath(this); + var appended = phantomas.getDOMPath(child); + appendChild(child, context, appended); phantomas.enterContext({ type: 'insertBefore', callDetails: { context: { - domElement: phantomas.getDOMPath(this) + domElement: context }, - arguments: [phantomas.getDOMPath(child)] + arguments: [appended] }, caller: phantomas.getCaller(1), backtrace: phantomas.getBacktrace() @@ -210,11 +217,18 @@ exports.module = function(phantomas) { var Collection = require('../../../node_modules/phantomas/lib/collection'), DOMqueries = new Collection(); - phantomas.on('domQuery', function(type, query, fnName) { - phantomas.log('DOM query: by %s - "%s" (using %s)', type, query, fnName); + phantomas.on('domQuery', function(type, query, fnName, context) { + phantomas.log('DOM query: by %s - "%s" (using %s on context %s)', type, query, fnName, context); phantomas.incrMetric('DOMqueries'); - DOMqueries.push(type + ' "' + query + '"'); + var domQuery = { + type: type, + query: query, + fnName: fnName, + context: context + }; + + DOMqueries.push(JSON.stringify(domQuery)); }); phantomas.on('report', function() {