2010-10-30 08:48:31 +08:00
|
|
|
var helper = require(__dirname + '/test-helper');
|
2011-08-29 15:35:08 +08:00
|
|
|
var Connection = require(__dirname + '/../../../lib/connection');
|
2010-10-30 08:48:31 +08:00
|
|
|
|
2011-08-30 12:43:36 +08:00
|
|
|
test('drain', function() {
|
|
|
|
var con = new Connection({stream: "NO"});
|
|
|
|
var client = new Client({connection:con});
|
|
|
|
con.connect = function() {
|
|
|
|
con.emit('connect');
|
|
|
|
};
|
|
|
|
con.query = function() {
|
|
|
|
};
|
|
|
|
client.connect();
|
2010-10-30 08:48:31 +08:00
|
|
|
|
2011-08-30 12:43:36 +08:00
|
|
|
var raisedDrain = false;
|
|
|
|
client.on('drain', function() {
|
|
|
|
raisedDrain = true;
|
|
|
|
});
|
2010-10-30 08:48:31 +08:00
|
|
|
|
2011-08-30 12:43:36 +08:00
|
|
|
client.query("hello");
|
|
|
|
client.query("sup");
|
|
|
|
client.query('boom');
|
2010-10-30 08:48:31 +08:00
|
|
|
|
2011-08-30 12:43:36 +08:00
|
|
|
test("with pending queries", function() {
|
|
|
|
test("does not emit drain", function() {
|
|
|
|
assert.equal(raisedDrain, false);
|
|
|
|
});
|
|
|
|
});
|
2010-10-30 08:48:31 +08:00
|
|
|
|
2011-08-30 12:43:36 +08:00
|
|
|
test("after some queries executed", function() {
|
|
|
|
con.emit('readyForQuery');
|
|
|
|
test("does not emit drain", function() {
|
|
|
|
assert.equal(raisedDrain, false);
|
|
|
|
});
|
2010-10-30 08:48:31 +08:00
|
|
|
});
|
|
|
|
|
2011-08-30 12:43:36 +08:00
|
|
|
test("when all queries are sent", function() {
|
|
|
|
con.emit('readyForQuery');
|
|
|
|
con.emit('readyForQuery');
|
|
|
|
test("does not emit drain", function() {
|
|
|
|
assert.equal(raisedDrain, false);
|
|
|
|
});
|
2010-10-30 08:48:31 +08:00
|
|
|
});
|
|
|
|
|
2011-08-30 12:43:36 +08:00
|
|
|
test("after last query finishes", function() {
|
|
|
|
con.emit('readyForQuery');
|
|
|
|
test("emits drain", function() {
|
|
|
|
process.nextTick(function() {
|
|
|
|
assert.ok(raisedDrain);
|
|
|
|
})
|
|
|
|
});
|
2010-10-30 08:48:31 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-08-30 12:43:36 +08:00
|
|
|
test('with drain paused', function() {
|
|
|
|
//mock out a fake connection
|
|
|
|
var con = new Connection({stream: "NO"});
|
|
|
|
con.connect = function() {
|
|
|
|
con.emit('connect');
|
|
|
|
};
|
|
|
|
con.query = function() {
|
|
|
|
};
|
|
|
|
|
|
|
|
var client = new Client({connection:con});
|
|
|
|
|
|
|
|
client.connect();
|
|
|
|
|
|
|
|
var drainCount = 0;
|
|
|
|
client.on('drain', function() {
|
|
|
|
drainCount++;
|
2010-10-30 08:48:31 +08:00
|
|
|
});
|
|
|
|
|
2011-08-30 12:43:36 +08:00
|
|
|
test('normally unpaused', function() {
|
|
|
|
con.emit('readyForQuery');
|
|
|
|
client.query('boom');
|
|
|
|
assert.emits(client, 'drain', function() {
|
|
|
|
assert.equal(drainCount, 1);
|
|
|
|
});
|
|
|
|
con.emit('readyForQuery');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('pausing', function() {
|
|
|
|
test('unpaused with no queries in between', function() {
|
|
|
|
client.pauseDrain();
|
|
|
|
client.resumeDrain();
|
|
|
|
assert.equal(drainCount, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('paused', function() {
|
|
|
|
test('resumeDrain after empty', function() {
|
|
|
|
client.pauseDrain();
|
|
|
|
client.query('asdf');
|
|
|
|
con.emit('readyForQuery');
|
|
|
|
assert.equal(drainCount, 1);
|
|
|
|
client.resumeDrain();
|
|
|
|
assert.equal(drainCount, 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('resumDrain while still pending', function() {
|
|
|
|
client.pauseDrain();
|
|
|
|
client.query('asdf');
|
|
|
|
client.query('asdf1');
|
|
|
|
con.emit('readyForQuery');
|
|
|
|
client.resumeDrain();
|
|
|
|
assert.equal(drainCount, 2);
|
|
|
|
con.emit('readyForQuery');
|
|
|
|
assert.equal(drainCount, 3);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|