int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    Test->set_timeout(30);

    Test->tprintf("Connecting to RWSplit\n");
    Test->conn_rwsplit = open_conn_no_db(Test->rwsplit_port, Test->maxscale_IP, Test->maxscale_user,
                                         Test->maxscale_password, Test->ssl);
    if (Test->conn_rwsplit == NULL)
    {
        Test->add_result(1, "Error connecting to MaxScale\n");
        delete Test;
        return 1;
    }
    Test->tprintf("Removing 'test' DB\n");
    execute_query(Test->conn_rwsplit, (char *) "DROP DATABASE IF EXISTS test;");
    Test->tprintf("Closing connections and waiting 5 seconds\n");
    Test->close_rwsplit();
    sleep(5);

    Test->tprintf("Connection to non-existing DB (all routers)\n");
    Test->connect_maxscale();
    Test->close_maxscale_connections();

    Test->tprintf("Connecting to RWSplit again to recreate 'test' db\n");
    Test->conn_rwsplit = open_conn_no_db(Test->rwsplit_port, Test->maxscale_IP, Test->maxscale_user,
                                         Test->maxscale_password, Test->ssl);
    if (Test->conn_rwsplit == NULL)
    {
        printf("Error connecting to MaxScale\n");
        delete Test;
        return 1;
    }

    Test->tprintf("Creating and selecting 'test' DB\n");
    Test->try_query(Test->conn_rwsplit, (char *) "CREATE DATABASE test; USE test");
    Test->tprintf("Creating 't1' table\n");
    Test->add_result(create_t1(Test->conn_rwsplit), "Error creation 't1'\n");
    Test->close_rwsplit();

    Test->tprintf("Reconnectiong\n");
    Test->add_result(Test->connect_maxscale(), "error connection to Maxscale\n");
    Test->tprintf("Trying simple operations with t1 \n");
    Test->try_query(Test->conn_rwsplit, (char *) "INSERT INTO t1 (x1, fl) VALUES(0, 1);");
    Test->set_timeout(240);
    Test->add_result(execute_select_query_and_check(Test->conn_rwsplit, (char *) "SELECT * FROM t1;", 1),
                     "Error execution SELECT * FROM t1;\n");

    Test->close_maxscale_connections();

    int rval = Test->global_result;
    delete Test;
    return rval;
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;
    int i;

    Test->read_env();
    Test->print_env();

    printf("Connecting to all MaxScale services\n");
    fflush(stdout);
    global_result += Test->connect_maxscale();

    printf("executing show status 1000 times\n");
    fflush(stdout);


    for (i = 0; i < 1000; i++)  {
        global_result += execute_query(Test->conn_rwsplit, (char *) "show status");
    }
    for (i = 0; i < 1000; i++)  {
        global_result += execute_query(Test->conn_slave, (char *) "show status");
    }
    for (i = 0; i < 1000; i++)  {
        global_result += execute_query(Test->conn_master, (char *) "show status");
    }

    Test->close_maxscale_connections();

    check_maxscale_alive();

    Test->copy_all_logs();
    return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;

    Test->read_env();
    Test->print_env();

    Test->connect_maxscale();

    printf("Trying SELECT @a:=@a+1 as a, test.b FROM test\n"); fflush(stdout);
    global_result += execute_query(Test->conn_rwsplit, "DROP TABLE IF EXISTS test; CREATE TABLE test (b integer);");
    for (int i=0; i<10000;i++) {execute_query(Test->conn_rwsplit, "insert into test value(2);");}
    if (execute_query(Test->conn_rwsplit, "SELECT @a:=@a+1 as a, test.b FROM test;") == 0) {
        printf("Query succeded, but expected to fail. Test FAILED!\n"); fflush(stdout);
        global_result++;
    }
    printf("Trying USE test\n"); fflush(stdout);
    global_result += execute_query(Test->conn_rwsplit, "USE test");

    global_result += execute_query(Test->conn_rwsplit, "DROP TABLE IF EXISTS test;");

    printf("Checking if MaxScale alive\n"); fflush(stdout);
    Test->close_maxscale_connections();

    printf("Checking logs\n"); fflush(stdout);
    global_result    += check_log_err((char *) "Warning : The query can't be routed to all backend servers because it includes SELECT and SQL variable modifications which is not supported", TRUE);
    global_result    += check_log_err((char *) "SELECT with session data modification is not supported if configuration parameter use_sql_variables_in=all", TRUE);

    global_result += check_maxscale_alive();

    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    char sql[10240];

    Test->connect_maxscale();
    create_t1(Test->conn_rwsplit);

    Test->tprintf("INSERTing data\n");
    for (int i = 0; i < 2000; i++)
    {
        Test->set_timeout(20);
        create_insert_string(sql, 100, i);
        Test->try_query(Test->conn_rwsplit, sql);
    }
    Test->tprintf("done, sleeping\n");
    Test->stop_timeout();
    sleep(20);
    Test->tprintf("Trying SELECT\n");
    Test->set_timeout(30);
    Test->try_query(Test->conn_rwsplit, (char *) "SELECT * FROM t1");

    Test->check_maxscale_alive();
    int rval = Test->global_result;
    delete Test;
    return rval;
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    Test->set_timeout(10);

    Test->connect_maxscale();

    Test->set_timeout(10);
    Test->tprintf("Trying USE db against RWSplit\n");
    Test->try_query(Test->conn_rwsplit, (char *) "USE mysql");
    Test->try_query(Test->conn_rwsplit, (char *) "USE test");
    Test->set_timeout(10);
    Test->tprintf("Trying USE db against ReadConn master\n");
    Test->try_query(Test->conn_master, (char *) "USE mysql");
    Test->try_query(Test->conn_master, (char *) "USE test");
    Test->set_timeout(10);
    Test->tprintf("Trying USE db against ReadConn slave\n");
    Test->try_query(Test->conn_master, (char *) "USE mysql");
    Test->try_query(Test->conn_slave, (char *) "USE test");

    Test->set_timeout(10);
    Test->close_maxscale_connections();

    Test->check_maxscale_alive();
    int rval = Test->global_result;
    delete Test;
    return rval;
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;
    int i;
    int N=4;

    Test->read_env();
    Test->print_env();


    for (i = 0; i < 4; i++) {
        Test->repl->connect();
        if (Test->connect_maxscale() !=0 ) {
            printf("Error connecting to MaxScale\n");
            exit(1);
        }

        global_result += insert_select(Test, N);

        printf("Creating database test1\n"); fflush(stdout);
        global_result += execute_query(Test->conn_rwsplit, "DROP TABLE t1");
        global_result += execute_query(Test->conn_rwsplit, "DROP DATABASE IF EXISTS test1;");
        global_result += execute_query(Test->conn_rwsplit, "CREATE DATABASE test1;");
        sleep(5);

        printf("Testing with database 'test1'\n");fflush(stdout);
        global_result += use_db(Test, (char *) "test1");
        global_result += insert_select(Test, N);

        global_result += check_t1_table(Test, FALSE, (char *) "test");
        global_result += check_t1_table(Test, TRUE, (char *) "test1");



        printf("Trying queries with syntax errors\n");fflush(stdout);
        execute_query(Test->conn_rwsplit, "DROP DATABASE I EXISTS test1;");
        execute_query(Test->conn_rwsplit, "CREATE TABLE ");

        execute_query(Test->conn_master, "DROP DATABASE I EXISTS test1;");
        execute_query(Test->conn_master, "CREATE TABLE ");

        execute_query(Test->conn_slave, "DROP DATABASE I EXISTS test1;");
        execute_query(Test->conn_slave, "CREATE TABLE ");

        // close connections
        Test->close_maxscale_connections();
        Test->repl->close_connections();

    }

    global_result += check_maxscale_alive();

    if (global_result == 0) {printf("PASSED!!\n");} else {printf("FAILED!!\n");}
    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    Test->set_timeout(30);
    MYSQL * conn_found_rows;
    my_ulonglong rows;


    Test->repl->connect();
    Test->connect_maxscale();

    conn_found_rows = open_conn_db_flags(Test->rwsplit_port, Test->maxscale_IP, (char *) "test",
                                         Test->maxscale_user, Test->maxscale_password, CLIENT_FOUND_ROWS, Test->ssl);

    Test->set_timeout(30);
    execute_query(Test->conn_rwsplit, "DROP TABLE IF EXISTS t1");
    execute_query(Test->conn_rwsplit, "CREATE TABLE t1(id INT PRIMARY KEY, val INT, msg VARCHAR(100))");
    execute_query(Test->conn_rwsplit,
                  "INSERT INTO t1 VALUES (1, 1, 'foo'), (2, 1, 'bar'), (3, 2, 'baz'), (4, 2, 'abc')");

    Test->set_timeout(30);
    execute_query_affected_rows(Test->conn_rwsplit, "UPDATE t1 SET msg='xyz' WHERE val=2", &rows);
    Test->tprintf("update #1: %ld (expeced value is 2)\n", (long) rows);
    if (rows != 2)
    {
        Test->add_result(1, "Affected rows is not 2\n");
    }

    Test->set_timeout(30);
    execute_query_affected_rows(Test->conn_rwsplit, "UPDATE t1 SET msg='xyz' WHERE val=2", &rows);
    Test->tprintf("update #2: %ld  (expeced value is 0)\n", (long) rows);
    if (rows != 0)
    {
        Test->add_result(1, "Affected rows is not 0\n");
    }

    Test->set_timeout(30);
    execute_query_affected_rows(conn_found_rows, "UPDATE t1 SET msg='xyz' WHERE val=2", &rows);
    Test->tprintf("update #3: %ld  (expeced value is 2)\n", (long) rows);
    if (rows != 2)
    {
        Test->add_result(1, "Affected rows is not 2\n");
    }

    Test->close_maxscale_connections();

    int rval = Test->global_result;
    delete Test;
    return rval;

}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    Test->set_timeout(10);
    int i;

    Test->repl->connect();
    Test->connect_maxscale();

    Test->tprintf("Trying SHOW VARIABLES to different Maxscale services\n");
    fflush(stdout);
    Test->tprintf("RWSplit\n");
    for (i = 0; i < 100; i++)
    {
        Test->set_timeout(5);
        Test->try_query(Test->conn_rwsplit, (char *) "SHOW VARIABLES;");
    }
    Test->tprintf("ReadConn master\n");
    for (i = 0; i < 100; i++)
    {
        Test->set_timeout(5);
        Test->try_query(Test->conn_master, (char *) "SHOW VARIABLES;");
    }
    Test->tprintf("ReadConn slave\n");
    for (i = 0; i < 100; i++)
    {
        Test->set_timeout(5);
        Test->try_query(Test->conn_slave, (char *) "SHOW VARIABLES;");
    }

    Test->tprintf("All in one loop\n");
    for (i = 0; i < 100; i++)
    {
        Test->set_timeout(5);
        Test->try_query(Test->conn_rwsplit, (char *) "SHOW VARIABLES;");
        Test->try_query(Test->conn_master, (char *) "SHOW VARIABLES;");
        Test->try_query(Test->conn_slave, (char *) "SHOW VARIABLES;");
    }

    Test->set_timeout(10);
    Test->close_maxscale_connections();
    Test->repl->close_connections();

    Test->check_maxscale_alive();

    int rval = Test->global_result;
    delete Test;
    return rval;
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;

    Test->print_env();

    Test->connect_maxscale();

    printf("RWSplit: \n"); fflush(stdout);
    global_result = execute_query(Test->conn_rwsplit, (char *) "SET OPTION SQL_QUOTE_SHOW_CREATE = 1;");
    printf("ReadConn master: \n"); fflush(stdout);
    global_result = execute_query(Test->conn_master, (char *) "SET OPTION SQL_QUOTE_SHOW_CREATE = 1;");
    printf("readConn slave: \n"); fflush(stdout);
    global_result = execute_query(Test->conn_slave, (char *) "SET OPTION SQL_QUOTE_SHOW_CREATE = 1;");

    Test->close_maxscale_connections();

    global_result += check_maxscale_alive();

    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{

    TestConnections * Test = new TestConnections(argc, argv);
    Test->set_timeout(20);

    Test->connect_maxscale();

    Test->tprintf("Trying query to ReadConn master\n");
    fflush(stdout);
    Test->try_query(Test->conn_master, "show processlist;");
    Test->tprintf("Trying query to ReadConn slave\n");
    Test->try_query(Test->conn_slave, "show processlist;");

    Test->close_maxscale_connections();

    Test->check_log_err((char *) "Creating client session for Tee filter failed. Terminating session.", true);
    Test->check_log_err((char *) "Failed to create filter 'DuplicaFilter' for service 'RW_Router'", true);

    int rval = Test->global_result;
    delete Test;
    return rval;
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;
    int i;

    Test->read_env();
    Test->print_env();

    Test->connect_maxscale();

    printf("Trying \n");  fflush(stdout);

    char serverid1[1024];
    char serverid2[1024];

    if ( (
             find_field(
                 Test->conn_rwsplit, sel3,
                 "@@server_id", &serverid1[0])
             != 0 ) || (
             find_field(
                 Test->conn_rwsplit, sel4,
                 "@@server_id", &serverid2[0])
             != 0 )) {
        printf("@@server_id field not found!!\n");
        exit(1);
    } else {
        printf("'%s' to RWSplit gave @@server_id %s\n", sel3, serverid1);
        printf("'%s' directly to master gave @@server_id %s\n", sel4, serverid2);
        if (strcmp(serverid1, serverid2) !=0 ) {
            global_result++;
            printf("server_id are different depending in which order terms are in SELECT\n");
        }
    }

    if ( (
             find_field(
                 Test->conn_rwsplit, sel1,
                 "@@hostname", &serverid1[0])
             != 0 ) || (
             find_field(
                 Test->conn_rwsplit, sel2,
                 "@@hostname", &serverid2[0])
             != 0 )) {
        printf("@@hostname field not found!!\n");
        exit(1);
    } else {
        printf("'%s' to RWSplit gave @@hostname %s\n", sel1, serverid1);
        printf("'%s' to RWSplit gave @@hostname %s\n", sel2, serverid2);
        if (strcmp(serverid1, serverid2) !=0 ) {
            global_result++;
            printf("hostname are different depending in which order terms are in SELECT\n");
        }
    }

    Test->close_maxscale_connections();

    global_result += check_maxscale_alive();

    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;
    int N=4;
    char str[1024];

    Test->read_env();
    Test->print_env();

    Test->connect_maxscale();
    Test->repl->connect();

    printf("Create t1\n"); fflush(stdout);
    create_t1(Test->conn_rwsplit);
    printf("Insert data into t1\n"); fflush(stdout);
    insert_into_t1(Test->conn_rwsplit, N);
    printf("Sleeping to let replication happen\n");fflush(stdout);
    sleep(30);

    printf("Copying data from t1 to file\n");fflush(stdout);
    sprintf(str, "ssh -i %s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no %s@%s '%s rm /tmp/t*.csv; %s chmod 777 -R /tmp'", Test->repl->sshkey[0], Test->repl->access_user, Test->repl->IP[0], Test->repl->access_sudo, Test->repl->access_sudo);
    printf("%s\n", str);
    system(str);

    printf("RWSplit:\n");fflush(stdout);
    global_result += execute_query(Test->conn_rwsplit, (char *) "SELECT * INTO OUTFILE '/tmp/t1.csv' FROM t1;");
    printf("ReadsConn master:\n");fflush(stdout);
    global_result += execute_query(Test->conn_master, (char *) "SELECT * INTO OUTFILE '/tmp/t2.csv' FROM t1;");
    printf("ReadsConn slave:\n");fflush(stdout);
    global_result += execute_query(Test->conn_slave, (char *) "SELECT * INTO OUTFILE '/tmp/t3.csv' FROM t1;");

    printf("Copying t1.cvs from Maxscale machine:\n");fflush(stdout);
    sprintf(str, "scp -i %s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no %s@%s:/tmp/t1.csv ./", Test->repl->sshkey[0], Test->repl->access_user, Test->repl->IP[0]);
    printf("%s\n", str);
    system(str);

    MYSQL *srv[2];

    srv[0] = Test->conn_rwsplit;
    srv[1] = Test->conn_master;
    for (int i=0; i<2; i++) {
        printf("Dropping t1 \n");fflush(stdout);
        global_result += execute_query(Test->conn_rwsplit, (char *) "DROP TABLE t1;");
        printf("Sleeping to let replication happen\n");fflush(stdout);
        sleep(100);
        printf("Create t1\n"); fflush(stdout);
        create_t1(Test->conn_rwsplit);
        printf("Loading data to t1 from file\n");fflush(stdout);
        global_result += execute_query(srv[i], (char *) "LOAD DATA LOCAL INFILE 't1.csv' INTO TABLE t1;");

        printf("Sleeping to let replication happen\n");fflush(stdout);
        sleep(100);
        printf("SELECT: rwsplitter\n");fflush(stdout);
        global_result += select_from_t1(Test->conn_rwsplit, N);
        printf("SELECT: master\n");fflush(stdout);
        global_result += select_from_t1(Test->conn_master, N);
        printf("SELECT: slave\n");fflush(stdout);
        global_result += select_from_t1(Test->conn_slave, N);
        printf("Sleeping to let replication happen\n");fflush(stdout);
        /*sleep(100);
        for (int i=0; i<Test->repl->N; i++) {
            printf("SELECT: directly from node %d\n", i);fflush(stdout);
            global_result += select_from_t1(Test->repl->nodes[i], N);
        }*/
    }



    Test->repl->close_connections();
    global_result += check_maxscale_alive();

    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;
    int i;

    Test->read_env();
    Test->print_env();

    printf("Connecting to Maxscale %s\n", Test->maxscale_IP);

    char sys1[4096];

    printf("Connecting to Maxscale %s to check its behaviour in case of blocking all bacxkends\n", Test->maxscale_IP);
    Test->connect_maxscale();

    for (i = 0; i < Test->repl->N; i++) {
        printf("Setup firewall to block mysql on node %d\n", i); fflush(stdout);
        Test->repl->block_node(i); fflush(stdout);
    }

    pid_t pid = fork();
    if (!pid) {
        Test->restart_maxscale(); fflush(stdout);
    } else {

        printf("Waiting 20 seconds\n"); fflush(stdout);
        sleep(20);

        printf("Checking if MaxScale is alive by connecting to MaxAdmin\n"); fflush(stdout);
        global_result += execute_maxadmin_command(Test->maxscale_IP, (char *) "admin", Test->maxadmin_password, (char* ) "show servers");

        for (i = 0; i < Test->repl->N; i++) {
            printf("Setup firewall back to allow mysql on node %d\n", i); fflush(stdout);
            Test->repl->unblock_node(i);fflush(stdout);
        }

        printf("Sleeping 60 seconds\n"); fflush(stdout);
        sleep(60);

        printf("Checking Maxscale is alive\n"); fflush(stdout);
        global_result += check_maxscale_alive(); fflush(stdout);
        if (global_result !=0) {
            printf("MaxScale is not alive\n");
        } else {
            printf("MaxScale is still alive\n");
        }

        Test->close_maxscale_connections(); fflush(stdout);

        printf("Reconnecting and trying query to RWSplit\n"); fflush(stdout);
        Test->connect_maxscale();
        global_result += execute_query(Test->conn_rwsplit, (char *) "show processlist;");
        printf("Trying query to ReadConn master\n"); fflush(stdout);
        global_result += execute_query(Test->conn_master, (char *) "show processlist;");
        printf("Trying query to ReadConn slave\n"); fflush(stdout);
        global_result += execute_query(Test->conn_slave, (char *) "show processlist;");
        Test->close_maxscale_connections();

        Test->copy_all_logs(); return(global_result);
    }
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    Test->set_timeout(60);

    Test->repl->connect();
    Test->connect_maxscale();

    Test->tprintf("Creating user 'user' \n");

    execute_query(Test->conn_rwsplit, "DROP USER 'user'@'%%'");
    Test->try_query(Test->conn_rwsplit, (char *) "CREATE USER user@'%%' identified by 'pass2'");
    Test->try_query(Test->conn_rwsplit, (char *) "GRANT SELECT ON test.* TO user@'%%'");
    Test->try_query(Test->conn_rwsplit, (char *) "FLUSH PRIVILEGES;");
    Test->try_query(Test->conn_rwsplit, (char *) "DROP TABLE IF EXISTS t1");
    Test->try_query(Test->conn_rwsplit, (char *) "CREATE TABLE t1 (x1 int, fl int)");

    Test->tprintf("Changing user... \n");
    Test->add_result(mysql_change_user(Test->conn_rwsplit, (char *) "user", (char *) "pass2", (char *) "test") ,
                     "changing user failed \n");
    Test->tprintf("mysql_error is %s\n", mysql_error(Test->conn_rwsplit));

    Test->tprintf("Trying INSERT (expecting access denied)... \n");
    if ( execute_query(Test->conn_rwsplit, (char *) "INSERT INTO t1 VALUES (77, 11);") == 0)
    {
        Test->add_result(1, "INSERT query succedded to user which does not have INSERT PRIVILEGES\n");
    }

    Test->tprintf("Changing user back... \n");
    Test->add_result(mysql_change_user(Test->conn_rwsplit, Test->repl->user_name, Test->repl->password,
                                       (char *) "test"), "changing user failed \n");

    Test->tprintf("Trying INSERT (expecting success)... \n");
    Test->try_query(Test->conn_rwsplit, (char *) "INSERT INTO t1 VALUES (77, 12);");

    Test->tprintf("Changing user with wrong password... \n");
    if (mysql_change_user(Test->conn_rwsplit, (char *) "user", (char *) "wrong_pass2", (char *) "test") == 0)
    {
        Test->add_result(1, "changing user with wrong password successed! \n");
    }
    Test->tprintf("%s\n", mysql_error(Test->conn_rwsplit));
    if ((strstr(mysql_error(Test->conn_rwsplit), "Access denied for user")) == NULL)
    {
        Test->add_result(1, "There is no proper error message\n");
    }

    Test->tprintf("Trying INSERT again (expecting success - use change should fail)... \n");
    Test->try_query(Test->conn_rwsplit, (char *) "INSERT INTO t1 VALUES (77, 13);");


    Test->tprintf("Changing user with wrong password using ReadConn \n");
    if (mysql_change_user(Test->conn_slave, (char *) "user", (char *) "wrong_pass2", (char *) "test") == 0)
    {
        Test->add_result(1, "FAILED: changing user with wrong password successed! \n");
    }
    Test->tprintf("%s\n", mysql_error(Test->conn_slave));
    if ((strstr(mysql_error(Test->conn_slave), "Access denied for user")) == NULL)
    {
        Test->add_result(1, "There is no proper error message\n");
    }

    Test->tprintf("Changing user for ReadConn \n");
    Test->add_result(mysql_change_user(Test->conn_slave, (char *) "user", (char *) "pass2", (char *) "test") ,
                     "changing user failed \n");

    Test->try_query(Test->conn_rwsplit, (char *) "DROP USER user@'%%';");
    execute_query_silent(Test->conn_rwsplit, "DROP TABLE test.t1");

    Test->close_maxscale_connections();
    int rval = Test->global_result;
    delete Test;
    return rval;
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    Test->set_timeout(10);

    Test->connect_maxscale();

    Test->set_timeout(10);
    Test->try_query(Test->conn_rwsplit, (char *) "SET @a=1");
    Test->stop_timeout();
    sleep(1);
    Test->set_timeout(20);
    Test->tprintf("Blocking first slave\n");
    Test->repl->block_node(1);
    Test->stop_timeout();
    sleep(5);
    Test->set_timeout(10);
    Test->tprintf("Unblocking first slave and blocking second slave\n");

    Test->repl->unblock_node(1);
    Test->stop_timeout();
    sleep(5);
    Test->repl->block_node(2);
    Test->stop_timeout();
    sleep(5);
    Test->set_timeout(20);

    int retries;

    for (retries = 0; retries < 10; retries++)
    {
        char server1_status[256];
        Test->get_maxadmin_param((char *) "show server server2", (char *) "Status", server1_status);
        if (strstr(server1_status, "Running"))
        {
            break;
        }
        sleep(1);
    }

    Test->add_result(retries == 10, "Slave is not recovered, slave status is not Running\n");

    Test->repl->connect();
    int real_id = Test->repl->get_server_id(1);

    char server_id[200] = "";
    find_field(Test->conn_rwsplit, "SELECT @@server_id", "@@server_id", server_id);
    int queried_id = atoi(server_id);

    Test->add_result(queried_id != real_id, "The query server ID '%d' does not match the one from server '%d'. "
                     "Slave was not recovered.", queried_id, real_id);

    char userval[200] = "";
    find_field(Test->conn_rwsplit, "SELECT @a", "@a", userval);

    Test->add_result(atoi(userval) != 1, "User variable @a is not 1, it is '%s'", userval);

    Test->tprintf("Unblocking second slave\n");
    Test->repl->unblock_node(2);

    Test->check_maxscale_alive();
    int rval = Test->global_result;
    delete Test;
    return rval;
}