@@ -4,49 +4,85 @@ var concat = require('concat-stream')
4
4
var QueryStream = require ( '../' )
5
5
var helper = require ( './helper' )
6
6
7
+ if ( process . version . startsWith ( 'v8.' ) ) {
8
+ return console . error ( 'warning! node versions less than 10lts no longer supported & stream closing semantics may not behave properly' ) ;
9
+ }
10
+
7
11
helper ( 'close' , function ( client ) {
8
12
it ( 'emits close' , function ( done ) {
9
- var stream = new QueryStream ( 'SELECT * FROM generate_series(0, $1) num' , [ 3 ] , { batchSize : 2 , highWaterMark : 2 } )
13
+ var stream = new QueryStream ( 'SELECT * FROM generate_series(0, $1) num' , [ 3 ] , { batchSize : 2 , highWaterMark : 2 } )
10
14
var query = client . query ( stream )
11
- query . pipe ( concat ( function ( ) { } ) )
15
+ query . pipe ( concat ( function ( ) { } ) )
12
16
query . on ( 'close' , done )
13
17
} )
14
18
} )
15
19
16
20
helper ( 'early close' , function ( client ) {
17
21
it ( 'can be closed early' , function ( done ) {
18
- var stream = new QueryStream ( 'SELECT * FROM generate_series(0, $1) num' , [ 20000 ] , { batchSize : 2 , highWaterMark : 2 } )
22
+ var stream = new QueryStream ( 'SELECT * FROM generate_series(0, $1) num' , [ 20000 ] , { batchSize : 2 , highWaterMark : 2 } )
19
23
var query = client . query ( stream )
20
24
var readCount = 0
21
25
query . on ( 'readable' , function ( ) {
22
26
readCount ++
23
27
query . read ( )
24
28
} )
25
29
query . once ( 'readable' , function ( ) {
26
- query . close ( )
30
+ query . destroy ( )
27
31
} )
28
32
query . on ( 'close' , function ( ) {
29
33
assert ( readCount < 10 , 'should not have read more than 10 rows' )
30
34
done ( )
31
35
} )
32
36
} )
33
- } )
34
37
35
- helper ( 'close callback' , function ( client ) {
36
- it ( 'notifies an optional callback when the conneciton is closed' , function ( done ) {
37
- var stream = new QueryStream ( 'SELECT * FROM generate_series(0, $1) num' , [ 10 ] , { batchSize : 2 , highWaterMark : 2 } )
38
- var query = client . query ( stream )
39
- query . once ( 'readable' , function ( ) { // only reading once
40
- query . read ( )
41
- } )
42
- query . once ( 'readable' , function ( ) {
43
- query . close ( function ( ) {
44
- // nothing to assert. This test will time out if the callback does not work.
45
- done ( )
38
+ it ( 'can destroy stream while reading' , function ( done ) {
39
+ var stream = new QueryStream ( 'SELECT * FROM generate_series(0, 100), pg_sleep(1)' )
40
+ client . query ( stream )
41
+ stream . on ( 'data' , ( ) => done ( new Error ( 'stream should not have returned rows' ) ) )
42
+ setTimeout ( ( ) => {
43
+ stream . destroy ( )
44
+ stream . on ( 'close' , done )
45
+ } , 100 )
46
+ } )
47
+
48
+ it ( 'emits an error when calling destroy with an error' , function ( done ) {
49
+ var stream = new QueryStream ( 'SELECT * FROM generate_series(0, 100), pg_sleep(1)' )
50
+ client . query ( stream )
51
+ stream . on ( 'data' , ( ) => done ( new Error ( 'stream should not have returned rows' ) ) )
52
+ setTimeout ( ( ) => {
53
+ stream . destroy ( new Error ( 'intentional error' ) )
54
+ stream . on ( 'error' , ( err ) => {
55
+ // make sure there's an error
56
+ assert ( err ) ;
57
+ assert . strictEqual ( err . message , 'intentional error' ) ;
58
+ done ( ) ;
46
59
} )
60
+ } , 100 )
61
+ } )
62
+
63
+ it ( 'can destroy stream while reading an error' , function ( done ) {
64
+ var stream = new QueryStream ( 'SELECT * from pg_sleep(1), basdfasdf;' )
65
+ client . query ( stream )
66
+ stream . on ( 'data' , ( ) => done ( new Error ( 'stream should not have returned rows' ) ) )
67
+ stream . once ( 'error' , ( ) => {
68
+ stream . destroy ( )
69
+ // wait a bit to let any other errors shake through
70
+ setTimeout ( done , 100 )
47
71
} )
48
- query . on ( 'close' , function ( ) {
49
- assert ( false , 'close event should not fire' ) // no close event because we did not read to the end of the stream.
50
- } )
72
+ } )
73
+
74
+ it ( 'does not crash when destroying the stream immediately after calling read' , function ( done ) {
75
+ var stream = new QueryStream ( 'SELECT * from generate_series(0, 100), pg_sleep(1);' )
76
+ client . query ( stream )
77
+ stream . on ( 'data' , ( ) => done ( new Error ( 'stream should not have returned rows' ) ) )
78
+ stream . destroy ( )
79
+ stream . on ( 'close' , done )
80
+ } )
81
+
82
+ it ( 'does not crash when destroying the stream before its submitted' , function ( done ) {
83
+ var stream = new QueryStream ( 'SELECT * from generate_series(0, 100), pg_sleep(1);' )
84
+ stream . on ( 'data' , ( ) => done ( new Error ( 'stream should not have returned rows' ) ) )
85
+ stream . destroy ( )
86
+ stream . on ( 'close' , done )
51
87
} )
52
88
} )
0 commit comments