<commit_before>/*
 * Copyright 2016 Realm Inc.
 *
 * 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.realm.objectserver.utils;

import java.io.IOException;

import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Start and Stop the node server responsible of creating a
 * temp directory & start a sync server on it for each unit test.
 */
public class HttpUtils {
    private final static OkHttpClient client = new OkHttpClient();
    // adb reverse tcp:8888 tcp:8888
    // will forward this query to the host, running the integration test server on 8888
    private final static String START_SERVER = "http://127.0.0.1:8888/start";
    private final static String STOP_SERVER = "http://127.0.0.1:8888/stop";

    public static void startSyncServer() throws Exception {
        Request request = new Request.Builder()
                .url(START_SERVER)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        Headers responseHeaders = response.headers();
        for (int i = 0; i < responseHeaders.size(); i++) {
            System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
        }

        System.out.println(response.body().string());

        // FIXME: Server ready checking should be done in the control server side!
        if (!waitAuthServerReady()) {
            stopSyncServer();
            throw new RuntimeException("Auth server cannot be started.");
        }
    }

    // Checking the server
    private static boolean waitAuthServerReady() throws InterruptedException {
        int retryTimes = 20;
        Request request = new Request.Builder()
                .url(Constants.AUTH_SERVER_URL)
                .build();

        while (retryTimes != 0) {
            try {
                Response response = client.newCall(request).execute();
                if (response.isSuccessful()) {
                    return true;
                }
            } catch (IOException e) {
                e.printStackTrace();
                Thread.sleep(50);
            }
            retryTimes--;
        }

        return false;
    }

    public static void stopSyncServer() throws Exception {
        Request request = new Request.Builder()
                .url(STOP_SERVER)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        Headers responseHeaders = response.headers();
        for (int i = 0; i < responseHeaders.size(); i++) {
            System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
        }

        System.out.println(response.body().string());
    }
}<commit_msg>Waiting longer time for checking auth server (#3913)<commit_after>/*
 * Copyright 2016 Realm Inc.
 *
 * 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.realm.objectserver.utils;

import java.io.IOException;

import io.realm.log.RealmLog;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Start and Stop the node server responsible of creating a
 * temp directory & start a sync server on it for each unit test.
 */
public class HttpUtils {
    private final static OkHttpClient client = new OkHttpClient();
    // adb reverse tcp:8888 tcp:8888
    // will forward this query to the host, running the integration test server on 8888
    private final static String START_SERVER = "http://127.0.0.1:8888/start";
    private final static String STOP_SERVER = "http://127.0.0.1:8888/stop";

    public static void startSyncServer() throws Exception {
        Request request = new Request.Builder()
                .url(START_SERVER)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        Headers responseHeaders = response.headers();
        for (int i = 0; i < responseHeaders.size(); i++) {
            RealmLog.debug(responseHeaders.name(i) + ": " + responseHeaders.value(i));
        }

        RealmLog.debug(response.body().string());

        // FIXME: Server ready checking should be done in the control server side!
        if (!waitAuthServerReady()) {
            stopSyncServer();
            throw new RuntimeException("Auth server cannot be started.");
        }
    }

    // Checking the server
    private static boolean waitAuthServerReady() throws InterruptedException {
        int retryTimes = 50;
        Request request = new Request.Builder()
                .url(Constants.AUTH_SERVER_URL)
                .build();

        while (retryTimes != 0) {
            try {
                Response response = client.newCall(request).execute();
                if (response.isSuccessful()) {
                    return true;
                }
                RealmLog.error("Error response from auth server: %s", response.toString());
            } catch (IOException e) {
                RealmLog.error(e);
                Thread.sleep(100);
            }
            retryTimes--;
        }

        return false;
    }

    public static void stopSyncServer() throws Exception {
        Request request = new Request.Builder()
                .url(STOP_SERVER)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        Headers responseHeaders = response.headers();
        for (int i = 0; i < responseHeaders.size(); i++) {
            RealmLog.debug(responseHeaders.name(i) + ": " + responseHeaders.value(i));
        }

        RealmLog.debug(response.body().string());
    }
}
{'fraction_non_alphanumeric': 0.10181451612903226, 'fraction_numerical': 0.015408986175115207, 'mean_word_length': 2.4178149606299213, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, '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': '7325667', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1721, 'n_words': 590}