13dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 23dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// 33dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// Use of this source code is governed by a BSD-style license 43dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// that can be found in the LICENSE file in the root of the source 53dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// tree. An additional intellectual property rights grant can be found 63dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// in the file PATENTS. All contributing project authors may 73dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// be found in the AUTHORS file in the root of the source tree. 83dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// 93dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// This script loads the test file in the virtual machine and runs it in a 103dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// context that only exposes a test variable with methods for testing and to 113dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// spawn bots. 123dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// 133dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// Note: an important part of this script is to keep nodejs-isms away from test 143dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// code and isolate it from implementation details. 153dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.orgvar fs = require('fs'); 163dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.orgvar vm = require('vm'); 173dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.orgvar BotManager = require('./botmanager.js'); 183dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org 19de4cf016beed6058f59285ce8cd1e7e5e47be28fhoussainy@google.comfunction Test(botType) { 20881c38a5242bea6edcc7da4186197c52cb690929houssainy@google.com // TODO(houssainy) set the time out. 213dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org this.timeout_ = setTimeout( 223dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org this.fail.bind(this, "Test timeout!"), 23881c38a5242bea6edcc7da4186197c52cb690929houssainy@google.com 10000); 24de4cf016beed6058f59285ce8cd1e7e5e47be28fhoussainy@google.com this.botType_ = botType; 253dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org} 263dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org 273dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.orgTest.prototype = { 283dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org log: function () { 293dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org console.log.apply(console.log, arguments); 303dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org }, 313dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org 323dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org abort: function (error) { 335eaf95be141b9730fc719821c91f159cea757334houssainy@google.com var error = new Error(error || "Test aborted"); 343dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org console.log(error.stack); 353dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org process.exit(1); 363dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org }, 373dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org 383dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org assert: function (value, message) { 393dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org if (value !== true) { 403dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org this.abort(message || "Assert failed."); 413dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org } 423dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org }, 433dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org 443dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org fail: function () { 453dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org this.assert(false, "Test failed."); 463dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org }, 473dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org 483dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org done: function () { 493dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org clearTimeout(this.timeout_); 503dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org console.log("Test succeeded"); 513dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org process.exit(0); 523dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org }, 533dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org 543dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org // Utility method to wait for multiple callbacks to be executed. 553dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org // functions - array of functions to call with a callback. 563dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org // doneCallback - called when all callbacks on the array have completed. 573dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org wait: function (functions, doneCallback) { 583dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org var result = new Array(functions.length); 593dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org var missingResult = functions.length; 603dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org for (var i = 0; i != functions.length; ++i) 613dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org functions[i](complete.bind(this, i)); 623dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org 633dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org function complete(index, value) { 643dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org missingResult--; 653dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org result[index] = value; 663dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org if (missingResult == 0) 673dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org doneCallback.apply(null, result); 683dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org } 693dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org }, 703dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org 713dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org spawnBot: function (name, doneCallback) { 723dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org // Lazy initialization of botmanager. 733dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org if (!this.botManager_) 743dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org this.botManager_ = new BotManager(); 75de4cf016beed6058f59285ce8cd1e7e5e47be28fhoussainy@google.com this.botManager_.spawnNewBot(name, this.botType_, doneCallback); 763dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org }, 773dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org} 783dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org 79604a2ba517b9ccdf671bbcc0e1a7e283b4a6e532houssainy@google.comfunction runTest(botType, testfile) { 803dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org console.log("Running test: " + testfile); 813dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org var script = vm.createScript(fs.readFileSync(testfile), testfile); 82604a2ba517b9ccdf671bbcc0e1a7e283b4a6e532houssainy@google.com script.runInNewContext({ test: new Test(botType) }); 833dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org} 843dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org 85604a2ba517b9ccdf671bbcc0e1a7e283b4a6e532houssainy@google.comrunTest(process.argv[2], process.argv[3]); 86