<commit_before>#include "Test.h"
#include "Application/Client/SDBPClient.h"
#include "Application/Client/SDBPClientWrapper.h"
#include "System/Common.h"
#include "System/Config.h"
#include "System/Threading/ThreadPool.h"
#include "System/Threading/Atomic.h"
#include "System/Events/Deferred.h"

using namespace SDBPClient;

#ifdef TEST
#undef TEST
#endif
#define TEST(x) if (x != SDBP_SUCCESS) TEST_CLIENT_FAIL();

#define TEST_CLIENT_FAIL()                                                      \
    {                                                                           \
        PRINT_CLIENT_STATUS("Transport", client.GetTransportStatus());          \
        PRINT_CLIENT_STATUS("Connectivity", client.GetConnectivityStatus());    \
        PRINT_CLIENT_STATUS("Timeout", client.GetTimeoutStatus());              \
        PRINT_CLIENT_STATUS("Command", client.GetCommandStatus());              \
        if (stopOnTestFailure)                                                  \
            TEST_FAIL();                                                        \
        else                                                                    \
            AtomicIncrement32(failCount);                                       \
    }

#define PRINT_CLIENT_STATUS(which, status) \
    switch (status) \
    { \
    case SDBP_SUCCESS: TEST_LOG("%s status: SDBP_SUCCESS", which); break; \
    case SDBP_API_ERROR: TEST_LOG("%s status: SDBP_API_ERROR", which); break; \
    case SDBP_PARTIAL: TEST_LOG("%s status: SDBP_PARTIAL", which); break; \
    case SDBP_FAILURE: TEST_LOG("%s status: SDBP_FAILURE", which); break; \
    case SDBP_NOMASTER: TEST_LOG("%s status: SDBP_NOMASTER", which); break; \
    case SDBP_NOCONNECTION: TEST_LOG("%s status: SDBP_NOCONNECTION", which); break; \
    case SDBP_NOPRIMARY: TEST_LOG("%s status: SDBP_NOPRIMARY", which); break; \
    case SDBP_MASTER_TIMEOUT: TEST_LOG("%s status: SDBP_MASTER_TIMEOUT", which); break; \
    case SDBP_GLOBAL_TIMEOUT: TEST_LOG("%s status: SDBP_GLOBAL_TIMEOUT", which); break; \
    case SDBP_PRIMARY_TIMEOUT: TEST_LOG("%s status: SDBP_PRIMARY_TIMEOUT", which); break; \
    case SDBP_NOSERVICE: TEST_LOG("%s status: SDBP_NOSERVICE", which); break; \
    case SDBP_FAILED: TEST_LOG("%s status: SDBP_FAILED", which); break; \
    case SDBP_BADSCHEMA: TEST_LOG("%s status: SDBP_BADSCHEMA", which); break; \
    }

static void TestShutdown();

// module global variables
static uint64_t     defaultTableID;
static uint64_t     defaultDatabaseID;
static bool         stopOnTestFailure = true;
static uint32_t     failCount;
static bool         configRead = false;
static Mutex        configMutex;
static Deferred     onShutdown = CFunc(TestShutdown);

static void TestShutdown()
{
    MutexGuard      guard(configMutex);

    if (configRead == false)
        return;

    configFile.Shutdown();
    configRead = false;
}

static int SetupDefaultControllerNodes(Client& client)
{
    uint64_t        nodeID;
    unsigned        numControllers;
    int             ret;
    const char**    nodes;

    // XXX: Warning! Don't use localhost on Windows, because if IPv6 is installed
    // resolution of localhost is at least 1 second. Instead use 127.0.0.1
    const char*     defaultNodes[] = {"127.0.0.1:7080"};
 
    // connect to the controller nodes
    numControllers = (unsigned) configFile.GetListNum("controllers");
    if (numControllers == 0)
    {
        // no controllers given in config, use default
        return client.Init(SIZE(defaultNodes), defaultNodes);
    }
    
    
    nodes = new const char*[numControllers];
    for (nodeID = 0; nodeID < numControllers; nodeID++)
    {
        nodes[nodeID] = configFile.GetListValue("controllers", (int) nodeID, "");
    }

    ret = client.Init(numControllers, nodes);
    delete[] nodes;
    return ret;
}

static void ReadConfig()
{    
    MutexGuard      guard(configMutex);

    if (configRead)
        return;

    configFile.Shutdown();
    configFile.Init("test/client/client.conf");
    configRead = true;
}

static int SetupDefaultClient(Client& client)
{
    std::string     databaseName = "test";
    std::string     tableName = "test";
    uint64_t        databaseID;
    uint64_t        tableID;
    int             ret;
    ClientObj       clientObj;

    ReadConfig();

    Log_SetTrace(configFile.GetBoolValue("log.trace", false));

    client.Shutdown();
    ret = SetupDefaultControllerNodes(client);
    if (ret != SDBP_SUCCESS)
        TEST_CLIENT_FAIL();

    databaseName = configFile.GetValue("databaseName", "test");
    tableName = configFile.GetValue("tableName", "test");

    client.SetMasterTimeout((uint64_t)configFile.GetInt64Value("masterTimeout", 10*1000));
    client.SetGlobalTimeout((uint64_t)configFile.GetInt64Value("globalTimeout", 30*1000));
    //client.SetConsistencyMode(SDBP_CONSISTENCY_RYW);

    clientObj = (ClientObj) &client;
    databaseID = 0;
    for (unsigned i = 0; i < SDBP_GetNumDatabases(clientObj); i++)
    {
        if (SDBP_GetDatabaseNameAt(clientObj, i) == databaseName)
        {
            databaseID = SDBP_GetDatabaseIDAt(clientObj, i);
            break;
        }
    }<commit_msg>Added extra configuration option to native client test.<commit_after>#include "Test.h"
#include "Application/Client/SDBPClient.h"
#include "Application/Client/SDBPClientWrapper.h"
#include "System/Common.h"
#include "System/Config.h"
#include "System/Threading/ThreadPool.h"
#include "System/Threading/Atomic.h"
#include "System/Events/Deferred.h"

using namespace SDBPClient;

#ifdef TEST
#undef TEST
#endif
#define TEST(x) if (x != SDBP_SUCCESS) TEST_CLIENT_FAIL();

#define TEST_CLIENT_FAIL()                                                      \
    {                                                                           \
        PRINT_CLIENT_STATUS("Transport", client.GetTransportStatus());          \
        PRINT_CLIENT_STATUS("Connectivity", client.GetConnectivityStatus());    \
        PRINT_CLIENT_STATUS("Timeout", client.GetTimeoutStatus());              \
        PRINT_CLIENT_STATUS("Command", client.GetCommandStatus());              \
        if (stopOnTestFailure)                                                  \
            TEST_FAIL();                                                        \
        else                                                                    \
            AtomicIncrement32(failCount);                                       \
    }

#define PRINT_CLIENT_STATUS(which, status) \
    switch (status) \
    { \
    case SDBP_SUCCESS: TEST_LOG("%s status: SDBP_SUCCESS", which); break; \
    case SDBP_API_ERROR: TEST_LOG("%s status: SDBP_API_ERROR", which); break; \
    case SDBP_PARTIAL: TEST_LOG("%s status: SDBP_PARTIAL", which); break; \
    case SDBP_FAILURE: TEST_LOG("%s status: SDBP_FAILURE", which); break; \
    case SDBP_NOMASTER: TEST_LOG("%s status: SDBP_NOMASTER", which); break; \
    case SDBP_NOCONNECTION: TEST_LOG("%s status: SDBP_NOCONNECTION", which); break; \
    case SDBP_NOPRIMARY: TEST_LOG("%s status: SDBP_NOPRIMARY", which); break; \
    case SDBP_MASTER_TIMEOUT: TEST_LOG("%s status: SDBP_MASTER_TIMEOUT", which); break; \
    case SDBP_GLOBAL_TIMEOUT: TEST_LOG("%s status: SDBP_GLOBAL_TIMEOUT", which); break; \
    case SDBP_PRIMARY_TIMEOUT: TEST_LOG("%s status: SDBP_PRIMARY_TIMEOUT", which); break; \
    case SDBP_NOSERVICE: TEST_LOG("%s status: SDBP_NOSERVICE", which); break; \
    case SDBP_FAILED: TEST_LOG("%s status: SDBP_FAILED", which); break; \
    case SDBP_BADSCHEMA: TEST_LOG("%s status: SDBP_BADSCHEMA", which); break; \
    }

static void TestShutdown();

// module global variables
static uint64_t     defaultTableID;
static uint64_t     defaultDatabaseID;
static bool         stopOnTestFailure = true;
static uint32_t     failCount;
static bool         configRead = false;
static Mutex        configMutex;
static Deferred     onShutdown = CFunc(TestShutdown);

static void TestShutdown()
{
    MutexGuard      guard(configMutex);

    if (configRead == false)
        return;

    configFile.Shutdown();
    configRead = false;
}

static int SetupDefaultControllerNodes(Client& client)
{
    uint64_t        nodeID;
    unsigned        numControllers;
    int             ret;
    const char**    nodes;

    // XXX: Warning! Don't use localhost on Windows, because if IPv6 is installed
    // resolution of localhost is at least 1 second. Instead use 127.0.0.1
    const char*     defaultNodes[] = {"127.0.0.1:7080"};
 
    // connect to the controller nodes
    numControllers = (unsigned) configFile.GetListNum("controllers");
    if (numControllers == 0)
    {
        // no controllers given in config, use default
        return client.Init(SIZE(defaultNodes), defaultNodes);
    }
    
    
    nodes = new const char*[numControllers];
    for (nodeID = 0; nodeID < numControllers; nodeID++)
    {
        nodes[nodeID] = configFile.GetListValue("controllers", (int) nodeID, "");
    }

    ret = client.Init(numControllers, nodes);
    delete[] nodes;
    return ret;
}

static void ReadConfig()
{    
    MutexGuard      guard(configMutex);

    if (configRead)
        return;

    configFile.Shutdown();
    configFile.Init("test/client/client.conf");
    configRead = true;
}

static int SetupDefaultClient(Client& client)
{
    std::string     databaseName = "test";
    std::string     tableName = "test";
    uint64_t        databaseID;
    uint64_t        tableID;
    int             ret;
    ClientObj       clientObj;

    ReadConfig();

    Log_SetTrace(configFile.GetBoolValue("log.trace", false));
    Log_SetDebug(configFile.GetBoolValue("log.debug", false));

    client.Shutdown();
    ret = SetupDefaultControllerNodes(client);
    if (ret != SDBP_SUCCESS)
        TEST_CLIENT_FAIL();

    databaseName = configFile.GetValue("databaseName", "test");
    tableName = configFile.GetValue("tableName", "test");

    client.SetMasterTimeout((uint64_t)configFile.GetInt64Value("masterTimeout", 10*1000));
    client.SetGlobalTimeout((uint64_t)configFile.GetInt64Value("globalTimeout", 30*1000));
    //client.SetConsistencyMode(SDBP_CONSISTENCY_RYW);

    clientObj = (ClientObj) &client;
    databaseID = 0;
    for (unsigned i = 0; i < SDBP_GetNumDatabases(clientObj); i++)
    {
        if (SDBP_GetDatabaseNameAt(clientObj, i) == databaseName)
        {
            databaseID = SDBP_GetDatabaseIDAt(clientObj, i);
            break;
        }
    }
{'fraction_non_alphanumeric': 0.11017677974199713, 'fraction_numerical': 0.010702341137123745, 'mean_word_length': 2.5346166835528536, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5967768', 'n_tokens_mistral': 3222, 'n_tokens_neox': 3094, 'n_words': 681}