|
1 | 1 | var helper = require(__dirname + '/test-helper');
|
2 | 2 | var Connection = require(__dirname + '/../../../lib/connection');
|
3 |
| -var con = new Connection({stream: "NO"}); |
4 |
| -var client = new Client({connection:con}); |
5 | 3 |
|
| 4 | +test('drain', function() { |
| 5 | + var con = new Connection({stream: "NO"}); |
| 6 | + var client = new Client({connection:con}); |
| 7 | + con.connect = function() { |
| 8 | + con.emit('connect'); |
| 9 | + }; |
| 10 | + con.query = function() { |
| 11 | + }; |
| 12 | + client.connect(); |
6 | 13 |
|
7 |
| -con.connect = function() { |
8 |
| - con.emit('connect'); |
9 |
| -}; |
10 |
| -con.query = function() { |
11 |
| -}; |
12 |
| -client.connect(); |
| 14 | + var raisedDrain = false; |
| 15 | + client.on('drain', function() { |
| 16 | + raisedDrain = true; |
| 17 | + }); |
13 | 18 |
|
14 |
| -var raisedDrain = false; |
15 |
| -client.on('drain', function() { |
16 |
| - raisedDrain = true; |
17 |
| -}); |
| 19 | + client.query("hello"); |
| 20 | + client.query("sup"); |
| 21 | + client.query('boom'); |
18 | 22 |
|
19 |
| -client.query("hello"); |
20 |
| -client.query("sup"); |
21 |
| -client.query('boom'); |
| 23 | + test("with pending queries", function() { |
| 24 | + test("does not emit drain", function() { |
| 25 | + assert.equal(raisedDrain, false); |
| 26 | + }); |
| 27 | + }); |
22 | 28 |
|
23 |
| -test("with pending queries", function() { |
24 |
| - test("does not emit drain", function() { |
25 |
| - assert.equal(raisedDrain, false); |
| 29 | + test("after some queries executed", function() { |
| 30 | + con.emit('readyForQuery'); |
| 31 | + test("does not emit drain", function() { |
| 32 | + assert.equal(raisedDrain, false); |
| 33 | + }); |
26 | 34 | });
|
27 |
| -}); |
28 | 35 |
|
29 |
| -test("after some queries executed", function() { |
30 |
| - con.emit('readyForQuery'); |
31 |
| - test("does not emit drain", function() { |
32 |
| - assert.equal(raisedDrain, false); |
| 36 | + test("when all queries are sent", function() { |
| 37 | + con.emit('readyForQuery'); |
| 38 | + con.emit('readyForQuery'); |
| 39 | + test("does not emit drain", function() { |
| 40 | + assert.equal(raisedDrain, false); |
| 41 | + }); |
33 | 42 | });
|
34 |
| -}); |
35 | 43 |
|
36 |
| -test("when all queries are sent", function() { |
37 |
| - con.emit('readyForQuery'); |
38 |
| - con.emit('readyForQuery'); |
39 |
| - test("does not emit drain", function() { |
40 |
| - assert.equal(raisedDrain, false); |
| 44 | + test("after last query finishes", function() { |
| 45 | + con.emit('readyForQuery'); |
| 46 | + test("emits drain", function() { |
| 47 | + process.nextTick(function() { |
| 48 | + assert.ok(raisedDrain); |
| 49 | + }) |
| 50 | + }); |
41 | 51 | });
|
42 | 52 | });
|
43 | 53 |
|
44 |
| -test("after last query finishes", function() { |
45 |
| - con.emit('readyForQuery'); |
46 |
| - test("emits drain", function() { |
47 |
| - process.nextTick(function() { |
48 |
| - assert.ok(raisedDrain); |
49 |
| - }) |
| 54 | +test('with drain paused', function() { |
| 55 | + //mock out a fake connection |
| 56 | + var con = new Connection({stream: "NO"}); |
| 57 | + con.connect = function() { |
| 58 | + con.emit('connect'); |
| 59 | + }; |
| 60 | + con.query = function() { |
| 61 | + }; |
| 62 | + |
| 63 | + var client = new Client({connection:con}); |
| 64 | + |
| 65 | + client.connect(); |
| 66 | + |
| 67 | + var drainCount = 0; |
| 68 | + client.on('drain', function() { |
| 69 | + drainCount++; |
50 | 70 | });
|
51 |
| -}); |
52 | 71 |
|
| 72 | + test('normally unpaused', function() { |
| 73 | + con.emit('readyForQuery'); |
| 74 | + client.query('boom'); |
| 75 | + assert.emits(client, 'drain', function() { |
| 76 | + assert.equal(drainCount, 1); |
| 77 | + }); |
| 78 | + con.emit('readyForQuery'); |
| 79 | + }); |
| 80 | + |
| 81 | + test('pausing', function() { |
| 82 | + test('unpaused with no queries in between', function() { |
| 83 | + client.pauseDrain(); |
| 84 | + client.resumeDrain(); |
| 85 | + assert.equal(drainCount, 1); |
| 86 | + }); |
| 87 | + |
| 88 | + test('paused', function() { |
| 89 | + test('resumeDrain after empty', function() { |
| 90 | + client.pauseDrain(); |
| 91 | + client.query('asdf'); |
| 92 | + con.emit('readyForQuery'); |
| 93 | + assert.equal(drainCount, 1); |
| 94 | + client.resumeDrain(); |
| 95 | + assert.equal(drainCount, 2); |
| 96 | + }); |
| 97 | + |
| 98 | + test('resumDrain while still pending', function() { |
| 99 | + client.pauseDrain(); |
| 100 | + client.query('asdf'); |
| 101 | + client.query('asdf1'); |
| 102 | + con.emit('readyForQuery'); |
| 103 | + client.resumeDrain(); |
| 104 | + assert.equal(drainCount, 2); |
| 105 | + con.emit('readyForQuery'); |
| 106 | + assert.equal(drainCount, 3); |
| 107 | + }); |
| 108 | + |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | +}); |
0 commit comments