2727import org .elasticsearch .action .ActionListener ;
2828import org .elasticsearch .action .DocWriteRequest ;
2929import org .elasticsearch .action .DocWriteResponse ;
30+ import org .elasticsearch .action .LatchedActionListener ;
3031import org .elasticsearch .action .bulk .BackoffPolicy ;
3132import org .elasticsearch .action .bulk .BulkItemResponse ;
3233import org .elasticsearch .action .bulk .BulkProcessor ;
6061import org .elasticsearch .script .ScriptType ;
6162import org .elasticsearch .search .fetch .subphase .FetchSourceContext ;
6263
63- import java .io .IOException ;
6464import java .util .Collections ;
6565import java .util .Date ;
6666import java .util .HashMap ;
6767import java .util .Map ;
68+ import java .util .concurrent .CountDownLatch ;
6869import java .util .concurrent .TimeUnit ;
6970
7071import static java .util .Collections .emptyMap ;
@@ -227,9 +228,8 @@ public void testIndex() throws Exception {
227228 }
228229 {
229230 IndexRequest request = new IndexRequest ("posts" , "doc" , "async" ).source ("field" , "value" );
230-
231- // tag::index-execute-async
232- client .indexAsync (request , new ActionListener <IndexResponse >() {
231+ // tag::index-execute-listener
232+ ActionListener <IndexResponse > listener = new ActionListener <IndexResponse >() {
233233 @ Override
234234 public void onResponse (IndexResponse indexResponse ) {
235235 // <1>
@@ -239,10 +239,18 @@ public void onResponse(IndexResponse indexResponse) {
239239 public void onFailure (Exception e ) {
240240 // <2>
241241 }
242- });
242+ };
243+ // end::index-execute-listener
244+
245+ // Replace the empty listener by a blocking listener in test
246+ final CountDownLatch latch = new CountDownLatch (1 );
247+ listener = new LatchedActionListener <>(listener , latch );
248+
249+ // tag::index-execute-async
250+ client .indexAsync (request , listener ); // <1>
243251 // end::index-execute-async
244252
245- assertBusy (() -> assertTrue (client . exists ( new GetRequest ( "posts" , "doc" , "async" )) ));
253+ assertTrue (latch . await ( 30L , TimeUnit . SECONDS ));
246254 }
247255 }
248256
@@ -490,8 +498,8 @@ public void testUpdate() throws Exception {
490498 {
491499 UpdateRequest request = new UpdateRequest ("posts" , "doc" , "async" ).doc ("reason" , "async update" ).docAsUpsert (true );
492500
493- // tag::update-execute-async
494- client . updateAsync ( request , new ActionListener <UpdateResponse >() {
501+ // tag::update-execute-listener
502+ ActionListener < UpdateResponse > listener = new ActionListener <UpdateResponse >() {
495503 @ Override
496504 public void onResponse (UpdateResponse updateResponse ) {
497505 // <1>
@@ -501,10 +509,18 @@ public void onResponse(UpdateResponse updateResponse) {
501509 public void onFailure (Exception e ) {
502510 // <2>
503511 }
504- });
512+ };
513+ // end::update-execute-listener
514+
515+ // Replace the empty listener by a blocking listener in test
516+ final CountDownLatch latch = new CountDownLatch (1 );
517+ listener = new LatchedActionListener <>(listener , latch );
518+
519+ // tag::update-execute-async
520+ client .updateAsync (request , listener ); // <1>
505521 // end::update-execute-async
506522
507- assertBusy (() -> assertTrue (client . exists ( new GetRequest ( "posts" , "doc" , "async" )) ));
523+ assertTrue (latch . await ( 30L , TimeUnit . SECONDS ));
508524 }
509525 }
510526
@@ -602,8 +618,8 @@ public void testDelete() throws Exception {
602618
603619 DeleteRequest request = new DeleteRequest ("posts" , "doc" , "async" );
604620
605- // tag::delete-execute-async
606- client . deleteAsync ( request , new ActionListener <DeleteResponse >() {
621+ // tag::delete-execute-listener
622+ ActionListener < DeleteResponse > listener = new ActionListener <DeleteResponse >() {
607623 @ Override
608624 public void onResponse (DeleteResponse deleteResponse ) {
609625 // <1>
@@ -613,14 +629,22 @@ public void onResponse(DeleteResponse deleteResponse) {
613629 public void onFailure (Exception e ) {
614630 // <2>
615631 }
616- });
632+ };
633+ // end::delete-execute-listener
634+
635+ // Replace the empty listener by a blocking listener in test
636+ final CountDownLatch latch = new CountDownLatch (1 );
637+ listener = new LatchedActionListener <>(listener , latch );
638+
639+ // tag::delete-execute-async
640+ client .deleteAsync (request , listener ); // <1>
617641 // end::delete-execute-async
618642
619- assertBusy (() -> assertFalse ( client . exists ( new GetRequest ( "posts" , "doc" , "async" )) ));
643+ assertTrue ( latch . await ( 30L , TimeUnit . SECONDS ));
620644 }
621645 }
622646
623- public void testBulk () throws IOException {
647+ public void testBulk () throws Exception {
624648 RestHighLevelClient client = highLevelClient ();
625649 {
626650 // tag::bulk-request
@@ -696,8 +720,8 @@ public void testBulk() throws IOException {
696720 request .waitForActiveShards (ActiveShardCount .ALL ); // <2>
697721 // end::bulk-request-active-shards
698722
699- // tag::bulk-execute-async
700- client . bulkAsync ( request , new ActionListener <BulkResponse >() {
723+ // tag::bulk-execute-listener
724+ ActionListener < BulkResponse > listener = new ActionListener <BulkResponse >() {
701725 @ Override
702726 public void onResponse (BulkResponse bulkResponse ) {
703727 // <1>
@@ -707,12 +731,22 @@ public void onResponse(BulkResponse bulkResponse) {
707731 public void onFailure (Exception e ) {
708732 // <2>
709733 }
710- });
734+ };
735+ // end::bulk-execute-listener
736+
737+ // Replace the empty listener by a blocking listener in test
738+ final CountDownLatch latch = new CountDownLatch (1 );
739+ listener = new LatchedActionListener <>(listener , latch );
740+
741+ // tag::bulk-execute-async
742+ client .bulkAsync (request , listener ); // <1>
711743 // end::bulk-execute-async
744+
745+ assertTrue (latch .await (30L , TimeUnit .SECONDS ));
712746 }
713747 }
714748
715- public void testGet () throws IOException {
749+ public void testGet () throws Exception {
716750 RestHighLevelClient client = highLevelClient ();
717751 {
718752 String mappings = "{\n " +
@@ -839,8 +873,9 @@ public void testGet() throws IOException {
839873 }
840874 {
841875 GetRequest request = new GetRequest ("posts" , "doc" , "1" );
842- //tag::get-execute-async
843- client .getAsync (request , new ActionListener <GetResponse >() {
876+
877+ //tag::get-execute-listener
878+ ActionListener <GetResponse > listener = new ActionListener <GetResponse >() {
844879 @ Override
845880 public void onResponse (GetResponse getResponse ) {
846881 // <1>
@@ -850,8 +885,18 @@ public void onResponse(GetResponse getResponse) {
850885 public void onFailure (Exception e ) {
851886 // <2>
852887 }
853- });
888+ };
889+ //end::get-execute-listener
890+
891+ // Replace the empty listener by a blocking listener in test
892+ final CountDownLatch latch = new CountDownLatch (1 );
893+ listener = new LatchedActionListener <>(listener , latch );
894+
895+ //tag::get-execute-async
896+ client .getAsync (request , listener ); // <1>
854897 //end::get-execute-async
898+
899+ assertTrue (latch .await (30L , TimeUnit .SECONDS ));
855900 }
856901 {
857902 //tag::get-indexnotfound
@@ -879,7 +924,7 @@ public void onFailure(Exception e) {
879924 }
880925 }
881926
882- public void testBulkProcessor () throws InterruptedException , IOException {
927+ public void testBulkProcessor () throws InterruptedException {
883928 RestHighLevelClient client = highLevelClient ();
884929 {
885930 // tag::bulk-processor-init
0 commit comments