<commit_before>/*
 * Copyright (c) 2015. <NAME>, <NAME>
 *
 * 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.
 *
 * QBit - The Microservice lib for Java : JSON, WebSocket, REST. Be The Web!
 */

package io.advantageous.qbit.http.jetty.test;

import io.advantageous.boon.core.Str;
import io.advantageous.boon.core.Sys;
import io.advantageous.qbit.annotation.RequestMapping;
import io.advantageous.qbit.annotation.RequestMethod;
import io.advantageous.qbit.annotation.RequestParam;
import io.advantageous.qbit.client.Client;
import io.advantageous.qbit.client.ClientBuilder;
import io.advantageous.qbit.http.client.HttpClient;
import io.advantageous.qbit.http.client.HttpClientBuilder;
import io.advantageous.qbit.http.request.HttpRequest;
import io.advantageous.qbit.http.request.HttpRequestBuilder;
import io.advantageous.qbit.http.request.HttpTextReceiver;
import io.advantageous.qbit.reactive.Callback;
import io.advantageous.qbit.server.EndpointServerBuilder;
import io.advantageous.qbit.server.ServiceEndpointServer;
import io.advantageous.qbit.service.ServiceProxyUtils;
import io.advantageous.qbit.test.TimedTesting;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import static io.advantageous.boon.core.Exceptions.die;
import static io.advantageous.boon.core.IO.puts;


/**
 * @author rhightower on 2/14/15.
 * We are going to deprecate Jetty support.
 */
public class FullJettyIntegration extends TimedTesting {


    static volatile int port = 7777;
    private AtomicReference<String> returnValue;

    Client client;
    ServiceEndpointServer server;
    HttpClient httpClient;
    ClientServiceInterface clientProxy;
    AtomicInteger callCount;
    AtomicReference<String> pongValue;
    boolean ok;
    AtomicInteger returnCount;

    //@Test
    public void testWebSocket() throws Exception {


        clientProxy.ping(s -> {
            puts(s);
            pongValue.set(s);
        }, "hi");

        ServiceProxyUtils.flushServiceProxy(clientProxy);

        waitForTrigger(20, o -> this.pongValue.get() != null);


        final String pongValue = this.pongValue.get();
        ok = pongValue.equals("hi pong") || die();

    }

    //@Test
    public void testWebSocketFlushHappy() throws Exception {


        final Callback<String> callback = s -> {
            int count = returnCount.incrementAndGet();

            if (count % 2 == 0) {
                puts("return count", count);
            }

            puts("                     PONG");
            pongValue.set(s);
        };

        for (int index = 0; index < 11; index++) {

            clientProxy.ping(callback, "hi");

        }

        ServiceProxyUtils.flushServiceProxy(clientProxy);
        Sys.sleep(100);

        client.flush();
        Sys.sleep(100);


        waitForTrigger(20, o -> returnCount.get() >= callCount.get());

        puts("HERE                        ", callCount, returnCount);

        ok = returnCount.get() >= callCount.get() || die(returnCount, callCount); //TODO off by one error?


    }

    //@Test
    public void testWebSocketSend10() throws Exception {


        final Callback<String> callback = s -> {

            returnCount.incrementAndGet();

            puts("                     PONG");
            pongValue.set(s);
        };

        for (int index = 0; index < 10; index++) {

            clientProxy.ping(callback, "hi");

        }

        ServiceProxyUtils.flushServiceProxy(clientProxy);
        Sys.sleep(100);


        client.flush();
        Sys.sleep(100);


        waitForTrigger(20, o -> returnCount.get() == callCount.get());

        puts("HERE                        ", callCount, returnCount);

        //TODO FAILS ON TRAVIS ONLY
        //ok = returnCount.get() == callCount.get() || die(returnCount, callCount);


    }

   // @Test
    public void testRestCallSimple() throws Exception {

        final HttpRequest request = new HttpRequestBuilder()
                .setUri("/services/mockservice/ping")
                .setJsonBodyForPost("\"hello\"")
                .setTextReceiver(new HttpTextReceiver() {
                    @Override
                    public void response(int code, String mimeType, String body) {
                        if (code == 200) {
                            pongValue.set(body);
                        } else {
                            pongValue.set("ERROR " + body + " code " + code);
                            throw new RuntimeException("ERROR " + code + " " + body);

                        }
                    }
                })
                .build();

        httpClient.sendHttpRequest(request);

        httpClient.flush();

        waitForTrigger(20, o -> this.pongValue.get() != null);

<commit_msg>consul is timing out becuase the default timeout for http client was 3,000. Changed to 120_000.<commit_after>/*
 * Copyright (c) 2015. <NAME>, <NAME>
 *
 * 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.
 *
 * QBit - The Microservice lib for Java : JSON, WebSocket, REST. Be The Web!
 */

package io.advantageous.qbit.http.jetty.test;

import io.advantageous.boon.core.Str;
import io.advantageous.boon.core.Sys;
import io.advantageous.qbit.annotation.RequestMapping;
import io.advantageous.qbit.annotation.RequestMethod;
import io.advantageous.qbit.annotation.RequestParam;
import io.advantageous.qbit.client.Client;
import io.advantageous.qbit.client.ClientBuilder;
import io.advantageous.qbit.http.client.HttpClient;
import io.advantageous.qbit.http.client.HttpClientBuilder;
import io.advantageous.qbit.http.request.HttpRequest;
import io.advantageous.qbit.http.request.HttpRequestBuilder;
import io.advantageous.qbit.http.request.HttpTextReceiver;
import io.advantageous.qbit.reactive.Callback;
import io.advantageous.qbit.server.EndpointServerBuilder;
import io.advantageous.qbit.server.ServiceEndpointServer;
import io.advantageous.qbit.service.ServiceProxyUtils;
import io.advantageous.qbit.test.TimedTesting;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import static io.advantageous.boon.core.Exceptions.die;
import static io.advantageous.boon.core.IO.puts;


/**
 * @author rhightower on 2/14/15.
 * We are going to deprecate Jetty support.
 */
public class FullJettyIntegration extends TimedTesting {


    static volatile int port = 7777;
    private AtomicReference<String> returnValue;

    Client client;
    ServiceEndpointServer server;
    HttpClient httpClient;
    ClientServiceInterface clientProxy;
    AtomicInteger callCount;
    AtomicReference<String> pongValue;
    boolean ok;
    AtomicInteger returnCount;

    //@Test
    public void testWebSocket() throws Exception {


        clientProxy.ping(s -> {
            puts(s);
            pongValue.set(s);
        }, "hi");

        ServiceProxyUtils.flushServiceProxy(clientProxy);

        waitForTrigger(20, o -> this.pongValue.get() != null);


        final String pongValue = this.pongValue.get();
        ok = pongValue.equals("hi pong") || die();

    }

    //@Test
    public void testWebSocketFlushHappy() throws Exception {


        final Callback<String> callback = s -> {
            int count = returnCount.incrementAndGet();

            if (count % 2 == 0) {
                puts("return count", count);
            }

            puts("                     PONG");
            pongValue.set(s);
        };

        for (int index = 0; index < 11; index++) {

            clientProxy.ping(callback, "hi");

        }

        ServiceProxyUtils.flushServiceProxy(clientProxy);
        Sys.sleep(100);

        client.flush();
        Sys.sleep(100);


        waitForTrigger(20, o -> returnCount.get() >= callCount.get());

        puts("HERE                        ", callCount, returnCount);

        ok = returnCount.get() >= callCount.get() || die(returnCount, callCount); //TODO off by one error?


    }

    //@Test
    public void testWebSocketSend10() throws Exception {


        final Callback<String> callback = s -> {

            returnCount.incrementAndGet();

            puts("                     PONG");
            pongValue.set(s);
        };

        for (int index = 0; index < 10; index++) {

            clientProxy.ping(callback, "hi");

        }

        ServiceProxyUtils.flushServiceProxy(clientProxy);
        Sys.sleep(100);


        client.flush();
        Sys.sleep(100);


        waitForTrigger(20, o -> returnCount.get() == callCount.get());

        puts("HERE                        ", callCount, returnCount);

        //TODO FAILS ON TRAVIS ONLY
        //ok = returnCount.get() == callCount.get() || die(returnCount, callCount);


    }

   // @Test//
    public void testRestCallSimple() throws Exception {

        final HttpRequest request = new HttpRequestBuilder()
                .setUri("/services/mockservice/ping")
                .setJsonBodyForPost("\"hello\"")
                .setTextReceiver(new HttpTextReceiver() {
                    @Override
                    public void response(int code, String mimeType, String body) {
                        if (code == 200) {
                            pongValue.set(body);
                        } else {
                            pongValue.set("ERROR " + body + " code " + code);
                            throw new RuntimeException("ERROR " + code + " " + body);

                        }
                    }
                })
                .build();

        httpClient.sendHttpRequest(request);

        httpClient.flush();

        waitForTrigger(20, o -> this.pongValue.get() != null);


{'fraction_non_alphanumeric': 0.10883660615042609, 'fraction_numerical': 0.01018895887365691, 'mean_word_length': 2.334465719579988, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6126940', 'n_tokens_mistral': 3174, 'n_tokens_neox': 2952, 'n_words': 721}