Permalink
Browse files

close ditz issue 13: wait until remote host is up to start the debugg…

…er instead of failing immediately.
  • Loading branch information...
1 parent 46c47a5 commit e2793e88248efb9ffd5afb05022df149dcabcd10 @smtlaissezfaire committed May 16, 2010
Showing with 112 additions and 39 deletions.
  1. +6 −2 bugs/issue-a137797b955bbb73a14bb820581396d077a15c36.yaml
  2. +27 −16 lib/ndb.js
  3. +79 −21 spec/unit/spec.js
@@ -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: []
@@ -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
+ - ""
View
@@ -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"),
@@ -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
View
@@ -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);
});
@@ -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;
@@ -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;
@@ -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.