Skip to content

Commit

Permalink
close ditz issue 13: wait until remote host is up to start the debugg…
Browse files Browse the repository at this point in the history
…er instead of failing immediately.
  • Loading branch information
smtlaissezfaire committed May 16, 2010
1 parent 46c47a5 commit e2793e8
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 39 deletions.
8 changes: 6 additions & 2 deletions bugs/issue-a137797b955bbb73a14bb820581396d077a15c36.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ type: :feature
component: ndb
release:
reporter: Scott Taylor <scott@railsnewbie.com>
status: :unstarted
disposition:
status: :closed
disposition: :fixed
creation_time: 2010-05-16 21:13:19.798821 Z
references: []

Expand All @@ -16,3 +16,7 @@ log_events:
- Scott Taylor <scott@railsnewbie.com>
- created
- ""
- - 2010-05-16 22:23:35.914310 Z
- Scott Taylor <scott@railsnewbie.com>
- closed with disposition fixed
- ""
43 changes: 27 additions & 16 deletions lib/ndb.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ ndb.reset = function() {
};

ndb.Helpers = {
puts: sys.puts,
print: sys.print,
tcp: tcp,
process: process,
exit: process.exit,
puts: sys.puts,
print: sys.print,
tcp: tcp,
process: process,
exit: process.exit,
setTimeout: setTimeout,

log: function(str, repetition) {
var lines = str.split("\n"),
Expand Down Expand Up @@ -53,33 +54,43 @@ ndb.Helpers = {
}
};

var createConnection = function() {
var ndb = exports,
tcp = exports.Helpers.tcp;
ndb.start = function() {
var tcp = ndb.Helpers.tcp,
eventListener = ndb.eventListener || Object.create(ndb.EventListener);

connection = tcp.createConnection(ndb.port, ndb.host);
connection.setEncoding("ascii");

connection.addListener("error", ndb.restart);

connection.addListener("data", function() {
eventListener.receive.apply(eventListener, arguments);
});

connection.addListener("end", ndb.Helpers.exit);

connection.addListener("connect", function() {
ndb.startDebugger(connection);
});

eventListener.verbose = this.verbose;

return connection;
};

ndb.start = function() {
ndb.restart = function() {
var puts = ndb.Helpers.puts,
setTimeout = ndb.Helpers.setTimeout;

puts("Could not connect to host " + ndb.host + " on port " + ndb.port + ". Will try again in 2 seconds.");
setTimeout(ndb.start, 2000);
};

ndb.startDebugger = function(connection) {
var puts = ndb.Helpers.puts,
prompt = ndb.Helpers.prompt,
commandCenter = this.commandCenter || Object.create(ndb.CommandCenter),
eventListener = this.eventListener || Object.create(ndb.EventListener),
connection;

connection = createConnection();
commandCenter = this.commandCenter || Object.create(ndb.CommandCenter);

// set some defaults
eventListener.verbose = this.verbose;
commandCenter.connection = connection;

// start the main repl loop
Expand Down
100 changes: 79 additions & 21 deletions spec/unit/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ describe('NodeDebugger', function() {
ndb.commandCenter = command_center;
});

describe("starting", function() {
describe("starting the debugger itself", function() {
before_each(function() {
connection = {};
});

it("should output welcome text", function() {
var text = "";

ndb.Helpers.puts = function(t) {
text += t;
};

ndb.start();
ndb.startDebugger(connection);

text.search(/welcome to the node debugger!/).should.not.equal(-1);
});
Expand All @@ -28,11 +32,31 @@ describe('NodeDebugger', function() {
text += t;
};

ndb.start();
ndb.startDebugger(connection);

text.search(/ndb> /).should.not.equal(-1);
});

it("should set the command center's connection", function() {
ndb.startDebugger(connection);

command_center.connection.should.equal(connection);
});

it("should call the commandCenter's loop method", function() {
var loop_called = false;

command_center.loop = function() {
loop_called = true;
};

ndb.startDebugger(connection);

loop_called.should.be(true);
});
});

describe("starting", function() {
it("should establish the connection on port 5858 and 127.0.0.1", function() {
var port_received = null,
host_received = null;
Expand Down Expand Up @@ -73,24 +97,6 @@ describe('NodeDebugger', function() {
encoding_received.should.equal("ascii");
});

it("should set the command center's connection", function() {
ndb.start();

command_center.connection.should.equal(connection);
});

it("should call the commandCenter's loop method", function() {
var loop_called = false;

command_center.loop = function() {
loop_called = true;
};

ndb.start();

loop_called.should.be(true);
});

it("should install the connection's event listener", function() {
var called = false;

Expand All @@ -117,6 +123,58 @@ describe('NodeDebugger', function() {
args[0].should.equal("end");
args[1].should.equal(ndb.Helpers.exit);
});

it("should listen for a connection error message", function() {
var args = [];

connection.addListener = function(msg, fun) {
if (msg === "error") {
args.push(msg);
args.push(fun);
}
};

ndb.start();

args[0].should.equal("error");
args[1].should.equal(ndb.restart);
});

describe("when there is an error", function() {
before_each(function() {
spy.stub(ndb.Helpers, "setTimeout");
});

it("should output that it is about to restart", function() {
spy.spyOn(ndb.Helpers, function() {
ndb.restart();

spy.intercepted(ndb.Helpers, "puts", function(text) {
text.should.equal("Could not connect to host 127.0.0.1 on port 5858. Will try again in 2 seconds.");
});
});
});

it("should use the correct host + port in the error message", function() {
ndb.host = "localhost";
ndb.port = "2020";

spy.spyOn(ndb.Helpers, function() {
ndb.restart();

spy.intercepted(ndb.Helpers, "puts", function(text) {
text.should.equal("Could not connect to host localhost on port 2020. Will try again in 2 seconds.");
});
});
});

it("should call start again in 2 seconds", function() {
spy.spyOn(ndb.Helpers, function() {
ndb.restart();
spy.intercepted(ndb.Helpers, "setTimeout", ndb.start, 2000);
});
});
});
});
});
});

0 comments on commit e2793e8

Please sign in to comment.