blob: 7e9c3354453482213cfd8314bce7aee3f17f6b33 [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
houssainy@google.comc77e4d62014-09-08 10:36:11 +000019function Test(botType) {
andresp@webrtc.org468516c2014-09-02 10:52:54 +000020 this.timeout_ = setTimeout(
21 this.fail.bind(this, "Test timeout!"),
houssainy@google.comd0bb5862014-09-30 15:20:15 +000022 100000);
houssainy@google.comc77e4d62014-09-08 10:36:11 +000023 this.botType_ = botType;
andresp@webrtc.org468516c2014-09-02 10:52:54 +000024}
25
26Test.prototype = {
27 log: function () {
28 console.log.apply(console.log, arguments);
29 },
30
31 abort: function (error) {
houssainy@google.come0761d02014-09-10 14:35:35 +000032 var error = new Error(error || "Test aborted");
andresp@webrtc.org468516c2014-09-02 10:52:54 +000033 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();
houssainy@google.comc77e4d62014-09-08 10:36:11 +000074 this.botManager_.spawnNewBot(name, this.botType_, doneCallback);
andresp@webrtc.org468516c2014-09-02 10:52:54 +000075 },
houssainy@google.comd0bb5862014-09-30 15:20:15 +000076
77 createStatisticsReport: function (outputFileName) {
78 return new StatisticsReport(outputFileName);
79 },
80}
81
82StatisticsReport = function (outputFileName) {
83 this.output_ = [];
84 this.output_.push("Version: 1");
85 this.outputFileName_ = outputFileName;
86}
87
88StatisticsReport.prototype = {
89 collectStatsFromPeerConnection: function (prefix, pc) {
90 setInterval(this.addPeerConnectionStats.bind(this, prefix, pc), 100);
91 },
92
93 addPeerConnectionStats: function (prefix, pc) {
94 pc.getStats(onStatsReady.bind(this));
95
96 function onStatsReady(reports) {
97 for (index in reports) {
98 var stats = {};
99 stats[reports[index].id] = collectStats(reports[index].stats);
100
101 var data = {};
102 data[prefix] = stats;
103
104 this.output_.push({
105 type: "UpdateCounters",
106 startTime: (new Date()).getTime(),
107 data: data,
108 });
109 }
110 };
111
112 function collectStats(stats) {
113 var outputStats = {};
114 for (index in stats) {
115 var statValue = parseFloat(stats[index].stat);
116 outputStats[stats[index].name] = isNaN(statValue)?
117 stats[index].stat : statValue;
118 }
119 return outputStats;
120 };
121 },
122
123 finish: function (doneCallback) {
124 fs.writeFile("test/reports/" + this.outputFileName_ + "_" +
125 (new Date()).getTime() +".json", JSON.stringify(this.output_),
126 doneCallback);
127 },
andresp@webrtc.org468516c2014-09-02 10:52:54 +0000128}
129
houssainy@google.com35850ff2014-09-22 15:24:56 +0000130function runTest(botType, testfile) {
andresp@webrtc.org468516c2014-09-02 10:52:54 +0000131 console.log("Running test: " + testfile);
132 var script = vm.createScript(fs.readFileSync(testfile), testfile);
houssainy@google.comd0bb5862014-09-30 15:20:15 +0000133 script.runInNewContext({ test: new Test(botType), setInterval: setInterval,
houssainy@google.com24f62e12014-09-29 09:36:28 +0000134 setTimeout: setTimeout });
andresp@webrtc.org468516c2014-09-02 10:52:54 +0000135}
136
houssainy@google.com35850ff2014-09-22 15:24:56 +0000137runTest(process.argv[2], process.argv[3]);