using passed in stream to client

This commit is contained in:
brianc 2010-10-06 19:54:02 -05:00
parent 4566a3563b
commit a3a21c50e4
2 changed files with 45 additions and 1 deletions

View File

@ -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);

View File

@ -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();
});
});