andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 1 | // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 2 | // |
| 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the LICENSE file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
| 8 | // |
| 9 | // This script loads the test file in the virtual machine and runs it in a |
| 10 | // context that only exposes a test variable with methods for testing and to |
| 11 | // spawn bots. |
| 12 | // |
| 13 | // Note: an important part of this script is to keep nodejs-isms away from test |
| 14 | // code and isolate it from implementation details. |
| 15 | var fs = require('fs'); |
| 16 | var vm = require('vm'); |
| 17 | var BotManager = require('./botmanager.js'); |
| 18 | |
houssainy@google.com | c77e4d6 | 2014-09-08 10:36:11 +0000 | [diff] [blame] | 19 | function Test(botType) { |
houssainy@google.com | 07ca949 | 2014-09-22 13:52:39 +0000 | [diff] [blame] | 20 | // TODO(houssainy) set the time out. |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 21 | this.timeout_ = setTimeout( |
| 22 | this.fail.bind(this, "Test timeout!"), |
houssainy@google.com | 07ca949 | 2014-09-22 13:52:39 +0000 | [diff] [blame] | 23 | 10000); |
houssainy@google.com | c77e4d6 | 2014-09-08 10:36:11 +0000 | [diff] [blame] | 24 | this.botType_ = botType; |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | Test.prototype = { |
| 28 | log: function () { |
| 29 | console.log.apply(console.log, arguments); |
| 30 | }, |
| 31 | |
| 32 | abort: function (error) { |
houssainy@google.com | e0761d0 | 2014-09-10 14:35:35 +0000 | [diff] [blame] | 33 | var error = new Error(error || "Test aborted"); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 34 | console.log(error.stack); |
| 35 | process.exit(1); |
| 36 | }, |
| 37 | |
| 38 | assert: function (value, message) { |
| 39 | if (value !== true) { |
| 40 | this.abort(message || "Assert failed."); |
| 41 | } |
| 42 | }, |
| 43 | |
| 44 | fail: function () { |
| 45 | this.assert(false, "Test failed."); |
| 46 | }, |
| 47 | |
| 48 | done: function () { |
| 49 | clearTimeout(this.timeout_); |
| 50 | console.log("Test succeeded"); |
| 51 | process.exit(0); |
| 52 | }, |
| 53 | |
| 54 | // Utility method to wait for multiple callbacks to be executed. |
| 55 | // functions - array of functions to call with a callback. |
| 56 | // doneCallback - called when all callbacks on the array have completed. |
| 57 | wait: function (functions, doneCallback) { |
| 58 | var result = new Array(functions.length); |
| 59 | var missingResult = functions.length; |
| 60 | for (var i = 0; i != functions.length; ++i) |
| 61 | functions[i](complete.bind(this, i)); |
| 62 | |
| 63 | function complete(index, value) { |
| 64 | missingResult--; |
| 65 | result[index] = value; |
| 66 | if (missingResult == 0) |
| 67 | doneCallback.apply(null, result); |
| 68 | } |
| 69 | }, |
| 70 | |
| 71 | spawnBot: function (name, doneCallback) { |
| 72 | // Lazy initialization of botmanager. |
| 73 | if (!this.botManager_) |
| 74 | this.botManager_ = new BotManager(); |
houssainy@google.com | c77e4d6 | 2014-09-08 10:36:11 +0000 | [diff] [blame] | 75 | this.botManager_.spawnNewBot(name, this.botType_, doneCallback); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 76 | }, |
| 77 | } |
| 78 | |
houssainy@google.com | 35850ff | 2014-09-22 15:24:56 +0000 | [diff] [blame] | 79 | function runTest(botType, testfile) { |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 80 | console.log("Running test: " + testfile); |
| 81 | var script = vm.createScript(fs.readFileSync(testfile), testfile); |
houssainy@google.com | 24f62e1 | 2014-09-29 09:36:28 +0000 | [diff] [blame] | 82 | script.runInNewContext({ test: new Test(botType), setInterval: setInterval |
| 83 | setTimeout: setTimeout }); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 84 | } |
| 85 | |
houssainy@google.com | 35850ff | 2014-09-22 15:24:56 +0000 | [diff] [blame] | 86 | runTest(process.argv[2], process.argv[3]); |