blob: 33ef960b29554ffaff55aa900a599d0a23f44799 [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//
andresp@webrtc.org458c2c32014-10-16 07:36:37 +00009// Provides a Test class that exposes api to the tests.
10// Read test.prototype to see what methods are exposed.
andresp@webrtc.org468516c2014-09-02 10:52:54 +000011var fs = require('fs');
houssainy@google.com0371a372014-10-17 16:43:50 +000012var request = require('request');
andresp@webrtc.org468516c2014-09-02 10:52:54 +000013var BotManager = require('./botmanager.js');
14
houssainy@google.com3e2f8ff2014-10-15 15:01:11 +000015function Test() {
andresp@webrtc.org468516c2014-09-02 10:52:54 +000016 this.timeout_ = setTimeout(
17 this.fail.bind(this, "Test timeout!"),
houssainy@google.comd0bb5862014-09-30 15:20:15 +000018 100000);
andresp@webrtc.org468516c2014-09-02 10:52:54 +000019}
20
21Test.prototype = {
22 log: function () {
23 console.log.apply(console.log, arguments);
24 },
25
26 abort: function (error) {
houssainy@google.come0761d02014-09-10 14:35:35 +000027 var error = new Error(error || "Test aborted");
andresp@webrtc.org468516c2014-09-02 10:52:54 +000028 console.log(error.stack);
29 process.exit(1);
30 },
31
32 assert: function (value, message) {
33 if (value !== true) {
34 this.abort(message || "Assert failed.");
35 }
36 },
37
38 fail: function () {
39 this.assert(false, "Test failed.");
40 },
41
42 done: function () {
43 clearTimeout(this.timeout_);
44 console.log("Test succeeded");
45 process.exit(0);
46 },
47
48 // Utility method to wait for multiple callbacks to be executed.
49 // functions - array of functions to call with a callback.
50 // doneCallback - called when all callbacks on the array have completed.
51 wait: function (functions, doneCallback) {
52 var result = new Array(functions.length);
53 var missingResult = functions.length;
54 for (var i = 0; i != functions.length; ++i)
55 functions[i](complete.bind(this, i));
56
57 function complete(index, value) {
58 missingResult--;
59 result[index] = value;
60 if (missingResult == 0)
61 doneCallback.apply(null, result);
62 }
63 },
64
houssainy@google.com3e2f8ff2014-10-15 15:01:11 +000065 spawnBot: function (name, botType, doneCallback) {
andresp@webrtc.org468516c2014-09-02 10:52:54 +000066 // Lazy initialization of botmanager.
67 if (!this.botManager_)
68 this.botManager_ = new BotManager();
houssainy@google.com3e2f8ff2014-10-15 15:01:11 +000069 this.botManager_.spawnNewBot(name, botType, doneCallback);
andresp@webrtc.org468516c2014-09-02 10:52:54 +000070 },
houssainy@google.comd0bb5862014-09-30 15:20:15 +000071
72 createStatisticsReport: function (outputFileName) {
73 return new StatisticsReport(outputFileName);
74 },
houssainy@google.com0371a372014-10-17 16:43:50 +000075
76 // Ask computeengineondemand to give us TURN server credentials and URIs.
77 createTurnConfig: function (onSuccess, onError) {
78 request('https://computeengineondemand.appspot.com/turn?username=1234&key=5678',
79 function (error, response, body) {
80 if (error || response.statusCode != 200) {
81 onError('TURN request failed');
82 return;
83 }
84
85 var response = JSON.parse(body);
86 var iceServer = {
87 'username': response.username,
88 'credential': response.password,
89 'urls': response.uris
90 };
91 onSuccess({ 'iceServers': [ iceServer ] });
92 }
93 );
94 },
houssainy@google.comd0bb5862014-09-30 15:20:15 +000095}
96
97StatisticsReport = function (outputFileName) {
98 this.output_ = [];
99 this.output_.push("Version: 1");
100 this.outputFileName_ = outputFileName;
101}
102
103StatisticsReport.prototype = {
104 collectStatsFromPeerConnection: function (prefix, pc) {
105 setInterval(this.addPeerConnectionStats.bind(this, prefix, pc), 100);
106 },
107
108 addPeerConnectionStats: function (prefix, pc) {
109 pc.getStats(onStatsReady.bind(this));
110
111 function onStatsReady(reports) {
112 for (index in reports) {
113 var stats = {};
114 stats[reports[index].id] = collectStats(reports[index].stats);
115
116 var data = {};
117 data[prefix] = stats;
118
119 this.output_.push({
120 type: "UpdateCounters",
121 startTime: (new Date()).getTime(),
122 data: data,
123 });
124 }
125 };
126
127 function collectStats(stats) {
128 var outputStats = {};
129 for (index in stats) {
130 var statValue = parseFloat(stats[index].stat);
131 outputStats[stats[index].name] = isNaN(statValue)?
132 stats[index].stat : statValue;
133 }
134 return outputStats;
135 };
136 },
137
138 finish: function (doneCallback) {
houssainy@google.com5d3e7ac2014-10-06 17:21:27 +0000139 fs.exists("test/reports/", function (exists) {
140 if(exists) {
141 writeFile.bind(this)();
142 } else {
143 fs.mkdir("test/reports/", 0777, writeFile.bind(this));
144 }
145 }.bind(this));
146
147 function writeFile () {
148 fs.writeFile("test/reports/" + this.outputFileName_ + "_" +
houssainy@google.comd0bb5862014-09-30 15:20:15 +0000149 (new Date()).getTime() +".json", JSON.stringify(this.output_),
150 doneCallback);
houssainy@google.com5d3e7ac2014-10-06 17:21:27 +0000151 }
houssainy@google.comd0bb5862014-09-30 15:20:15 +0000152 },
andresp@webrtc.org458c2c32014-10-16 07:36:37 +0000153};
andresp@webrtc.org468516c2014-09-02 10:52:54 +0000154
andresp@webrtc.org458c2c32014-10-16 07:36:37 +0000155module.exports = Test;