-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathasync-socket.js
155 lines (136 loc) · 5.17 KB
/
async-socket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var LibPQ = require('../')
var helper = require('./helper')
var assert = require('assert');
var consume = function(pq, cb) {
if(!pq.isBusy()) return cb();
pq.startReader();
var onReadable = function() {
assert(pq.consumeInput(), pq.errorMessage());
if(pq.isBusy()) {
console.log('consuming a 2nd buffer of input later...')
return;
}
pq.removeListener('readable', onReadable);
pq.stopReader();
cb();
}
pq.on('readable', onReadable);
}
describe('async simple query', function() {
helper.setupIntegration();
it('dispatches simple query', function(done) {
var pq = this.pq;
assert(this.pq.setNonBlocking(true));
this.pq.writable(function() {
var success = pq.sendQuery('SELECT 1');
assert.strictEqual(pq.flush(), 0, 'Should have flushed all data to socket');
assert(success, pq.errorMessage());
consume(pq, function() {
assert.ifError(pq.errorMessage());
assert(pq.getResult());
assert.strictEqual(pq.getResult(), false);
assert.strictEqual(pq.ntuples(), 1);
assert.strictEqual(pq.getvalue(0, 0), '1');
done();
});
});
});
it('dispatches parameterized query', function(done) {
var pq = this.pq;
var success = pq.sendQueryParams('SELECT $1::text as name', ['Brian']);
assert(success, pq.errorMessage());
assert.strictEqual(pq.flush(), 0, 'Should have flushed query text & parameters');
consume(pq, function() {
assert.ifError(pq.errorMessage());
assert(pq.getResult());
assert.strictEqual(pq.getResult(), false);
assert.strictEqual(pq.ntuples(), 1);
assert.equal(pq.getvalue(0, 0), 'Brian');
done();
})
});
it('dispatches query with binary parameter', function(done) {
var pq = this.pq;
var string = 'fo\\o';
var buffer = helper.createBuffer(string, 'utf8');
var success = pq.sendQueryParams('SELECT $1::bytea as value', [buffer]);
assert(success, pq.errorMessage());
assert.strictEqual(pq.flush(), 0, 'Should have flushed query text & parameters');
consume(pq, function() {
assert.ifError(pq.errorMessage());
assert(pq.getResult());
assert.strictEqual(pq.getResult(), false);
assert.strictEqual(pq.ntuples(), 1);
var value = helper.parseHexOutput(pq.getvalue(0, 0));
assert.equal(value, string);
done();
})
});
it('dispatches named query', function(done) {
var pq = this.pq;
var statementName = 'async-get-name';
var success = pq.sendPrepare(statementName, 'SELECT $1::text as name', 1);
assert(success, pq.errorMessage());
assert.strictEqual(pq.flush(), 0, 'Should have flushed query text');
consume(pq, function() {
assert.ifError(pq.errorMessage());
//first time there should be a result
assert(pq.getResult());
//call 'getResult' until it returns false indicating
//there is no more input to consume
assert.strictEqual(pq.getResult(), false);
//since we only prepared a statement there should be
//0 tuples in the result
assert.equal(pq.ntuples(), 0);
//now execute the previously prepared statement
var success = pq.sendQueryPrepared(statementName, ['Brian']);
assert(success, pq.errorMessage());
assert.strictEqual(pq.flush(), 0, 'Should have flushed parameters');
consume(pq, function() {
assert.ifError(pq.errorMessage());
//consume the result of the query execution
assert(pq.getResult());
assert.equal(pq.ntuples(), 1);
assert.equal(pq.getvalue(0, 0), 'Brian');
//call 'getResult' again to ensure we're finished
assert.strictEqual(pq.getResult(), false);
done();
});
});
});
it('dispatches named query with binary parameter', function(done) {
var pq = this.pq;
var statementName = 'async-get-binary-param';
var string = 'fo\\o';
var buffer = helper.createBuffer(string, 'utf8');
var success = pq.sendPrepare(statementName, 'SELECT $1::bytea as value', 1);
assert(success, pq.errorMessage());
assert.strictEqual(pq.flush(), 0, 'Should have flushed query text');
consume(pq, function() {
assert.ifError(pq.errorMessage());
//first time there should be a result
assert(pq.getResult());
//call 'getResult' until it returns false indicating
//there is no more input to consume
assert.strictEqual(pq.getResult(), false);
//since we only prepared a statement there should be
//0 tuples in the result
assert.equal(pq.ntuples(), 0);
//now execute the previously prepared statement
var success = pq.sendQueryPrepared(statementName, [buffer]);
assert(success, pq.errorMessage());
assert.strictEqual(pq.flush(), 0, 'Should have flushed parameters');
consume(pq, function() {
assert.ifError(pq.errorMessage());
//consume the result of the query execution
assert(pq.getResult());
assert.equal(pq.ntuples(), 1);
var value = helper.parseHexOutput(pq.getvalue(0, 0));
assert.equal(value, string);
//call 'getResult' again to ensure we're finished
assert.strictEqual(pq.getResult(), false);
done();
});
});
});
});