1 // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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.
9 // Provides a Test class that exposes api to the tests.
10 // Read test.prototype to see what methods are exposed.
11 var fs = require('fs');
12 var request = require('request');
13 var BotManager = require('./botmanager.js');
16 this.timeout_ = setTimeout(
17 this.fail.bind(this, "Test timeout!"),
23 console.log.apply(console.log, arguments);
26 abort: function (error) {
27 var error = new Error(error || "Test aborted");
28 console.log(error.stack);
32 assert: function (value, message) {
34 this.abort(message || "Assert failed.");
39 this.assert(false, "Test failed.");
43 clearTimeout(this.timeout_);
44 console.log("Test succeeded");
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));
57 function complete(index, value) {
59 result[index] = value;
60 if (missingResult == 0)
61 doneCallback.apply(null, result);
65 spawnBot: function (name, botType, doneCallback) {
66 // Lazy initialization of botmanager.
67 if (!this.botManager_)
68 this.botManager_ = new BotManager();
69 this.botManager_.spawnNewBot(name, botType, doneCallback);
72 createStatisticsReport: function (outputFileName) {
73 return new StatisticsReport(outputFileName);
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');
85 var response = JSON.parse(body);
87 'username': response.username,
88 'credential': response.password,
91 onSuccess({ 'iceServers': [ iceServer ] });
97 StatisticsReport = function (outputFileName) {
99 this.output_.push("Version: 1");
100 this.outputFileName_ = outputFileName;
103 StatisticsReport.prototype = {
104 collectStatsFromPeerConnection: function (prefix, pc) {
105 setInterval(this.addPeerConnectionStats.bind(this, prefix, pc), 100);
108 addPeerConnectionStats: function (prefix, pc) {
109 pc.getStats(onStatsReady.bind(this));
111 function onStatsReady(reports) {
112 for (index in reports) {
114 stats[reports[index].id] = collectStats(reports[index].stats);
117 data[prefix] = stats;
120 type: "UpdateCounters",
121 startTime: (new Date()).getTime(),
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;
138 finish: function (doneCallback) {
139 fs.exists("test/reports/", function (exists) {
141 writeFile.bind(this)();
143 fs.mkdir("test/reports/", 0777, writeFile.bind(this));
147 function writeFile () {
148 fs.writeFile("test/reports/" + this.outputFileName_ + "_" +
149 (new Date()).getTime() +".json", JSON.stringify(this.output_),
155 module.exports = Test;