1
1
var expect = require ( 'expect.js' )
2
- var Client = require ( 'pg' ) . Client
3
2
var co = require ( 'co' )
4
3
var Promise = require ( 'bluebird' )
5
4
var _ = require ( 'lodash' )
6
5
7
- var Pool = require ( '../' )
6
+ var describe = require ( 'mocha' ) . describe
7
+ var it = require ( 'mocha' ) . it
8
8
9
- describe ( 'pool' , function ( ) {
9
+ var Pool = require ( '../' )
10
10
11
- describe ( 'with callbacks' , function ( ) {
12
- it ( 'works totally unconfigured' , function ( done ) {
11
+ describe ( 'pool' , function ( ) {
12
+ describe ( 'with callbacks' , function ( ) {
13
+ it ( 'works totally unconfigured' , function ( done ) {
13
14
const pool = new Pool ( )
14
- pool . connect ( function ( err , client , release ) {
15
+ pool . connect ( function ( err , client , release ) {
15
16
if ( err ) return done ( err )
16
- client . query ( 'SELECT NOW()' , function ( err , res ) {
17
+ client . query ( 'SELECT NOW()' , function ( err , res ) {
17
18
release ( )
18
19
if ( err ) return done ( err )
19
20
expect ( res . rows ) . to . have . length ( 1 )
@@ -22,37 +23,39 @@ describe('pool', function() {
22
23
} )
23
24
} )
24
25
25
- it ( 'passes props to clients' , function ( done ) {
26
+ it ( 'passes props to clients' , function ( done ) {
26
27
const pool = new Pool ( { binary : true } )
27
- pool . connect ( function ( err , client , release ) {
28
+ pool . connect ( function ( err , client , release ) {
28
29
release ( )
30
+ if ( err ) return done ( err )
29
31
expect ( client . binary ) . to . eql ( true )
30
32
pool . end ( done )
31
33
} )
32
34
} )
33
35
34
- it ( 'removes client if it errors in background' , function ( done ) {
36
+ it ( 'removes client if it errors in background' , function ( done ) {
35
37
const pool = new Pool ( )
36
- pool . connect ( function ( err , client , release ) {
38
+ pool . connect ( function ( err , client , release ) {
37
39
release ( )
40
+ if ( err ) return done ( err )
38
41
client . testString = 'foo'
39
- setTimeout ( function ( ) {
42
+ setTimeout ( function ( ) {
40
43
client . emit ( 'error' , new Error ( 'on purpose' ) )
41
44
} , 10 )
42
45
} )
43
- pool . on ( 'error' , function ( err ) {
46
+ pool . on ( 'error' , function ( err ) {
44
47
expect ( err . message ) . to . be ( 'on purpose' )
45
48
expect ( err . client ) . to . not . be ( undefined )
46
49
expect ( err . client . testString ) . to . be ( 'foo' )
47
- err . client . connection . stream . on ( 'end' , function ( ) {
50
+ err . client . connection . stream . on ( 'end' , function ( ) {
48
51
pool . end ( done )
49
52
} )
50
53
} )
51
54
} )
52
55
} )
53
56
54
- describe ( 'with promises' , function ( ) {
55
- it ( 'connects and disconnects' , co . wrap ( function * ( ) {
57
+ describe ( 'with promises' , function ( ) {
58
+ it ( 'connects and disconnects' , co . wrap ( function * ( ) {
56
59
var pool = new Pool ( )
57
60
var client = yield pool . connect ( )
58
61
expect ( pool . pool . availableObjectsCount ( ) ) . to . be ( 0 )
@@ -64,13 +67,13 @@ describe('pool', function() {
64
67
return yield pool . end ( )
65
68
} ) )
66
69
67
- it ( 'properly pools clients' , co . wrap ( function * ( ) {
70
+ it ( 'properly pools clients' , co . wrap ( function * ( ) {
68
71
var pool = new Pool ( { poolSize : 9 } )
69
72
var count = 0
70
73
while ( count < 30 ) {
71
74
count ++
72
- pool . connect ( ) . then ( function ( client ) {
73
- client . queryAsync ( 'select $1::text as name' , [ 'hi' ] ) . then ( function ( res ) {
75
+ pool . connect ( ) . then ( function ( client ) {
76
+ client . queryAsync ( 'select $1::text as name' , [ 'hi' ] ) . then ( function ( res ) {
74
77
client . release ( )
75
78
} )
76
79
} )
@@ -80,28 +83,25 @@ describe('pool', function() {
80
83
return yield pool . end ( )
81
84
} ) )
82
85
83
- it ( 'supports just running queries' , co . wrap ( function * ( ) {
86
+ it ( 'supports just running queries' , co . wrap ( function * ( ) {
84
87
var pool = new Pool ( { poolSize : 9 } )
85
- var count = 0
86
- var queries = _ . times ( 30 ) . map ( function ( ) {
88
+ var queries = _ . times ( 30 ) . map ( function ( ) {
87
89
return pool . query ( 'SELECT $1::text as name' , [ 'hi' ] )
88
90
} )
89
- console . log ( 'executing' )
90
91
yield queries
91
92
expect ( pool . pool . getPoolSize ( ) ) . to . be ( 9 )
92
93
expect ( pool . pool . availableObjectsCount ( ) ) . to . be ( 9 )
93
94
return yield pool . end ( )
94
95
} ) )
95
96
96
- it ( 'recovers from all errors' , co . wrap ( function * ( ) {
97
+ it ( 'recovers from all errors' , co . wrap ( function * ( ) {
97
98
var pool = new Pool ( { poolSize : 9 } )
98
99
var count = 0
99
100
100
- while ( count ++ < 30 ) {
101
+ while ( count ++ < 30 ) {
101
102
try {
102
103
yield pool . query ( 'SELECT lksjdfd' )
103
- } catch ( e ) {
104
- }
104
+ } catch ( e ) { }
105
105
}
106
106
var res = yield pool . query ( 'SELECT $1::text as name' , [ 'hi' ] )
107
107
expect ( res . rows ) . to . eql ( [ { name : 'hi' } ] )
0 commit comments