andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 1 | // 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.org | 458c2c3 | 2014-10-16 07:36:37 +0000 | [diff] [blame] | 9 | // Provides a Test class that exposes api to the tests. |
| 10 | // Read test.prototype to see what methods are exposed. |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 11 | var fs = require('fs'); |
houssainy@google.com | 0371a37 | 2014-10-17 16:43:50 +0000 | [diff] [blame] | 12 | var request = require('request'); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 13 | var BotManager = require('./botmanager.js'); |
| 14 | |
houssainy@google.com | 3e2f8ff | 2014-10-15 15:01:11 +0000 | [diff] [blame] | 15 | function Test() { |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 16 | this.timeout_ = setTimeout( |
| 17 | this.fail.bind(this, "Test timeout!"), |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 18 | 100000); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | Test.prototype = { |
| 22 | log: function () { |
| 23 | console.log.apply(console.log, arguments); |
| 24 | }, |
| 25 | |
| 26 | abort: function (error) { |
houssainy@google.com | e0761d0 | 2014-09-10 14:35:35 +0000 | [diff] [blame] | 27 | var error = new Error(error || "Test aborted"); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 28 | 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.com | 3e2f8ff | 2014-10-15 15:01:11 +0000 | [diff] [blame] | 65 | spawnBot: function (name, botType, doneCallback) { |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 66 | // Lazy initialization of botmanager. |
| 67 | if (!this.botManager_) |
| 68 | this.botManager_ = new BotManager(); |
houssainy@google.com | 3e2f8ff | 2014-10-15 15:01:11 +0000 | [diff] [blame] | 69 | this.botManager_.spawnNewBot(name, botType, doneCallback); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 70 | }, |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 71 | |
| 72 | createStatisticsReport: function (outputFileName) { |
| 73 | return new StatisticsReport(outputFileName); |
| 74 | }, |
houssainy@google.com | 0371a37 | 2014-10-17 16:43:50 +0000 | [diff] [blame] | 75 | |
| 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.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | StatisticsReport = function (outputFileName) { |
| 98 | this.output_ = []; |
| 99 | this.output_.push("Version: 1"); |
| 100 | this.outputFileName_ = outputFileName; |
| 101 | } |
| 102 | |
| 103 | StatisticsReport.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.com | 5d3e7ac | 2014-10-06 17:21:27 +0000 | [diff] [blame] | 139 | 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.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 149 | (new Date()).getTime() +".json", JSON.stringify(this.output_), |
| 150 | doneCallback); |
houssainy@google.com | 5d3e7ac | 2014-10-06 17:21:27 +0000 | [diff] [blame] | 151 | } |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 152 | }, |
andresp@webrtc.org | 458c2c3 | 2014-10-16 07:36:37 +0000 | [diff] [blame] | 153 | }; |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 154 | |
andresp@webrtc.org | 458c2c3 | 2014-10-16 07:36:37 +0000 | [diff] [blame] | 155 | module.exports = Test; |