Home | History | Annotate | Download | only in rtcbot
      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 
     19 function Test(botType) {
     20   // TODO(houssainy) set the time out.
     21   this.timeout_ = setTimeout(
     22       this.fail.bind(this, "Test timeout!"),
     23       10000);
     24   this.botType_ = botType;
     25 }
     26 
     27 Test.prototype = {
     28   log: function () {
     29     console.log.apply(console.log, arguments);
     30   },
     31 
     32   abort: function (error) {
     33     var error = new Error(error || "Test aborted");
     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();
     75     this.botManager_.spawnNewBot(name, this.botType_, doneCallback);
     76   },
     77 }
     78 
     79 function runTest(botType, testfile) {
     80   console.log("Running test: " + testfile);
     81   var script = vm.createScript(fs.readFileSync(testfile), testfile);
     82   script.runInNewContext({ test: new Test(botType) });
     83 }
     84 
     85 runTest(process.argv[2], process.argv[3]);
     86