Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 5e3a0ef

Browse files
authored
debuglet: stop can only be called on running agents (#209)
1 parent a680d87 commit 5e3a0ef

File tree

2 files changed

+59
-51
lines changed

2 files changed

+59
-51
lines changed

src/agent/debuglet.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ module.exports = Debuglet;
4848
/**
4949
* @param {Debug} debug - A Debug instance.
5050
* @param {object=} config - The option parameters for the Debuglet.
51-
* @event 'error' on startup errors
52-
* @event 'started' once the startup tasks are completed
53-
* @event 'registered' once successfully registered to the debug api
54-
* @event 'stopped' if the agent stops due to a fatal error after starting
55-
* @event 'remotelyDisabled' if the debuggee is disabled by the server.
51+
* @event 'started' once the startup tasks are completed. Only called once.
52+
* @event 'stopped' if the agent stops due to a fatal error after starting. Only
53+
* called once.
54+
* @event 'registered' once successfully registered to the debug api. May be
55+
* emitted multiple times.
56+
* @event 'remotelyDisabled' if the debuggee is disabled by the server. May be
57+
* called multiple times.
5658
* @constructor
5759
*/
5860
function Debuglet(debug, config) {
@@ -596,9 +598,13 @@ Debuglet.prototype.scheduleBreakpointExpiry_ = function(breakpoint) {
596598
};
597599

598600
/**
599-
* Stops the Debuglet
601+
* Stops the Debuglet. This is for testing purposes only. Stop should only be
602+
* called on a agent that has started (i.e. emitted the 'started' event).
603+
* Calling this while the agent is initializing may not necessarily stop all
604+
* pending operations.
600605
*/
601606
Debuglet.prototype.stop = function() {
607+
assert.ok(this.running_, 'stop can only be called on a running agent');
602608
this.logger_.debug('Stopping Debuglet');
603609
this.running_ = false;
604610
this.emit('stopped');

test/standalone/test-debuglet.js

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,13 @@ var errorBp = {
4343
};
4444

4545
describe(__filename, function() {
46-
var debuglet;
47-
4846
beforeEach(function() {
4947
delete process.env.GCLOUD_PROJECT;
5048
});
5149

52-
afterEach(function() {
53-
assert.ok(debuglet);
54-
debuglet.stop();
55-
});
56-
5750
it('should not start when projectId is not available', function(done) {
5851
var debug = require('../..')();
59-
debuglet = new Debuglet(debug, defaultConfig);
52+
var debuglet = new Debuglet(debug, defaultConfig);
6053

6154
// The following mock is neccessary for the case when the test is running
6255
// on GCP. In that case we will get the projectId from the metadata service.
@@ -66,6 +59,7 @@ describe(__filename, function() {
6659

6760
debuglet.once('initError', function(err) {
6861
assert.ok(err);
62+
// no need to stop the debuggee.
6963
scope.done();
7064
done();
7165
});
@@ -77,7 +71,7 @@ describe(__filename, function() {
7771

7872
it('should not crash without project num', function(done) {
7973
var debug = require('../..')();
80-
debuglet = new Debuglet(debug, defaultConfig);
74+
var debuglet = new Debuglet(debug, defaultConfig);
8175

8276
// The following mock is neccessary for the case when the test is running
8377
// on GCP. In that case we will get the projectId from the metadata service.
@@ -88,17 +82,17 @@ describe(__filename, function() {
8882
debuglet.once('started', function() {
8983
assert.fail();
9084
});
91-
setTimeout(function() {
85+
debuglet.once('initError', function() {
9286
scope.done();
9387
done();
94-
}, 1500);
88+
});
9589
debuglet.start();
9690
});
9791

9892
it('should accept non-numeric GCLOUD_PROJECT', function(done) {
9993
var debug = require('../..')(
10094
{projectId: '11020304f2934', credentials: fakeCredentials});
101-
debuglet = new Debuglet(debug, defaultConfig);
95+
var debuglet = new Debuglet(debug, defaultConfig);
10296

10397
nocks.oauth2();
10498
var scope = nock(API)
@@ -111,6 +105,7 @@ describe(__filename, function() {
111105

112106
debuglet.once('registered', function(id) {
113107
assert.equal(id, DEBUGGEE_ID);
108+
debuglet.stop();
114109
scope.done();
115110
done();
116111
});
@@ -122,7 +117,7 @@ describe(__filename, function() {
122117
this.timeout(10000);
123118
process.env.GCLOUD_PROJECT='11020304f2934';
124119
var debug = require('../..')({credentials: fakeCredentials});
125-
debuglet = new Debuglet(debug, defaultConfig);
120+
var debuglet = new Debuglet(debug, defaultConfig);
126121

127122
nocks.oauth2();
128123
var scope = nock(API)
@@ -139,6 +134,7 @@ describe(__filename, function() {
139134

140135
debuglet.once('registered', function(id) {
141136
assert.equal(id, DEBUGGEE_ID);
137+
debuglet.stop();
142138
scope.done();
143139
done();
144140
});
@@ -150,7 +146,7 @@ describe(__filename, function() {
150146
var debug = require('../..')(
151147
{projectId: 'fake-project', credentials: fakeCredentials});
152148
var config = extend({}, defaultConfig, {workingDirectory: __dirname});
153-
debuglet = new Debuglet(debug, config);
149+
var debuglet = new Debuglet(debug, config);
154150

155151
nocks.oauth2();
156152
debuglet.once('initError', function(err) {
@@ -164,14 +160,15 @@ describe(__filename, function() {
164160
it('should register successfully otherwise', function(done) {
165161
var debug = require('../..')(
166162
{projectId: 'fake-project', credentials: fakeCredentials});
167-
debuglet = new Debuglet(debug, defaultConfig);
163+
var debuglet = new Debuglet(debug, defaultConfig);
168164

169165
nocks.oauth2();
170166
var scope =
171167
nock(API).post(REGISTER_PATH).reply(200, {debuggee: {id: DEBUGGEE_ID}});
172168

173169
debuglet.once('registered', function(id) {
174170
assert.equal(id, DEBUGGEE_ID);
171+
debuglet.stop();
175172
scope.done();
176173
done();
177174
});
@@ -185,7 +182,7 @@ describe(__filename, function() {
185182

186183
var debug = require('../..')(
187184
{projectId: 'fake-project', credentials: fakeCredentials});
188-
debuglet = new Debuglet(debug, defaultConfig);
185+
var debuglet = new Debuglet(debug, defaultConfig);
189186

190187
nocks.oauth2();
191188
var scope = nock(API)
@@ -201,6 +198,7 @@ describe(__filename, function() {
201198

202199
debuglet.once('registered', function(id) {
203200
assert.equal(id, DEBUGGEE_ID);
201+
debuglet.stop();
204202
scope.done();
205203
process.chdir('../..');
206204
done();
@@ -209,36 +207,34 @@ describe(__filename, function() {
209207
debuglet.start();
210208
});
211209

212-
it('should de-activate when the server responds with isDisabled', function(done) {
213-
this.timeout(4000);
214-
var debug = require('../..')(
215-
{projectId: 'fake-project', credentials: fakeCredentials});
216-
debuglet = new Debuglet(debug, defaultConfig);
217-
218-
nocks.oauth2();
219-
var scope = nock(API)
220-
.post(REGISTER_PATH)
221-
.reply(200, {
222-
debuggee: {
223-
id: DEBUGGEE_ID,
224-
isDisabled: true
225-
}
226-
});
227-
228-
debuglet.once('remotelyDisabled', function() {
229-
assert.ok(!debuglet.fetcherActive_);
230-
scope.done();
231-
done();
232-
});
210+
it('should de-activate when the server responds with isDisabled',
211+
function(done) {
212+
this.timeout(4000);
213+
var debug = require('../..')(
214+
{projectId: 'fake-project', credentials: fakeCredentials});
215+
var debuglet = new Debuglet(debug, defaultConfig);
216+
217+
nocks.oauth2();
218+
var scope =
219+
nock(API)
220+
.post(REGISTER_PATH)
221+
.reply(200, {debuggee: {id: DEBUGGEE_ID, isDisabled: true}});
222+
223+
debuglet.once('remotelyDisabled', function() {
224+
assert.ok(!debuglet.fetcherActive_);
225+
debuglet.stop();
226+
scope.done();
227+
done();
228+
});
233229

234-
debuglet.start();
235-
});
230+
debuglet.start();
231+
});
236232

237233
it('should retry after a isDisabled request', function(done) {
238234
this.timeout(4000);
239235
var debug = require('../..')(
240236
{projectId: 'fake-project', credentials: fakeCredentials});
241-
debuglet = new Debuglet(debug, defaultConfig);
237+
var debuglet = new Debuglet(debug, defaultConfig);
242238

243239
nocks.oauth2();
244240
var scope = nock(API)
@@ -265,6 +261,7 @@ describe(__filename, function() {
265261
debuglet.once('registered', function(id) {
266262
assert.ok(gotDisabled);
267263
assert.equal(id, DEBUGGEE_ID);
264+
debuglet.stop();
268265
scope.done();
269266
done();
270267
});
@@ -275,7 +272,7 @@ describe(__filename, function() {
275272
it('should re-register when registration expires', function(done) {
276273
var debug = require('../..')(
277274
{projectId: 'fake-project', credentials: fakeCredentials});
278-
debuglet = new Debuglet(debug, defaultConfig);
275+
var debuglet = new Debuglet(debug, defaultConfig);
279276

280277
nocks.oauth2();
281278
var scope = nock(API)
@@ -298,6 +295,7 @@ describe(__filename, function() {
298295
assert.equal(id, DEBUGGEE_ID);
299296
debuglet.once('registered', function(id) {
300297
assert.equal(id, DEBUGGEE_ID);
298+
debuglet.stop();
301299
scope.done();
302300
done();
303301
});
@@ -310,7 +308,7 @@ describe(__filename, function() {
310308
this.timeout(2000);
311309
var debug = require('../..')(
312310
{projectId: 'fake-project', credentials: fakeCredentials});
313-
debuglet = new Debuglet(debug, defaultConfig);
311+
var debuglet = new Debuglet(debug, defaultConfig);
314312

315313
nocks.oauth2();
316314
var scope = nock(API)
@@ -329,6 +327,7 @@ describe(__filename, function() {
329327
assert.equal(id, DEBUGGEE_ID);
330328
setTimeout(function() {
331329
assert.deepEqual(debuglet.activeBreakpointMap_.test, bp);
330+
debuglet.stop();
332331
scope.done();
333332
done();
334333
}, 1000);
@@ -342,7 +341,7 @@ describe(__filename, function() {
342341

343342
var debug = require('../..')(
344343
{projectId: 'fake-project', credentials: fakeCredentials});
345-
debuglet = new Debuglet(debug, defaultConfig);
344+
var debuglet = new Debuglet(debug, defaultConfig);
346345

347346
nocks.oauth2();
348347
var scope = nock(API)
@@ -380,6 +379,7 @@ describe(__filename, function() {
380379
setTimeout(function() {
381380
assert.deepEqual(debuglet.activeBreakpointMap_.test, bp);
382381
assert(!debuglet.activeBreakpointMap_.testLog);
382+
debuglet.stop();
383383
scope.done();
384384
done();
385385
}, 1000);
@@ -412,13 +412,14 @@ describe(__filename, function() {
412412
})
413413
.reply(200);
414414

415-
debuglet = new Debuglet(debug, config);
415+
var debuglet = new Debuglet(debug, config);
416416
debuglet.once('registered', function(id) {
417417
assert.equal(id, DEBUGGEE_ID);
418418
setTimeout(function() {
419419
assert.deepEqual(debuglet.activeBreakpointMap_.test, bp);
420420
setTimeout(function() {
421421
assert(!debuglet.activeBreakpointMap_.test);
422+
debuglet.stop();
422423
scope.done();
423424
done();
424425
}, 1100);
@@ -466,7 +467,7 @@ describe(__filename, function() {
466467
breakpoints: [bp]
467468
});
468469

469-
debuglet = new Debuglet(debug, config);
470+
var debuglet = new Debuglet(debug, config);
470471
debuglet.once('registered', function(id) {
471472
assert.equal(id, DEBUGGEE_ID);
472473
setTimeout(function() {
@@ -475,6 +476,7 @@ describe(__filename, function() {
475476
assert(!debuglet.activeBreakpointMap_.test);
476477
// Fetcher disables if we re-update since endpoint isn't mocked twice
477478
assert(debuglet.fetcherActive_);
479+
debuglet.stop();
478480
scope.done();
479481
done();
480482
}, 4500);

0 commit comments

Comments
 (0)