@@ -26,6 +26,38 @@ const longTestTimeout = 3000;
26
26
27
27
const db = request . client ( `${ url } /${ dbName } ` , { parallelism : 1 } ) ;
28
28
29
+ const seqSuffix = Buffer . alloc ( 124 , 'abc123' ) . toString ( 'base64' ) ;
30
+ function provideChanges ( batchSize , totalChanges , fullResponse = false ) {
31
+ let pending = totalChanges ;
32
+ const sparseResultsArray = Array ( batchSize ) . fill ( {
33
+ seq : null ,
34
+ id : 'doc' ,
35
+ changes : [ { rev : '1-abcdef0123456789abcdef0123456789' } ]
36
+ } ) ;
37
+ nock ( url )
38
+ . post ( `/${ dbName } /_changes` )
39
+ . query ( true )
40
+ . times ( totalChanges / batchSize + ( totalChanges % batchSize > 0 ? 1 : 0 ) )
41
+ . reply ( 200 , ( uri , requestBody ) => {
42
+ pending -= batchSize ;
43
+ const lastSeq = ( totalChanges - pending ) ;
44
+ const seq = lastSeq - batchSize ;
45
+ return {
46
+ results : fullResponse
47
+ ? Array . from ( Array ( batchSize ) , ( _ , i ) => {
48
+ return {
49
+ seq : `${ seq + i } -${ seqSuffix } ` ,
50
+ id : `doc${ seq + i } ` ,
51
+ changes : [ { rev : '1-abcdef0123456789abcdef0123456789' } ]
52
+ } ;
53
+ } )
54
+ : sparseResultsArray ,
55
+ pending : pending ,
56
+ last_seq : `${ lastSeq } -abc`
57
+ } ;
58
+ } ) ;
59
+ }
60
+
29
61
describe ( '#unit Check spool changes' , function ( ) {
30
62
it ( 'should terminate on request error' , function ( done ) {
31
63
nock ( url )
@@ -59,4 +91,66 @@ describe('#unit Check spool changes', function() {
59
91
done ( ) ;
60
92
} ) ;
61
93
} ) . timeout ( longTestTimeout ) ;
94
+
95
+ it ( 'should keep collecting changes' , function ( done ) {
96
+ // This test validates that spooling will correctly
97
+ // continue across multiple requests
98
+ // (4 batches of 100000 to be precise).
99
+ // This test might take up to 10 seconds
100
+ this . timeout ( 10 * 1000 ) ;
101
+
102
+ // Use full changes for this test
103
+ provideChanges ( 100000 , 400000 , true ) ;
104
+ changes ( db , '/dev/null' , 500 , null , function ( err ) {
105
+ assert . ok ( ! err ) ;
106
+ assert . ok ( nock . isDone ( ) ) ;
107
+ done ( ) ;
108
+ } ) ;
109
+ } ) ;
110
+
111
+ it ( 'should keep collecting sparse changes' , function ( done ) {
112
+ // This test checks that making thousands of requests doesn't
113
+ // make anything bad happen.
114
+ // This test might take up to 25 seconds
115
+ this . timeout ( 25 * 1000 ) ;
116
+ // Use sparse changes for this test and a batch size of 1
117
+ provideChanges ( 1 , 2500 ) ;
118
+ changes ( db , '/dev/null' , 500 , null , function ( err ) {
119
+ assert . ok ( ! err ) ;
120
+ assert . ok ( nock . isDone ( ) ) ;
121
+ done ( ) ;
122
+ } ) ;
123
+ } ) ;
124
+ } ) ;
125
+
126
+ describe ( 'Longer spool changes checks' , function ( ) {
127
+ it ( '#slow should keep collecting changes (25M)' , function ( done ) {
128
+ // This test might take up to 2 minutes
129
+ this . timeout ( 2 * 60 * 1000 ) ;
130
+ // Note changes spooling uses a constant batch size, we are setting
131
+ // a test value here and setting the buffer to match
132
+ const batch = 100000 ;
133
+ // Use sparse changes for this test
134
+ provideChanges ( batch , 25000000 ) ;
135
+ changes ( db , '/dev/null' , batch , null , function ( err ) {
136
+ assert . ok ( ! err ) ;
137
+ assert . ok ( nock . isDone ( ) ) ;
138
+ done ( ) ;
139
+ } ) ;
140
+ } ) ;
141
+
142
+ it ( '#slower should keep collecting changes (500M)' , function ( done ) {
143
+ // This test might take up to 90 minutes
144
+ this . timeout ( 90 * 60 * 1000 ) ;
145
+ // Note changes spooling uses a constant batch size, we are setting
146
+ // a test value here and setting the buffer to match
147
+ const batch = 1000000 ;
148
+ // Use full changes for this test to exercise load
149
+ provideChanges ( batch , 500000000 , true ) ;
150
+ changes ( db , '/dev/null' , batch , null , function ( err ) {
151
+ assert . ok ( ! err ) ;
152
+ assert . ok ( nock . isDone ( ) ) ;
153
+ done ( ) ;
154
+ } ) ;
155
+ } ) ;
62
156
} ) ;
0 commit comments