From 83e276ebd3e00d857b11cea34d48d9f213552b4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 26 Jun 2019 15:26:45 +0300 Subject: [PATCH] MXS-2521: Fix the test Fixed the test to correctly set the parameters and also to first verify that a direct connection works before doing the test via MaxScale. --- maxscale-system-test/mxs2521_double_exec.cpp | 82 +++++++++++++------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/maxscale-system-test/mxs2521_double_exec.cpp b/maxscale-system-test/mxs2521_double_exec.cpp index ad110ecfe..c6ef40729 100644 --- a/maxscale-system-test/mxs2521_double_exec.cpp +++ b/maxscale-system-test/mxs2521_double_exec.cpp @@ -5,63 +5,87 @@ #include "testconnections.h" -int main(int argc, char** argv) +void do_test(TestConnections& test, MYSQL* conn) { - TestConnections test(argc, argv); - test.maxscales->connect(); - - auto conn = test.maxscales->conn_rwsplit[0]; - - test.try_query(conn, "DROP TABLE IF EXISTS double_execute;"); - test.try_query(conn, "CREATE TABLE double_execute(a int);"); - test.try_query(conn, "INSERT INTO double_execute VALUES (123), (456)"); - auto stmt = mysql_stmt_init(conn); std::string sql = "select a, @@server_id from double_execute where a = ?"; test.expect(mysql_stmt_prepare(stmt, sql.c_str(), sql.length()) == 0, "Prepare should work: %s", mysql_error(conn)); - int data[2] = {0, 0}; - MYSQL_BIND my_bind[2] = {}; - char is_null = 0; - my_bind[0].buffer_type = MYSQL_TYPE_LONG; - my_bind[0].buffer = &data[0]; - my_bind[0].buffer_length = sizeof(data[0]); - my_bind[0].is_null = &is_null; - my_bind[1].buffer_type = MYSQL_TYPE_LONG; - my_bind[1].buffer = &data[1]; - my_bind[1].buffer_length = sizeof(data[2]); - my_bind[1].is_null = &is_null; - data[1] = 123; - test.expect(mysql_stmt_bind_param(stmt, my_bind) == 0, "Bind: %s", mysql_stmt_error(stmt)); + int data_out = 123; + MYSQL_BIND bind_out; + char null_out = 0; + bind_out.buffer_type = MYSQL_TYPE_LONG; + bind_out.buffer = &data_out; + bind_out.buffer_length = sizeof(data_out); + bind_out.is_null = &null_out; + test.expect(mysql_stmt_bind_param(stmt, &bind_out) == 0, "Bind: %s", mysql_stmt_error(stmt)); // The first execute is done on the master test.try_query(conn, "BEGIN"); test.expect(mysql_stmt_execute(stmt) == 0, "First execute should work: %s", mysql_stmt_error(stmt)); - data[0] = 0; + int data_in[2] = {}; + MYSQL_BIND bind_in[2] = {}; + char null_in[2] = {}; + + for (int i = 0; i < 2; i++) + { + bind_in[i].buffer_type = MYSQL_TYPE_LONG; + bind_in[i].buffer = &data_in[i]; + bind_in[i].buffer_length = sizeof(data_in[i]); + bind_in[i].is_null = &null_in[i]; + } + + mysql_stmt_bind_result(stmt, bind_in); mysql_stmt_store_result(stmt); + test.expect(mysql_stmt_fetch(stmt) == 0, "First fetch of first execute should work"); - test.expect(data[0] == 123, "Query should return one row with value 123: `%d`", data[0]); + test.expect(data_in[0] == 123, "Query should return one row with value 123: `%d`", data_in[0]); test.expect(mysql_stmt_fetch(stmt) != 0, "Second fetch of first execute should NOT work"); + int first_server = data_in[1]; + test.try_query(conn, "COMMIT"); // The second execute goes to a slave, no new parameters are sent in it - data[0] = 123; + memset(data_in, 0, sizeof(data_in)); test.expect(mysql_stmt_execute(stmt) == 0, "Second execute should work: %s", mysql_stmt_error(stmt)); - data[0] = 0; + mysql_stmt_bind_result(stmt, bind_in); mysql_stmt_store_result(stmt); test.expect(mysql_stmt_fetch(stmt) == 0, "First fetch of second execute should work"); - test.expect(data[0] == 123, "Query should return one row with value 123: `%d`", data[0]); + test.expect(data_in[0] == 123, "Query should return one row with value 123: `%d`", data_in[0]); + test.expect(data_in[1] == first_server, + "The query should be routed to the server with server_id %d, not %d", + first_server, data_in[1]); test.expect(mysql_stmt_fetch(stmt) != 0, "Second fetch of second execute should NOT work"); mysql_stmt_close(stmt); +} - test.try_query(conn, "DROP TABLE IF EXISTS double_execute;"); +int main(int argc, char** argv) +{ + TestConnections test(argc, argv); + + test.repl->connect(); + test.maxscales->connect(); + + // Prepare a table + test.try_query(test.repl->nodes[0], "DROP TABLE IF EXISTS double_execute;"); + test.try_query(test.repl->nodes[0], "CREATE TABLE double_execute(a int);"); + test.try_query(test.repl->nodes[0], "INSERT INTO double_execute VALUES (123), (456)"); + test.repl->sync_slaves(); + + test.tprintf("Running test with a direct connection"); + do_test(test, test.repl->nodes[0]); + + test.tprintf("Running test through readwritesplit"); + do_test(test, test.maxscales->conn_rwsplit[0]); + + test.try_query(test.repl->nodes[0], "DROP TABLE IF EXISTS double_execute;"); return test.global_result; }