blob: e1173985d3fdd016ae3116127d9bdb6ba40a9969 [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.com3e2f8ff2014-10-15 15:01:11 +000019function Test() {
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);
andresp@webrtc.org468516c2014-09-02 10:52:54 +000023}
24
25Test.prototype = {
26 log: function () {
27 console.log.apply(console.log, arguments);
28 },
29
30 abort: function (error) {
houssainy@google.come0761d02014-09-10 14:35:35 +000031 var error = new Error(error || "Test aborted");
andresp@webrtc.org468516c2014-09-02 10:52:54 +000032 console.log(error.stack);
33 process.exit(1);
34 },
35
36 assert: function (value, message) {
37 if (value !== true) {
38 this.abort(message || "Assert failed.");
39 }
40 },
41
42 fail: function () {
43 this.assert(false, "Test failed.");
44 },
45
46 done: function () {
47 clearTimeout(this.timeout_);
48 console.log("Test succeeded");
49 process.exit(0);
50 },
51
52 // Utility method to wait for multiple callbacks to be executed.
53 // functions - array of functions to call with a callback.
54 // doneCallback - called when all callbacks on the array have completed.
55 wait: function (functions, doneCallback) {
56 var result = new Array(functions.length);
57 var missingResult = functions.length;
58 for (var i = 0; i != functions.length; ++i)
59 functions[i](complete.bind(this, i));
60
61 function complete(index, value) {
62 missingResult--;
63 result[index] = value;
64 if (missingResult == 0)
65 doneCallback.apply(null, result);
66 }
67 },
68
houssainy@google.com3e2f8ff2014-10-15 15:01:11 +000069 spawnBot: function (name, botType, doneCallback) {
andresp@webrtc.org468516c2014-09-02 10:52:54 +000070 // Lazy initialization of botmanager.
71 if (!this.botManager_)
72 this.botManager_ = new BotManager();
houssainy@google.com3e2f8ff2014-10-15 15:01:11 +000073 this.botManager_.spawnNewBot(name, botType, doneCallback);
andresp@webrtc.org468516c2014-09-02 10:52:54 +000074 },
houssainy@google.comd0bb5862014-09-30 15:20:15 +000075
76 createStatisticsReport: function (outputFileName) {
77 return new StatisticsReport(outputFileName);
78 },
79}
80
81StatisticsReport = function (outputFileName) {
82 this.output_ = [];
83 this.output_.push("Version: 1");
84 this.outputFileName_ = outputFileName;
85}
86
87StatisticsReport.prototype = {
88 collectStatsFromPeerConnection: function (prefix, pc) {
89 setInterval(this.addPeerConnectionStats.bind(this, prefix, pc), 100);
90 },
91
92 addPeerConnectionStats: function (prefix, pc) {
93 pc.getStats(onStatsReady.bind(this));
94
95 function onStatsReady(reports) {
96 for (index in reports) {
97 var stats = {};
98 stats[reports[index].id] = collectStats(reports[index].stats);
99
100 var data = {};
101 data[prefix] = stats;
102
103 this.output_.push({
104 type: "UpdateCounters",
105 startTime: (new Date()).getTime(),
106 data: data,
107 });
108 }
109 };
110
111 function collectStats(stats) {
112 var outputStats = {};
113 for (index in stats) {
114 var statValue = parseFloat(stats[index].stat);
115 outputStats[stats[index].name] = isNaN(statValue)?
116 stats[index].stat : statValue;
117 }
118 return outputStats;
119 };
120 },
121
122 finish: function (doneCallback) {
houssainy@google.com5d3e7ac2014-10-06 17:21:27 +0000123 fs.exists("test/reports/", function (exists) {
124 if(exists) {
125 writeFile.bind(this)();
126 } else {
127 fs.mkdir("test/reports/", 0777, writeFile.bind(this));
128 }
129 }.bind(this));
130
131 function writeFile () {
132 fs.writeFile("test/reports/" + this.outputFileName_ + "_" +
houssainy@google.comd0bb5862014-09-30 15:20:15 +0000133 (new Date()).getTime() +".json", JSON.stringify(this.output_),
134 doneCallback);
houssainy@google.com5d3e7ac2014-10-06 17:21:27 +0000135 }
houssainy@google.comd0bb5862014-09-30 15:20:15 +0000136 },
andresp@webrtc.org468516c2014-09-02 10:52:54 +0000137}
138
houssainy@google.com3e2f8ff2014-10-15 15:01:11 +0000139function runTest(testfile) {
andresp@webrtc.org468516c2014-09-02 10:52:54 +0000140 console.log("Running test: " + testfile);
141 var script = vm.createScript(fs.readFileSync(testfile), testfile);
houssainy@google.com3e2f8ff2014-10-15 15:01:11 +0000142 script.runInNewContext({ test: new Test(), setInterval: setInterval,
houssainy@google.com24f62e12014-09-29 09:36:28 +0000143 setTimeout: setTimeout });
andresp@webrtc.org468516c2014-09-02 10:52:54 +0000144}
145
houssainy@google.com3e2f8ff2014-10-15 15:01:11 +0000146runTest(process.argv[2]);