@@ -143,6 +143,51 @@ test('bulk index', t => {
143143 } )
144144 } )
145145
146+ t . test ( 'refreshOnCompletion' , async t => {
147+ let count = 0
148+ const MockConnection = connection . buildMockConnection ( {
149+ onRequest ( params ) {
150+ if ( params . method === 'GET' ) {
151+ t . strictEqual ( params . path , '/_all/_refresh' )
152+ return { body : { acknowledged : true } }
153+ } else {
154+ t . strictEqual ( params . path , '/_bulk' )
155+ t . match ( params . headers , { 'Content-Type' : 'application/x-ndjson' } )
156+ const [ action , payload ] = params . body . split ( '\n' )
157+ t . deepEqual ( JSON . parse ( action ) , { index : { _index : 'test' } } )
158+ t . deepEqual ( JSON . parse ( payload ) , dataset [ count ++ ] )
159+ return { body : { errors : false , items : [ { } ] } }
160+ }
161+ }
162+ } )
163+
164+ const client = new Client ( {
165+ node : 'http://localhost:9200' ,
166+ Connection : MockConnection
167+ } )
168+ const result = await client . helpers . bulk ( {
169+ datasource : dataset . slice ( ) ,
170+ flushBytes : 1 ,
171+ concurrency : 1 ,
172+ refreshOnCompletion : true ,
173+ onDocument ( doc ) {
174+ return {
175+ index : { _index : 'test' }
176+ }
177+ }
178+ } )
179+
180+ t . type ( result . time , 'number' )
181+ t . type ( result . bytes , 'number' )
182+ t . match ( result , {
183+ total : 3 ,
184+ successful : 3 ,
185+ retry : 0 ,
186+ failed : 0 ,
187+ aborted : false
188+ } )
189+ } )
190+
146191 t . test ( 'Should perform a bulk request (custom action)' , async t => {
147192 let count = 0
148193 const MockConnection = connection . buildMockConnection ( {
@@ -262,6 +307,55 @@ test('bulk index', t => {
262307 server . stop ( )
263308 } )
264309
310+ t . test ( 'Should perform a bulk request (retry a single document from batch)' , async t => {
311+ function handler ( req , res ) {
312+ res . setHeader ( 'content-type' , 'application/json' )
313+ res . end ( JSON . stringify ( {
314+ took : 0 ,
315+ errors : true ,
316+ items : [
317+ { index : { status : 200 } } ,
318+ { index : { status : 429 } } ,
319+ { index : { status : 200 } }
320+ ]
321+ } ) )
322+ }
323+
324+ const [ { port } , server ] = await buildServer ( handler )
325+ const client = new Client ( { node : `http://localhost:${ port } ` } )
326+ const result = await client . helpers . bulk ( {
327+ datasource : dataset . slice ( ) ,
328+ concurrency : 1 ,
329+ wait : 10 ,
330+ retries : 0 ,
331+ onDocument ( doc ) {
332+ return {
333+ index : { _index : 'test' }
334+ }
335+ } ,
336+ onDrop ( doc ) {
337+ t . deepEqual ( doc , {
338+ status : 429 ,
339+ error : null ,
340+ operation : { index : { _index : 'test' } } ,
341+ document : { user : 'arya' , age : 18 } ,
342+ retried : false
343+ } )
344+ }
345+ } )
346+
347+ t . type ( result . time , 'number' )
348+ t . type ( result . bytes , 'number' )
349+ t . match ( result , {
350+ total : 3 ,
351+ successful : 2 ,
352+ retry : 0 ,
353+ failed : 1 ,
354+ aborted : false
355+ } )
356+ server . stop ( )
357+ } )
358+
265359 t . test ( 'Should perform a bulk request (failure)' , async t => {
266360 if ( semver . lt ( process . versions . node , '10.0.0' ) ) {
267361 t . skip ( 'This test will not pass on Node v8' )
@@ -475,6 +569,35 @@ test('bulk index', t => {
475569 server . stop ( )
476570 } )
477571
572+ t . test ( 'Invalid operation' , t => {
573+ t . plan ( 2 )
574+ const MockConnection = connection . buildMockConnection ( {
575+ onRequest ( params ) {
576+ return { body : { errors : false , items : [ { } ] } }
577+ }
578+ } )
579+
580+ const client = new Client ( {
581+ node : 'http://localhost:9200' ,
582+ Connection : MockConnection
583+ } )
584+ client . helpers
585+ . bulk ( {
586+ datasource : dataset . slice ( ) ,
587+ flushBytes : 1 ,
588+ concurrency : 1 ,
589+ onDocument ( doc ) {
590+ return {
591+ foo : { _index : 'test' }
592+ }
593+ }
594+ } )
595+ . catch ( err => {
596+ t . true ( err instanceof errors . ConfigurationError )
597+ t . is ( err . message , `Bulk helper invalid action: 'foo'` )
598+ } )
599+ } )
600+
478601 t . end ( )
479602 } )
480603
0 commit comments