package org.mitallast.queue.rest;

import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.*;
import org.junit.Test;
import org.mitallast.queue.common.BaseQueueTest;
import org.mitallast.queue.common.strings.Strings;
import org.mitallast.queue.rest.transport.RestClient;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

public class RestIntegrationTest extends BaseQueueTest {

    @Test
    public void test() throws Exception {
        createQueue();
        assertQueueEmpty();
        warmUp();
        long start = System.currentTimeMillis();
        send(max());
        long end = System.currentTimeMillis();
        printQps("send", max(), start, end);
    }

    @Test
    public void testConcurrent() throws Exception {
        createQueue();
        assertQueueEmpty();
        warmUp();
        long start = System.currentTimeMillis();
        executeConcurrent(() -> send(max()));
        long end = System.currentTimeMillis();
        printQps("send", total(), start, end);
    }

    private void warmUp() throws Exception {
        send(max());
    }

    private void send(int max) throws Exception {
        RestClient restClient = new RestClient(settings());
        restClient.start();
        try {
            logger.info("send");
            byte[] bytes = "{\"message\":\"hello world\"}".getBytes(Strings.UTF8);
            List<Future<FullHttpResponse>> futures = new ArrayList<>(max);
            for (int i = 0; i < max; i++) {
                DefaultFullHttpRequest request = new DefaultFullHttpRequest(
                    HttpVersion.HTTP_1_1,
                    HttpMethod.PUT,
                    "/" + queueName() + "/message",
                    Unpooled.wrappedBuffer(bytes),
                    false
                );
                request.headers().set(HttpHeaderNames.CONTENT_LENGTH, bytes.length);
                futures.add(restClient.send(request));
            }
            restClient.flush();
            logger.info("await");
            for (Future<FullHttpResponse> future : futures) {
                FullHttpResponse response = future.get();
                assert response.status().code() >= 200 : response.status();
                assert response.status().code() < 300 : response.status();
                response.content().release();
            }
            logger.info("await done");
        } finally {
            restClient.stop();
        }
    }
}

@@ -99,16 +99,65 @@
 http.*;%0A
+import org.junit.After;%0Aimport org.junit.Before;%0A
 import o
@@ -167,24 +167,24 @@
 junit.Test;%0A
-
 import org.m
@@ -472,16 +472,607 @@
 Test %7B%0A%0A
+    private List%3CRestClient%3E clients;%0A    private int client;%0A%0A    @Before%0A    public void setUpClient() throws Exception %7B%0A        client = 0;%0A        clients = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C= concurrency(); i++) %7B%0A            RestClient restClient = new RestClient(settings());%0A            restClient.start();%0A            clients.add(restClient);%0A        %7D%0A    %7D%0A%0A    @After%0A    public void tearDownClient() throws Exception %7B%0A        clients.forEach(RestClient::stop);%0A    %7D%0A%0A    private synchronized RestClient restClient() %7B%0A        return clients.get(client++);%0A    %7D%0A%0A
     @Tes
@@ -1734,29 +1734,28 @@
        send(
-max()
+1000
 );%0A    %7D%0A%0A  
@@ -1838,82 +1838,23 @@
 t = 
-new RestClient(settings());%0A        restClient.start();%0A        try %7B%0A    
+restClient();%0A%0A
     
@@ -1874,28 +1874,24 @@
 fo(%22send%22);%0A
-    
         byte
@@ -1965,20 +1965,16 @@
         
-  
-  
 List%3CFut
@@ -2032,28 +2032,24 @@
 x);%0A        
-    
 for (int i =
@@ -2080,20 +2080,16 @@
         
- 
-   
 DefaultF
@@ -2153,28 +2153,24 @@
             
-   
- 
 HttpVersion.
@@ -2191,28 +2191,24 @@
             
-    
 HttpMethod.P
@@ -2227,20 +2227,16 @@
         
-  
-  
 %22/%22 + qu
@@ -2259,20 +2259,16 @@
 ssage%22,%0A
-    
         
@@ -2322,20 +2322,16 @@
         
-    
 false%0A  
@@ -2344,19 +2344,11 @@
     
-    );%0A    
+);%0A
     
@@ -2436,20 +2436,16 @@
         
- 
-   
 futures.
@@ -2479,34 +2479,26 @@
 ));%0A        
-    %7D%0A    
+%7D%0A
         rest
@@ -2513,36 +2513,32 @@
 lush();%0A        
-  
-  
 logger.info(%22awa
@@ -2539,28 +2539,24 @@
 o(%22await%22);%0A
-    
         for 
@@ -2613,20 +2613,16 @@
         
- 
-   
 FullHttp
@@ -2659,36 +2659,32 @@
 ();%0A            
-   
- 
 assert response.
@@ -2731,36 +2731,32 @@
 ();%0A            
-    
 assert response.
@@ -2810,20 +2810,16 @@
         
-  
-  
 response
@@ -2836,28 +2836,24 @@
 .release();%0A
-    
         %7D%0A  
@@ -2854,28 +2854,24 @@
   %7D%0A        
-    
 logger.info(
@@ -2889,69 +2889,8 @@
 %22);%0A
-        %7D finally %7B%0A            restClient.stop();%0A        %7D%0A
     

optimize rest integration test