int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;

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


    Test->binlog_cmd_option = 2;
    Test->start_binlog();

    Test->repl->connect();

    create_t1(Test->repl->nodes[0]);
    global_result += insert_into_t1(Test->repl->nodes[0], 4);
    printf("Sleeping to let replication happen\n"); fflush(stdout);
    sleep(30);

    for (int i = 0; i < Test->repl->N; i++) {
        printf("Checking data from node %d (%s)\n", i, Test->repl->IP[i]); fflush(stdout);
        global_result += select_from_t1(Test->repl->nodes[i], 4);
    }

    Test->repl->close_connections();


    Test->copy_all_logs(); return(global_result);
}
void test_binlog(TestConnections* Test)
{
    int i;
    MYSQL* binlog;
    Test->repl->connect();

    Test->set_timeout(100);
    Test->try_query(Test->repl->nodes[0], (char *) "SET NAMES utf8mb4");
    Test->try_query(Test->repl->nodes[0], (char *) "set autocommit=1");
    Test->try_query(Test->repl->nodes[0], (char *) "select USER()");

    Test->set_timeout(100);
    create_t1(Test->repl->nodes[0]);
    Test->add_result(insert_into_t1(Test->repl->nodes[0], 4), "Data inserting to t1 failed\n");
    Test->stop_timeout();
    Test->tprintf("Sleeping to let replication happen\n");
    sleep(60);

    for (i = 0; i < Test->repl->N; i++)
    {
        Test->tprintf("Checking data from node %d (%s)\n", i, Test->repl->IP[i]);
        Test->set_timeout(100);
        Test->add_result(select_from_t1(Test->repl->nodes[i], 4), "Selecting from t1 failed\n");
        Test->stop_timeout();
    }

    Test->set_timeout(10);
    Test->tprintf("First transaction test (with ROLLBACK)\n");
    start_transaction(Test);

    Test->set_timeout(50);

    Test->tprintf("SELECT * FROM t1 WHERE fl=10, checking inserted values\n");
    Test->add_result(execute_query_check_one(Test->repl->nodes[0], (char *) "SELECT * FROM t1 WHERE fl=10",
                     "111"), "SELECT check failed\n");

    //Test->add_result(check_sha1(Test), "sha1 check failed\n");

    Test->tprintf("ROLLBACK\n");
    Test->try_query(Test->repl->nodes[0], (char *) "ROLLBACK");
    Test->tprintf("INSERT INTO t1 VALUES(112, 10)\n");
    Test->try_query(Test->repl->nodes[0], (char *) "INSERT INTO t1 VALUES(112, 10)");
    Test->try_query(Test->repl->nodes[0], (char *) "COMMIT");
    Test->stop_timeout();
    sleep(20);

    Test->set_timeout(20);
    Test->tprintf("SELECT * FROM t1 WHERE fl=10, checking inserted values\n");
    Test->add_result(execute_query_check_one(Test->repl->nodes[0], (char *) "SELECT * FROM t1 WHERE fl=10",
                     "112"), "SELECT check failed\n");

    Test->tprintf("SELECT * FROM t1 WHERE fl=10, checking inserted values from slave\n");
    Test->add_result(execute_query_check_one(Test->repl->nodes[2], (char *) "SELECT * FROM t1 WHERE fl=10",
                     "112"), "SELECT check failed\n");
    Test->tprintf("DELETE FROM t1 WHERE fl=10\n");
    Test->try_query(Test->repl->nodes[0], (char *) "DELETE FROM t1 WHERE fl=10");
    Test->tprintf("Checking t1\n");
    Test->add_result(select_from_t1(Test->repl->nodes[0], 4), "SELECT from t1 failed\n");

    Test->tprintf("Second transaction test (with COMMIT)\n");
    start_transaction(Test);

    Test->tprintf("COMMIT\n");
    Test->try_query(Test->repl->nodes[0], (char *) "COMMIT");

    Test->tprintf("SELECT, checking inserted values\n");
    Test->add_result(execute_query_check_one(Test->repl->nodes[0], (char *) "SELECT * FROM t1 WHERE fl=10",
                     "111"), "SELECT check failed\n");

    Test->tprintf("SELECT, checking inserted values from slave\n");
    Test->add_result(execute_query_check_one(Test->repl->nodes[2], (char *) "SELECT * FROM t1 WHERE fl=10",
                     "111"), "SELECT check failed\n");
    Test->tprintf("DELETE FROM t1 WHERE fl=10\n");
    Test->try_query(Test->repl->nodes[0], (char *) "DELETE FROM t1 WHERE fl=10");

    Test->stop_timeout();

    Test->set_timeout(50);
    Test->add_result(check_sha1(Test), "sha1 check failed\n");
    Test->repl->close_connections();

    Test->stop_timeout();

    // test SLAVE STOP/START
    for (int j = 0; j < 3; j++)
    {
        Test->set_timeout(100);
        Test->repl->connect();

        Test->tprintf("Dropping and re-creating t1\n");
        Test->try_query(Test->repl->nodes[0], (char *) "DROP TABLE IF EXISTS t1");
        create_t1(Test->repl->nodes[0]);

        Test->tprintf("Connecting to MaxScale binlog router\n");
        binlog = open_conn(Test->binlog_port, Test->maxscale_IP, Test->repl->user_name, Test->repl->password,
                           Test->ssl);

        Test->tprintf("STOP SLAVE against Maxscale binlog\n");
        execute_query(binlog, (char *) "STOP SLAVE");

        if (j == 1)
        {
            Test->tprintf("FLUSH LOGS on master\n");
            execute_query(Test->repl->nodes[0], (char *) "FLUSH LOGS");
        }
        Test->add_result(insert_into_t1(Test->repl->nodes[0], 4), "INSERT into t1 failed\n");

        Test->tprintf("START SLAVE against Maxscale binlog\n");
        Test->try_query(binlog, (char *) "START SLAVE");

        Test->tprintf("Sleeping to let replication happen\n");
        Test->stop_timeout();
        sleep(30);

        for (i = 0; i < Test->repl->N; i++)
        {
            Test->set_timeout(50);
            Test->tprintf("Checking data from node %d (%s)\n", i, Test->repl->IP[i]);
            Test->add_result(select_from_t1(Test->repl->nodes[i], 4), "SELECT from t1 failed\n");
        }

        Test->set_timeout(100);
        Test->add_result(check_sha1(Test), "sha1 check failed\n");
        Test->repl->close_connections();
        Test->stop_timeout();
    }
}