blob: 83bb39f4c800e31de1694e4aed7eb1f3f88767a5 [file] [log] [blame]
andresp@webrtc.org468516c2014-09-02 10:52:54 +00001// 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.
15var fs = require('fs');
16var vm = require('vm');
17var BotManager = require('./botmanager.js');
18
19function Test() {
20 // Make the test fail if not completed in 3 seconds.
21 this.timeout_ = setTimeout(
22 this.fail.bind(this, "Test timeout!"),
23 3000);
24}
25
26Test.prototype = {
27 log: function () {
28 console.log.apply(console.log, arguments);
29 },
30
31 abort: function (error) {
32 var error = error || new Error("Test aborted");
33 console.log(error.stack);
34 process.exit(1);
35 },
36
37 assert: function (value, message) {
38 if (value !== true) {
39 this.abort(message || "Assert failed.");
40 }
41 },
42
43 fail: function () {
44 this.assert(false, "Test failed.");
45 },
46
47 done: function () {
48 clearTimeout(this.timeout_);
49 console.log("Test succeeded");
50 process.exit(0);
51 },
52
53 // Utility method to wait for multiple callbacks to be executed.
54 // functions - array of functions to call with a callback.
55 // doneCallback - called when all callbacks on the array have completed.
56 wait: function (functions, doneCallback) {
57 var result = new Array(functions.length);
58 var missingResult = functions.length;
59 for (var i = 0; i != functions.length; ++i)
60 functions[i](complete.bind(this, i));
61
62 function complete(index, value) {
63 missingResult--;
64 result[index] = value;
65 if (missingResult == 0)
66 doneCallback.apply(null, result);
67 }
68 },
69
70 spawnBot: function (name, doneCallback) {
71 // Lazy initialization of botmanager.
72 if (!this.botManager_)
73 this.botManager_ = new BotManager();
74 this.botManager_.spawnNewBot(name, doneCallback);
75 },
76}
77
78function runTest(testfile) {
79 console.log("Running test: " + testfile);
80 var script = vm.createScript(fs.readFileSync(testfile), testfile);
81 script.runInNewContext({ test: new Test() });
82}
83
84runTest("./test/simple_offer_answer.js");