0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 23:43:09 +01:00
nodejs/test/simple/test-debugger-client.js

201 lines
5.8 KiB
JavaScript
Raw Normal View History

2011-03-10 09:54:52 +01:00
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2011-11-03 00:26:43 +01:00
2011-08-24 08:42:23 +02:00
var common = require('../common');
var assert = require('assert');
2010-12-22 07:31:30 +01:00
var debug = require('_debugger');
var spawn = require('child_process').spawn;
var resCount = 0;
2010-12-22 07:31:30 +01:00
var p = new debug.Protocol();
p.onResponse = function(res) {
resCount++;
};
p.execute('Type: connect\r\n' +
'V8-Version: 3.0.4.1\r\n' +
'Protocol-Version: 1\r\n' +
'Embedding-Host: node v0.3.3-pre\r\n' +
'Content-Length: 0\r\n\r\n');
assert.equal(1, resCount);
// Make sure split messages go in.
var parts = [];
parts.push('Content-Length: 336\r\n');
assert.equal(21, parts[0].length);
parts.push('\r\n');
assert.equal(2, parts[1].length);
var bodyLength = 0;
parts.push('{"seq":12,"type":"event","event":"break","body":' +
'{"invocationText":"#<a Server>');
assert.equal(78, parts[2].length);
bodyLength += parts[2].length;
2012-02-19 00:01:35 +01:00
parts.push('.[anonymous](req=#<an IncomingMessage>, ' +
'res=#<a ServerResponse>)","sourceLine"');
assert.equal(78, parts[3].length);
bodyLength += parts[3].length;
2012-02-19 00:01:35 +01:00
parts.push(':45,"sourceColumn":4,"sourceLineText":" debugger;",' +
'"script":{"id":24,"name":"/home/ryan/projects/node/' +
'benchmark/http_simple.js","lineOffset":0,"columnOffset":0,' +
'"lineCount":98}}}');
assert.equal(180, parts[4].length);
bodyLength += parts[4].length;
assert.equal(336, bodyLength);
for (var i = 0; i < parts.length; i++) {
p.execute(parts[i]);
}
assert.equal(2, resCount);
// Make sure that if we get backed up, we still manage to get all the
// messages
var d = 'Content-Length: 466\r\n\r\n' +
'{"seq":10,"type":"event","event":"afterCompile","success":true,' +
'"body":{"script":{"handle":1,"type":"script","name":"dns.js",' +
2012-02-19 00:01:35 +01:00
'"id":34,"lineOffset":0,"columnOffset":0,"lineCount":241,' +
'"sourceStart":"(function (module, exports, require) {' +
'var dns = process.binding(\'cares\')' +
';\\nvar ne","sourceLength":6137,"scriptType":2,"compilationType":0,' +
2012-02-19 00:01:35 +01:00
'"context":{"ref":0},"text":"dns.js (lines: 241)"}},"refs":' +
'[{"handle":0' +
',"type":"context","text":"#<a ContextMirror>"}],"running":true}' +
'Content-Length: 119\r\n\r\n' +
'{"seq":11,"type":"event","event":"scriptCollected","success":true,' +
'"body":{"script":{"id":26}},"refs":[],"running":true}';
p.execute(d);
assert.equal(4, resCount);
2010-12-22 23:31:57 +01:00
var expectedConnections = 0;
var tests = [];
function addTest(cb) {
2010-12-22 23:31:57 +01:00
expectedConnections++;
tests.push(cb);
}
addTest(function(client, done) {
console.error('requesting version');
client.reqVersion(function(err, v) {
assert.ok(!err);
console.log('version: %s', v);
2010-12-22 23:31:57 +01:00
assert.equal(process.versions.v8, v);
done();
});
});
addTest(function(client, done) {
console.error('requesting scripts');
client.reqScripts(function(err) {
assert.ok(!err);
console.error('got %d scripts', Object.keys(client.scripts).length);
2010-12-31 02:52:14 +01:00
2010-12-22 23:31:57 +01:00
var foundMainScript = false;
2010-12-31 02:52:14 +01:00
for (var k in client.scripts) {
var script = client.scripts[k];
if (script && script.name === 'node.js') {
2010-12-22 23:31:57 +01:00
foundMainScript = true;
break;
}
}
assert.ok(foundMainScript);
done();
});
});
addTest(function(client, done) {
console.error('eval 2+2');
client.reqEval('2+2', function(err, res) {
2010-12-22 23:31:57 +01:00
console.error(res);
assert.ok(!err);
assert.equal('4', res.text);
assert.equal(4, res.value);
2010-12-22 23:31:57 +01:00
done();
});
});
2010-12-22 07:31:30 +01:00
var connectCount = 0;
2010-12-22 23:31:57 +01:00
function doTest(cb, done) {
2010-12-22 07:31:30 +01:00
var nodeProcess = spawn(process.execPath,
['-e', 'setInterval(function () { console.log("blah"); }, 100);']);
nodeProcess.stdout.once('data', function() {
console.log('>>> new node process: %d', nodeProcess.pid);
2011-11-04 16:23:02 +01:00
process._debugProcess(nodeProcess.pid);
console.log('>>> starting debugger session');
2010-12-22 07:31:30 +01:00
});
2010-12-22 07:31:30 +01:00
var didTryConnect = false;
nodeProcess.stderr.setEncoding('utf8');
var b = '';
nodeProcess.stderr.on('data', function(data) {
2011-01-12 01:30:01 +01:00
b += data;
if (didTryConnect == false && /debugger listening on port/.test(b)) {
2010-12-22 07:31:30 +01:00
didTryConnect = true;
2011-01-12 01:30:01 +01:00
setTimeout(function() {
// Wait for some data before trying to connect
var c = new debug.Client();
process.stdout.write('>>> connecting...');
c.connect(debug.port);
c.on('ready', function() {
2011-01-12 01:30:01 +01:00
connectCount++;
console.log('ready!');
cb(c, function() {
console.error('>>> killing node process %d\n\n', nodeProcess.pid);
2011-01-12 01:30:01 +01:00
nodeProcess.kill();
done();
});
2010-12-22 23:31:57 +01:00
});
2011-01-12 01:30:01 +01:00
}, 100);
2010-12-22 07:31:30 +01:00
}
});
2010-12-22 07:31:30 +01:00
}
2010-12-22 07:31:30 +01:00
function run() {
2010-12-22 23:31:57 +01:00
var t = tests[0];
if (!t) return;
doTest(t, function() {
2010-12-22 23:31:57 +01:00
tests.shift();
run();
});
2010-12-22 23:31:57 +01:00
}
2010-12-22 23:31:57 +01:00
run();
process.on('exit', function() {
2010-12-22 23:31:57 +01:00
assert.equal(expectedConnections, connectCount);
});