1
+ const assert = require ( 'assert' )
2
+ const Cursor = require ( '../' )
3
+ const pg = require ( 'pg' )
4
+
5
+ const text = 'SELECT generate_series as num FROM generate_series(0, 50)'
6
+
7
+ function poolQueryPromise ( pool , readRowCount ) {
8
+ return new Promise ( ( resolve , reject ) => {
9
+ pool . connect ( ( err , client , done ) => {
10
+ if ( err ) {
11
+ done ( err )
12
+ return reject ( err )
13
+ }
14
+ const cursor = client . query ( new Cursor ( text ) )
15
+ cursor . read ( readRowCount , ( err , res ) => {
16
+ if ( err ) {
17
+ done ( err )
18
+ return reject ( err )
19
+ }
20
+ cursor . close ( err => {
21
+ if ( err ) {
22
+ done ( err )
23
+ return reject ( err )
24
+ }
25
+ done ( )
26
+ resolve ( )
27
+ } )
28
+ } )
29
+ } )
30
+ } )
31
+ }
32
+
33
+ describe ( 'pool' , function ( ) {
34
+ beforeEach ( function ( ) {
35
+ this . pool = new pg . Pool ( { max : 1 } )
36
+ } )
37
+
38
+ afterEach ( function ( ) {
39
+ this . pool . end ( )
40
+ } )
41
+
42
+ it ( 'closes cursor early, single pool query' , function ( done ) {
43
+ poolQueryPromise ( this . pool , 25 )
44
+ . then ( ( ) => done ( ) )
45
+ . catch ( err => {
46
+ assert . ifError ( err )
47
+ done ( )
48
+ } )
49
+ } )
50
+
51
+ it ( 'closes cursor early, saturated pool' , function ( done ) {
52
+ const promises = [ ]
53
+ for ( let i = 0 ; i < 10 ; i ++ ) {
54
+ promises . push ( poolQueryPromise ( this . pool , 25 ) )
55
+ }
56
+ Promise . all ( promises )
57
+ . then ( ( ) => done ( ) )
58
+ . catch ( err => {
59
+ assert . ifError ( err )
60
+ done ( )
61
+ } )
62
+ } )
63
+
64
+ it ( 'closes exhausted cursor, single pool query' , function ( done ) {
65
+ poolQueryPromise ( this . pool , 100 )
66
+ . then ( ( ) => done ( ) )
67
+ . catch ( err => {
68
+ assert . ifError ( err )
69
+ done ( )
70
+ } )
71
+ } )
72
+
73
+ it ( 'closes exhausted cursor, saturated pool' , function ( done ) {
74
+ const promises = [ ]
75
+ for ( let i = 0 ; i < 10 ; i ++ ) {
76
+ promises . push ( poolQueryPromise ( this . pool , 100 ) )
77
+ }
78
+ Promise . all ( promises )
79
+ . then ( ( ) => done ( ) )
80
+ . catch ( err => {
81
+ assert . ifError ( err )
82
+ done ( )
83
+ } )
84
+ } )
85
+ } )
0 commit comments