mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
using passed in stream to client
This commit is contained in:
parent
4566a3563b
commit
a3a21c50e4
@ -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);
|
||||
|
||||
@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user