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.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(() -> {
            try {
                send(max());
            } catch (Exception e) {
                assert false : e;
            }
        });
        long end = System.currentTimeMillis();
        printQps("send", total(), start, end);
    }

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

    private void send(int max) throws Exception {
        RestClient restClient = new RestClient(settings());
        restClient.start();
        try {
            while (true) {
                logger.info("send");
                byte[] bytes = "{\"message\":\"hello world\"}".getBytes();
                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)
                    );
                    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();
        }
    }
}

@@ -779,10 +779,8 @@
 ();%0A
-//
     
@@ -785,32 +785,32 @@
       warmUp();%0A
+
         long sta
@@ -1351,39 +1351,8 @@
 y %7B%0A
-            while (true) %7B%0A    
     
@@ -1392,20 +1392,16 @@
         
-  
-  
 byte%5B%5D b
@@ -1447,28 +1447,24 @@
 getBytes();%0A
-    
             
@@ -1534,28 +1534,24 @@
             
-  
-  
 for (int i =
@@ -1566,28 +1566,24 @@
 max; i++) %7B%0A
-    
             
@@ -1663,28 +1663,24 @@
             
-  
-  
 HttpVersion.
@@ -1705,28 +1705,24 @@
             
-    
 HttpMethod.P
@@ -1745,20 +1745,16 @@
         
-  
-  
 %22/%22 + qu
@@ -1797,20 +1797,16 @@
         
-    
 Unpooled
@@ -1847,19 +1847,11 @@
     
-  
-  
 );%0A
-    
     
@@ -1947,20 +1947,16 @@
         
-    
 futures.
@@ -1994,32 +1994,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -2004,34 +2004,32 @@
   %7D%0A            
-  
 restClient.flush
@@ -2028,28 +2028,24 @@
 nt.flush();%0A
-    
             
@@ -2074,28 +2074,24 @@
             
-  
-  
 for (Future%3C
@@ -2144,20 +2144,16 @@
         
-    
 FullHttp
@@ -2194,36 +2194,32 @@
                 
-  
-  
 assert response.
@@ -2270,36 +2270,32 @@
                 
-    
 assert response.
@@ -2353,20 +2353,16 @@
         
-  
-  
 response
@@ -2391,32 +2391,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -2401,34 +2401,32 @@
   %7D%0A            
-  
 logger.info(%22awa
@@ -2436,30 +2436,16 @@
 done%22);%0A
-            %7D%0A
         

fix rest integration test