/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.gateway.standalone.http;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertEquals;

import io.gravitee.common.http.HttpStatusCode;
import io.gravitee.gateway.standalone.AbstractWiremockGatewayTest;
import io.gravitee.gateway.standalone.junit.annotation.ApiDescriptor;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.*;
import java.util.concurrent.*;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author GraviteeSource Team
 */
@ApiDescriptor("/io/gravitee/gateway/standalone/http/teams.json")
public class ParallelizedRequestsGatewayTest extends AbstractWiremockGatewayTest {

    private final int NUMBER_OF_CLIENTS = 10;
    private final int NUMBER_OF_REQUESTS = 1000;

    private final ExecutorService executorService = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());

    @Test
    public void shouldProcessParallelRequests() throws Exception {
        wireMockRule.stubFor(get("/team/my_team").willReturn(ok()));

        Vertx vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true));

        HttpClient client = vertx.createHttpClient();
        CountDownLatch latch = new CountDownLatch(NUMBER_OF_CLIENTS * NUMBER_OF_REQUESTS);

        Runnable calls = () -> {
            for (int i = 0; i < NUMBER_OF_REQUESTS; i++) {
                client.request(
                    new RequestOptions().setAbsoluteURI("http://localhost:8082/test/my_team").setMethod(HttpMethod.GET),
                    new Handler<AsyncResult<HttpClientRequest>>() {
                        @Override
                        public void handle(AsyncResult<HttpClientRequest> httpClientRequestEvt) {
                            if (httpClientRequestEvt.succeeded()) {
                                httpClientRequestEvt
                                    .result()
                                    .send(
                                        new Handler<AsyncResult<HttpClientResponse>>() {
                                            @Override
                                            public void handle(AsyncResult<HttpClientResponse> httpClientResponseEvt) {
                                                if (httpClientResponseEvt.succeeded()) {
                                                    assertEquals(HttpStatusCode.OK_200, httpClientResponseEvt.result().statusCode());
                                                } else {
                                                    Assert.fail();
                                                }
                                            }
                                        }
                                    );
                            } else {
                                Assert.fail();
                            }

                            latch.countDown();
                        }
                    }
                );
            }
        };

        for (int i = 0; i < NUMBER_OF_CLIENTS; i++) {
            executorService.submit(calls);
        }

        latch.await(30, TimeUnit.SECONDS);
        executorService.shutdown();
        wireMockRule.verify(NUMBER_OF_CLIENTS * NUMBER_OF_REQUESTS, getRequestedFor(urlPathEqualTo("/team/my_team")));
    }
}

@@ -986,27 +986,21 @@
 tx.core.
-AsyncResult
+Vertx
 ;%0Aimport
@@ -1014,23 +1014,28 @@
 tx.core.
-Handler
+VertxOptions
 ;%0Aimport
@@ -1041,37 +1041,47 @@
 t io.vertx.core.
-Vertx
+http.HttpClient
 ;%0Aimport io.vert
@@ -1083,36 +1083,39 @@
 .vertx.core.
-VertxOptions
+http.HttpMethod
 ;%0Aimport io.
@@ -1130,17 +1130,30 @@
 re.http.
-*
+RequestOptions
 ;%0Aimport
@@ -1194,21 +1194,19 @@
 g.junit.
-Asser
+Tes
 t;%0Aimpor
@@ -1211,26 +1211,60 @@
 ort org.
-junit.Test
+slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0A/**%0A 
@@ -1512,16 +1512,115 @@
 Test %7B%0A%0A
+    private final Logger logger = LoggerFactory.getLogger(ParallelizedRequestsGatewayTest.class);%0A%0A
     priv
@@ -2496,164 +2496,8 @@
     
- new Handler%3CAsyncResult%3CHttpClientRequest%3E%3E() %7B%0A                        @Override%0A                        public void handle(AsyncResult%3CHttpClientRequest%3E
  htt
@@ -2513,23 +2513,22 @@
 questEvt
-) %7B%0A   
+ -%3E %7B%0A
         
@@ -2535,33 +2535,32 @@
                 
- 
 if (httpClientRe
@@ -2575,36 +2575,32 @@
 .succeeded()) %7B%0A
-    
                 
@@ -2664,20 +2664,16 @@
         
-    
 .result(
@@ -2706,20 +2706,16 @@
         
-   
- 
 .send(%0A 
@@ -2752,210 +2752,8 @@
     
-     new Handler%3CAsyncResult%3CHttpClientResponse%3E%3E() %7B%0A                                            @Override%0A                                            public void handle(AsyncResult%3CHttpClientResponse%3E
  htt
@@ -2770,28 +2770,22 @@
 ponseEvt
-) %7B%0A        
+ -%3E %7B%0A
         
@@ -2853,32 +2853,24 @@
 ceeded()) %7B%0A
-        
             
@@ -3027,38 +3027,106 @@
         
-    %7D else %7B%0A         
+latch.countDown(); // We count down only when we are sure the response has been received.%0A
         
@@ -3157,33 +3157,24 @@
         
-   Assert.fail();
+%7D else %7B
 %0A       
@@ -3206,34 +3206,113 @@
             
-    %7D%0A    
+logger.error(%22An error occurred during client response%22, httpClientResponseEvt.cause());%0A
             
@@ -3373,34 +3373,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -3430,36 +3430,32 @@
                 
-    
 %7D else %7B%0A       
@@ -3479,102 +3479,92 @@
     
-    Assert.fail();%0A                            %7D%0A%0A                            latch.countDown(
+logger.error(%22An error occurred during client request%22, httpClientRequestEvt.cause()
 );%0A 

fix unit test failing randomly