From a3a21c50e4e11de773bcc14943516cc4f4884f4f Mon Sep 17 00:00:00 2001 From: brianc Date: Wed, 6 Oct 2010 19:54:02 -0500 Subject: [PATCH] using passed in stream to client --- lib/index.js | 7 ++++++- test/communication-tests.js | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 643e9f49..ddb09843 100644 --- a/lib/index.js +++ b/lib/index.js @@ -17,13 +17,18 @@ var Client = function(config) { this.user = config.user; this.database = config.database; this.port = config.port || 5432; + this.host = config.host; this.queryQueue = []; + this.stream = config.stream || new net.Stream(); }; sys.inherits(Client, EventEmitter); Client.prototype.connect = function() { - var con = net.createConnection(this.port); + var con = this.stream; + if(con.readyState == 'closed'){ + con.connect(this.port, this.host); + } var self = this; con.on('connect', function() { var data = ['user',self.user,'database', self.database,NUL].join(NUL); diff --git a/test/communication-tests.js b/test/communication-tests.js index ccb905ba..f9560c04 100644 --- a/test/communication-tests.js +++ b/test/communication-tests.js @@ -15,3 +15,42 @@ test('client can take existing stream', function() { }); assert.equal(client.stream, stream); }); + +test('using closed stream', function() { + var stream = new MemoryStream(); + stream.readyState = 'closed'; + stream.connect = function(port, host) { + this.connectCalled = true; + this.port = port; + this.host = host; + } + var client = new Client({ + stream: stream, + host: 'bang', + port: 1234 + }); + client.connect(); + test('makes stream connect', function() { + assert.equal(stream.connectCalled, true); + }); + test('uses configured port', function() { + assert.equal(stream.port, 1234); + }); + test('uses configured host', function() { + assert.equal(stream.host, 'bang'); + }); +}); + +test('using opened stream', function() { + var stream = new MemoryStream(); + stream.readyState = 'open'; + stream.connect = function() { + assert.ok(false, "Should not call open"); + }; + var client = new Client({stream: stream}); + test('does not call open', function() { + client.connect(); + }); +}); + +