|
| 1 | +/* KEEP THIS CODE AT THE TOP SO THAT THE BREAKPOINT LINE NUMBERS DON'T CHANGE */ |
| 2 | +'use strict'; |
| 3 | +function fib(n) { |
| 4 | + if (n < 2) { return n; } var o = { a: [1, 'hi', true] }; |
| 5 | + return fib(n - 1, o) + fib(n - 2, o); // adding o to appease linter. |
| 6 | +} |
| 7 | +/** |
| 8 | + * Copyright 2015 Google Inc. All Rights Reserved. |
| 9 | + * |
| 10 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | + * you may not use this file except in compliance with the License. |
| 12 | + * You may obtain a copy of the License at |
| 13 | + * |
| 14 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | + * |
| 16 | + * Unless required by applicable law or agreed to in writing, software |
| 17 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | + * See the License for the specific language governing permissions and |
| 20 | + * limitations under the License. |
| 21 | + */ |
| 22 | + |
| 23 | +process.env.GCLOUD_DEBUG_LOGLEVEL=2; |
| 24 | + |
| 25 | +var assert = require('assert'); |
| 26 | +var util = require('util'); |
| 27 | +var GoogleAuth = require('google-auth-library'); |
| 28 | +var _ = require('lodash'); // for _.find. Can't use ES6 yet. |
| 29 | +var Q = require('q'); |
| 30 | +var cluster = require('cluster'); |
| 31 | + |
| 32 | +var DEBUG_API = 'https://clouddebugger.googleapis.com/v2/debugger'; |
| 33 | +var SCOPES = [ |
| 34 | + 'https://www.googleapis.com/auth/cloud-platform', |
| 35 | + 'https://www.googleapis.com/auth/cloud_debugger', |
| 36 | + ]; |
| 37 | + |
| 38 | +var agent; |
| 39 | +var transcript = ''; |
| 40 | + |
| 41 | +function apiRequest(authClient, url, method, body) { |
| 42 | + method = method || 'GET'; |
| 43 | + |
| 44 | + var deferred = Q.defer(); |
| 45 | + authClient.request({ |
| 46 | + url: url, |
| 47 | + method: method, |
| 48 | + json: true, |
| 49 | + body: body |
| 50 | + }, function(err, body, response) { |
| 51 | + if (err) { |
| 52 | + deferred.reject(err); |
| 53 | + } else { |
| 54 | + deferred.resolve(body); |
| 55 | + } |
| 56 | + }); |
| 57 | + return deferred.promise; |
| 58 | +} |
| 59 | + |
| 60 | +function runTest() { |
| 61 | + Q.delay(10 * 1000).then(function() { |
| 62 | + var api = agent.private_.debugletApi_; |
| 63 | + |
| 64 | + var debuggee = api.debuggeeId_; |
| 65 | + var project = api.project_; |
| 66 | + |
| 67 | + // Get our own credentials because we need an extra scope |
| 68 | + var auth = new GoogleAuth(); |
| 69 | + auth.getApplicationDefault(function(err, authClient) { |
| 70 | + if (err) { |
| 71 | + console.log(err); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + // Inject scopes if they have not been injected by the environment |
| 76 | + if (authClient.createScopedRequired && |
| 77 | + authClient.createScopedRequired()) { |
| 78 | + authClient = authClient.createScoped(SCOPES); |
| 79 | + } |
| 80 | + |
| 81 | + function listDebuggees(project) { |
| 82 | + return apiRequest(authClient, DEBUG_API + '/debuggees?project=' + |
| 83 | + project); |
| 84 | + } |
| 85 | + |
| 86 | + function listBreakpoints(debuggee) { |
| 87 | + return apiRequest(authClient, DEBUG_API + '/debuggees/' + debuggee + |
| 88 | + '/breakpoints'); |
| 89 | + } |
| 90 | + |
| 91 | + function deleteBreakpoint(debuggee, breakpoint) { |
| 92 | + return apiRequest(authClient, DEBUG_API + '/debuggees/' + debuggee + |
| 93 | + '/breakpoints/' + breakpoint.id, 'DELETE'); |
| 94 | + } |
| 95 | + |
| 96 | + function setBreakpoint(debuggee, body) { |
| 97 | + return apiRequest(authClient, DEBUG_API + '/debuggees/' + debuggee + |
| 98 | + '/breakpoints/set', 'POST', body); |
| 99 | + } |
| 100 | + |
| 101 | + listDebuggees(project) |
| 102 | + .then(function(body) { |
| 103 | + console.log('-- List of debuggees\n', |
| 104 | + util.inspect(body, { depth: null})); |
| 105 | + assert.ok(body, 'should get a valid ListDebuggees response'); |
| 106 | + assert.ok(body.debuggees, 'should have a debuggees property'); |
| 107 | + var result = _.find(body.debuggees, function(d) { |
| 108 | + return d.id === debuggee; |
| 109 | + }); |
| 110 | + assert.ok(result, 'should find the debuggee we just registered'); |
| 111 | + return debuggee; |
| 112 | + }) |
| 113 | + .then(listBreakpoints) |
| 114 | + .then(function(body) { |
| 115 | + console.log('-- List of breakpoints\n', body); |
| 116 | + body.breakpoints = body.breakpoints || []; |
| 117 | + |
| 118 | + var promises = body.breakpoints.map(function(breakpoint) { |
| 119 | + return deleteBreakpoint(debuggee, breakpoint); |
| 120 | + }); |
| 121 | + |
| 122 | + var deleteAll = Q.all(promises); |
| 123 | + |
| 124 | + return deleteAll; |
| 125 | + }) |
| 126 | + .then(function(deleteResults) { |
| 127 | + console.log('-- delete results', deleteResults); |
| 128 | + return debuggee; |
| 129 | + }) |
| 130 | + .then(function(debuggee) { |
| 131 | + // Set a breakpoint |
| 132 | + console.log('-- setting a logpoint'); |
| 133 | + var promise = setBreakpoint(debuggee, { |
| 134 | + id: 'breakpoint-1', |
| 135 | + location: {path: 'test-log-throttling.js', line: 5}, |
| 136 | + condition: 'n === 10', |
| 137 | + action: 'LOG', |
| 138 | + expressions: ['o'], |
| 139 | + log_message_format: 'o is: $0' |
| 140 | + }); |
| 141 | + // I don't know what I am doing. There is a better way to write the |
| 142 | + // following using promises. |
| 143 | + var result = promise.then(function(body) { |
| 144 | + console.log('-- resolution of setBreakpoint', body); |
| 145 | + assert.ok(body.breakpoint, 'should have set a breakpoint'); |
| 146 | + var breakpoint = body.breakpoint; |
| 147 | + assert.ok(breakpoint.id, 'breakpoint should have an id'); |
| 148 | + assert.ok(breakpoint.location, 'breakpoint should have a location'); |
| 149 | + assert.strictEqual(breakpoint.location.path, 'test-log-throttling.js'); |
| 150 | + return { debuggee: debuggee, breakpoint: breakpoint }; |
| 151 | + }); |
| 152 | + return result; |
| 153 | + }) |
| 154 | + .then(function(result) { |
| 155 | + console.log('-- waiting before checking if the log was written'); |
| 156 | + return Q.delay(10 * 1000).then(function() { return result; }); |
| 157 | + }) |
| 158 | + .then(function(result) { |
| 159 | + // If no throttling occurs, we expect ~20 logs since we are logging |
| 160 | + // 2x per second over a 10 second period. |
| 161 | + var logCount = transcript.split('o is: {"a":[1,"hi",true]}').length - 1; |
| 162 | + // A log count of greater than 10 indicates that we did not successfully |
| 163 | + // pause when the rate of `maxLogsPerSecond` was reached. |
| 164 | + assert(logCount < 10); |
| 165 | + // A log count of less than 3 indicates that we did not successfully |
| 166 | + // resume logging after `logDelaySeconds` have passed. |
| 167 | + assert(logCount > 2); |
| 168 | + deleteBreakpoint(result.debuggee, result.breakpoint).then(); |
| 169 | + console.log('Test passed'); |
| 170 | + process.exit(0); |
| 171 | + }) |
| 172 | + .catch(function(e) { |
| 173 | + console.error(e); |
| 174 | + process.exit(1); |
| 175 | + }); |
| 176 | + }); |
| 177 | + }).catch(function(e) { |
| 178 | + console.error(e); |
| 179 | + process.exit(1); |
| 180 | + }); |
| 181 | +} |
| 182 | + |
| 183 | +if (cluster.isMaster) { |
| 184 | + cluster.setupMaster({ silent: true }); |
| 185 | + var handler = function(m) { |
| 186 | + assert.ok(m.private_, 'debuglet has initialized'); |
| 187 | + var debuglet = m.private_; |
| 188 | + assert.ok(debuglet.debugletApi_, 'debuglet api is active'); |
| 189 | + var api = debuglet.debugletApi_; |
| 190 | + assert.ok(api.uid_, 'debuglet provided unique id'); |
| 191 | + assert.ok(api.debuggeeId_, 'debuglet has registered'); |
| 192 | + agent = m; |
| 193 | + }; |
| 194 | + var stdoutHandler = function(chunk) { |
| 195 | + transcript += chunk; |
| 196 | + }; |
| 197 | + var worker = cluster.fork(); |
| 198 | + worker.on('message', handler); |
| 199 | + worker.process.stdout.on('data', stdoutHandler); |
| 200 | + runTest(); |
| 201 | +} else { |
| 202 | + var agent = require('../..'); |
| 203 | + agent.private_.config_.log.maxLogsPerSecond = 1; |
| 204 | + agent.private_.config_.log.logDelaySeconds = 5; |
| 205 | + setTimeout(process.send.bind(process, agent), 7000); |
| 206 | + setInterval(fib.bind(null, 12), 500); |
| 207 | +} |
0 commit comments