1 : /* 2 : Copyright (c) 2011, Yahoo! Inc. 3 : All rights reserved. 4 : 5 : Redistribution and use of this software in source and binary forms, 6 : with or without modification, are permitted provided that the following 7 : conditions are met: 8 : 9 : * Redistributions of source code must retain the above 10 : copyright notice, this list of conditions and the 11 : following disclaimer. 12 : 13 : * Redistributions in binary form must reproduce the above 14 : copyright notice, this list of conditions and the 15 : following disclaimer in the documentation and/or other 16 : materials provided with the distribution. 17 : 18 : * Neither the name of Yahoo! Inc. nor the names of its 19 : contributors may be used to endorse or promote products 20 : derived from this software without specific prior 21 : written permission of Yahoo! Inc. 22 : 23 : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 24 : IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 : TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 26 : PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 : OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 : SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 : LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 : DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 : THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 : (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 : OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 : */ 35 : 36 : 37 1 : module.exports = { 38 : Create: function(hub, common, glob) { 39 : // Javascript is single threaded! We don't have to worry about concurrency! 40 1 : var path = require('path'); 41 : 42 : // Events I care about 43 1 : hub.addListener('action:get_test', getTest); 44 : 45 1 : function getTest(req, res, cache) { 46 6 : var browser = req.session.uuid, 47 : bName = common.browserName(req), 48 : now = new Date().getTime(), 49 : testURL; 50 : 51 6 : if (!cache.browsers[browser]) { 52 6 : cache.browsers[browser] = {}; 53 : } 54 : 55 6 : cache.browsers[browser].get_test = now; 56 : 57 6 : hub.emit(hub.LOG, hub.INFO, 'Getting test for ' + bName); 58 : 59 6 : for (var i = 0; i < cache.tests_to_run.length; i++) { 60 4 : var test = cache.tests_to_run[i]; 61 4 : if ((test.browser == browser) && test.running) { 62 : // um you're already running this test! 63 : // must be something wrong with it - pop it 64 1 : var error = 'Skipping bad test: ' + test.url + ': we thought it was running!'; 65 1 : hub.emit(hub.LOG, hub.ERROR, error); 66 1 : if (test.sendOutput) { 67 1 : res.write(error); 68 : } 69 1 : cache.tests_to_run.splice(i, 1); 70 1 : i--; 71 1 : continue; 72 : } 73 : 74 : // Check if this is a Selenium test for this Selenium browser 75 : // if so then assign it to this browser 76 3 : if (test.browser == req.session.seleniumUUID) { 77 : // The Selenium host 78 1 : test.browser = browser; 79 : } 80 : 81 : // This test already running in another browser 82 3 : if (test.browser != browser) continue; 83 : 84 : // Otherwise start running this test in capture mode!! 85 2 : test.running = now; 86 2 : testURL = test.url; 87 2 : break; 88 : } 89 : 90 6 : if (testURL) { 91 2 : res.end(JSON.stringify({ testLocation: testURL })); 92 2 : hub.emit(hub.LOG, hub.INFO, "Sending test url: " + testURL); 93 : } else { 94 : // find all local tests 95 4 : var prefix = hub.config.testDir, 96 : webPrefix = hub.config.testDirWeb, 97 : local_test_files = hub.config.testRegex, 98 : full_find = path.join(prefix, '**', local_test_files), 99 : matches = glob.globSync(full_find), 100 : data = []; 101 4 : ; 102 : 103 : // No tests for me - end if we're a Selenium browser 104 4 : if (req.session.seleniumUUID) { 105 : // Selenium job all done!! 106 1 : hub.emit('seleniumTestsFinished'); 107 : } 108 : 109 4 : matches.forEach(function(testFile) { 110 4 : testFile = testFile.replace(prefix, ''); 111 4 : data.push({ test_url: testFile }); 112 : }); 113 : 114 4 : res.end(JSON.stringify({ availableTests: data, config: hub.config })); 115 : } 116 : } 117 : } 118 : }; 119 :