From 3843a54bede315ca4e2d0827539a9bc4bf40eeed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Fri, 28 Jul 2023 19:19:53 +0200 Subject: [PATCH] Remove duplicate code from several tests --- .../reg_test_3184-set_wait_timeout-t.cpp | 25 +---------------- .../reg_test_3493-USE_with_comment-t.cpp | 10 +------ .../test_auto_increment_delay_multiplex-t.cpp | 26 ++---------------- ...ult_value_transaction_isolation_attr-t.cpp | 27 ++++--------------- .../test_keep_multiplexing_variables-t.cpp | 15 +---------- 5 files changed, 10 insertions(+), 93 deletions(-) diff --git a/test/tap/tests/reg_test_3184-set_wait_timeout-t.cpp b/test/tap/tests/reg_test_3184-set_wait_timeout-t.cpp index cb3f967d0..ff5b5cdcc 100644 --- a/test/tap/tests/reg_test_3184-set_wait_timeout-t.cpp +++ b/test/tap/tests/reg_test_3184-set_wait_timeout-t.cpp @@ -23,24 +23,6 @@ using std::string; using namespace nlohmann; - -/** - * @brief Helper function to convert a 'MYSQL_RES' into a - * nlohmann::json. - * - * @param result The 'MYSQL_RES*' to be converted into JSON. - * @param j 'nlohmann::json' output parameter holding the - * converted 'MYSQL_RES' supplied. - */ -void parse_result_json_column(MYSQL_RES *result, json& j) { - if(!result) return; - MYSQL_ROW row; - - while ((row = mysql_fetch_row(result))) { - j = json::parse(row[0]); - } -} - /** * @brief Valid variations of 'SET wait_timeout' supported * by ProxySQL to be ignored. @@ -96,12 +78,7 @@ int main(int argc, char** argv) { int query_err = mysql_query(proxysql_mysql, set_wait_timeout.c_str()); ok (query_err == 0, "Query '%s' should be properly executed.", set_wait_timeout.c_str()); - MYSQL_QUERY(proxysql_mysql, "PROXYSQL INTERNAL SESSION"); - json j_status {}; - MYSQL_RES* int_session_res = mysql_store_result(proxysql_mysql); - parse_result_json_column(int_session_res, j_status); - mysql_free_result(int_session_res); - + json j_status = fetch_internal_session(proxysql_mysql); bool found_backends = j_status.contains("backends"); ok(found_backends == false, "No backends should be found for the current connection."); } diff --git a/test/tap/tests/reg_test_3493-USE_with_comment-t.cpp b/test/tap/tests/reg_test_3493-USE_with_comment-t.cpp index 9ae225108..ee83a67f7 100644 --- a/test/tap/tests/reg_test_3493-USE_with_comment-t.cpp +++ b/test/tap/tests/reg_test_3493-USE_with_comment-t.cpp @@ -45,15 +45,7 @@ void parse_result_json_column(MYSQL_RES *result, json& j) { int get_session_schemaname(MYSQL* proxysql, std::string& schemaname) { int res = EXIT_FAILURE; - json j_status; - int query_res = mysql_query(proxysql, "PROXYSQL INTERNAL SESSION"); - if (query_res) { - return query_res; - } - - MYSQL_RES* tr_res = mysql_store_result(proxysql); - parse_result_json_column(tr_res, j_status); - mysql_free_result(tr_res); + json j_status = fetch_internal_session(proxysql); try { schemaname = j_status["client"]["userinfo"]["schemaname"]; diff --git a/test/tap/tests/test_auto_increment_delay_multiplex-t.cpp b/test/tap/tests/test_auto_increment_delay_multiplex-t.cpp index a6b321bec..1ffd35e0c 100644 --- a/test/tap/tests/test_auto_increment_delay_multiplex-t.cpp +++ b/test/tap/tests/test_auto_increment_delay_multiplex-t.cpp @@ -82,20 +82,10 @@ int get_query_result(MYSQL* mysql, const string& query, uint64_t& out_val) { #define log_err(err_msg) fprintf(stderr, "File %s, line %d, Error: \"%s\"\n", __FILE__, __LINE__, err_msg); int get_conn_auto_inc_delay_token(MYSQL* proxy_mysql, int& out_auto_inc_delay) { - MYSQL_QUERY(proxy_mysql, "PROXYSQL INTERNAL SESSION"); - MYSQL_RES* my_res = mysql_store_result(proxy_mysql); - vector int_sess_res = extract_mysql_rows(my_res); - mysql_free_result(my_res); - int cur_auto_inc_delay_mult = 0; - if (int_sess_res.empty()) { - log_err("Empty result received from 'PROXYSQL INTERNAL SESSION'"); - return EXIT_FAILURE; - } - try { - nlohmann::json j_int_sess = nlohmann::json::parse(int_sess_res[0][0]); + nlohmann::json j_int_sess = fetch_internal_session(proxy_mysql); nlohmann::json backend_conns = j_int_sess.at("backends"); nlohmann::json m_off_conn {}; @@ -124,20 +114,8 @@ int get_conn_auto_inc_delay_token(MYSQL* proxy_mysql, int& out_auto_inc_delay) { } int get_session_backends(MYSQL* proxy_mysql,vector& out_backend_conns) { - MYSQL_QUERY(proxy_mysql, "PROXYSQL INTERNAL SESSION"); - MYSQL_RES* my_res = mysql_store_result(proxy_mysql); - vector int_sess_res = extract_mysql_rows(my_res); - mysql_free_result(my_res); - - int cur_auto_inc_delay_mult = 0; - - if (int_sess_res.empty()) { - log_err("Empty result received from 'PROXYSQL INTERNAL SESSION'"); - return EXIT_FAILURE; - } - try { - nlohmann::json j_int_sess = nlohmann::json::parse(int_sess_res[0][0]); + nlohmann::json j_int_sess = fetch_internal_session(proxy_mysql); nlohmann::json backend_conns = j_int_sess.at("backends"); vector _out_conns {}; diff --git a/test/tap/tests/test_default_value_transaction_isolation_attr-t.cpp b/test/tap/tests/test_default_value_transaction_isolation_attr-t.cpp index 342205bcb..a84fe833f 100644 --- a/test/tap/tests/test_default_value_transaction_isolation_attr-t.cpp +++ b/test/tap/tests/test_default_value_transaction_isolation_attr-t.cpp @@ -33,23 +33,6 @@ using std::string; using namespace nlohmann; -/** - * @brief Helper function to convert a 'MYSQL_RES' into a - * nlohmann::json. - * - * @param result The 'MYSQL_RES*' to be converted into JSON. - * @param j 'nlohmann::json' output parameter holding the - * converted 'MYSQL_RES' supplied. - */ -void parse_result_json_column(MYSQL_RES *result, json& j) { - if(!result) return; - MYSQL_ROW row; - - while ((row = mysql_fetch_row(result))) { - j = json::parse(row[0]); - } -} - using user_attributes = std::tuple; /** @@ -87,11 +70,7 @@ int check_front_conn_isolation_level( const std::string& exp_iso_level, const bool set_via_attr ) { - MYSQL_QUERY(proxysql_mysql, "PROXYSQL INTERNAL SESSION"); - json j_status {}; - MYSQL_RES* int_session_res = mysql_store_result(proxysql_mysql); - parse_result_json_column(int_session_res, j_status); - mysql_free_result(int_session_res); + json j_status = fetch_internal_session(proxysql_mysql); try { std::string front_conn_isolation_level = @@ -145,6 +124,7 @@ int check_backend_conn_isolation_level( // Verify that the query produced a correct result if (trx_iso_row && trx_iso_row[0]) { trx_iso_val = std::string { trx_iso_row[0] }; + mysql_free_result(trx_iso_res); } else { const std::string err_msg { "Empty result received from query '" + select_trx_iso_query + "'" @@ -335,5 +315,8 @@ int main(int argc, char** argv) { mysql_close(proxysql_mysql); } + mysql_close(proxysql_admin); + mysql_close(mysql_server); + return exit_status(); } diff --git a/test/tap/tests/test_keep_multiplexing_variables-t.cpp b/test/tap/tests/test_keep_multiplexing_variables-t.cpp index 2ed928549..ef4995bc9 100644 --- a/test/tap/tests/test_keep_multiplexing_variables-t.cpp +++ b/test/tap/tests/test_keep_multiplexing_variables-t.cpp @@ -18,15 +18,6 @@ using std::string; using namespace nlohmann; -void parse_result_json_column(MYSQL_RES *result, json& j) { - if(!result) return; - MYSQL_ROW row; - - while ((row = mysql_fetch_row(result))) { - j = json::parse(row[0]); - } -} - std::vector select_queries { "select @@session.autocommit, @@session.big_tables, @@autocommit,@@bulk_insert_buffer_size, @@character_set_database,@@transaction_isolation, @@version,@@session.transaction_isolation", "select @@autocommit, @@sql_mode, @@big_tables, @@autocommit,@@bulk_insert_buffer_size, @@character_set_database,@@session.transaction_isolation, @@version,@@transaction_isolation", @@ -53,11 +44,7 @@ int check_multiplexing_disabled(const CommandLine& cl, const std::string query, MYSQL_RES* dummy_res = mysql_store_result(proxysql_mysql); mysql_free_result(dummy_res); - MYSQL_QUERY(proxysql_mysql, "PROXYSQL INTERNAL SESSION"); - json j_status {}; - MYSQL_RES* int_session_res = mysql_store_result(proxysql_mysql); - parse_result_json_column(int_session_res, j_status); - mysql_free_result(int_session_res); + json j_status = fetch_internal_session(proxysql_mysql); if (j_status.contains("backends")) { for (auto& backend : j_status["backends"]) {