diff --git a/test/tap/tests/Makefile b/test/tap/tests/Makefile index 8d8d7f859..8dcb9e510 100644 --- a/test/tap/tests/Makefile +++ b/test/tap/tests/Makefile @@ -108,4 +108,7 @@ test_tokenizer-t: test_tokenizer-t.cpp $(TAP_LIBDIR)/libtap.a g++ -DDEBUG test_mixed_compression-t.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) -std=c++11 $(OBJ) $(MYLIBS) -ltap -ldl $(STATIC_LIBS) -o reg_test_1493-mixed_compression-t -DGITVERSION=\"$(GIT_VERSION)\" 2793_compression: reg_test_2793-compression-t.cpp - g++ -DDEBUG reg_test_2793-compression-t.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) -std=c++11 $(OBJ) $(MYLIBS) -ltap -ldl $(STATIC_LIBS) -o reg_test_2793-compression-t -DGITVERSION=\"$(GIT_VERSION)\" + g++ -DTEST_AURORA -DDEBUG reg_test_2793-compression-t.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) -std=c++11 $(OBJ) $(MYLIBS) -ltap -ldl $(STATIC_LIBS) -o reg_test_2793-compression-t -DGITVERSION=\"$(GIT_VERSION)\" + +create_connection_annotation: test_connection_annotation-t.cpp + g++ -DTEST_AURORA -DDEBUG test_connection_annotation-t.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) -std=c++11 $(OBJ) $(MYLIBS) -ltap -ldl $(STATIC_LIBS) -o test_connection_annotation-t -DGITVERSION=\"$(GIT_VERSION)\" diff --git a/test/tap/tests/test_connection_annotation-t.cpp b/test/tap/tests/test_connection_annotation-t.cpp new file mode 100644 index 000000000..74e9b7963 --- /dev/null +++ b/test/tap/tests/test_connection_annotation-t.cpp @@ -0,0 +1,98 @@ +/** + * @file test_connection_annotation-t.cpp + * @brief This test verifies the feature 'create_new_connection' annotation is working properly. Strategy used is doing + * a random number of queries that by themselves shouldn't increase the number of free connections ('ConnFree'), but with + * the new supported annotation, and verify that the total number of free connections is increased by that random number. + */ + +#include +#include +#include +#include + +#include +#include + +#include "tap.h" +#include "command_line.h" +#include "utils.h" + +int main(int argc, char** argv) { + CommandLine cl; + + if (cl.getEnv()) { + diag("Failed to get the required environmental variables."); + return -1; + } + + MYSQL* proxysql_mysql = mysql_init(NULL); + MYSQL* proxysql_admin = mysql_init(NULL); + + if (!mysql_real_connect(proxysql_mysql, cl.host, cl.username, cl.password, NULL, cl.port, NULL, 0)) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_mysql)); + return -1; + } + if (!mysql_real_connect(proxysql_admin, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_mysql)); + return -1; + } + + const char* stats_mysql_connection_pool = "SELECT COUNT(*) FROM (SELECT ConnFree FROM stats.stats_mysql_connection_pool WHERE hostgroup=1) WHERE ConnFree >= 1"; + + while (true) { + MYSQL_QUERY(proxysql_mysql, "SELECT 1"); + MYSQL_RES* proxy_res = mysql_store_result(proxysql_mysql); + mysql_free_result(proxy_res); + + MYSQL_QUERY(proxysql_admin, stats_mysql_connection_pool); + MYSQL_RES* admin_res = mysql_store_result(proxysql_admin); + MYSQL_ROW row = mysql_fetch_row(admin_res); + + if (strstr(row[0], "3")) { + break; + } + } + + // We should check and store all the actual free connections + MYSQL_QUERY(proxysql_admin, "SELECT ConnFree FROM stats.stats_mysql_connection_pool WHERE hostgroup=1"); + MYSQL_RES* proxy_res = mysql_store_result(proxysql_admin); + + std::vector free_connections {}; + MYSQL_ROW row; + while ((row = mysql_fetch_row(proxy_res))) { + int row_free_conn = atoi(row[0]); + free_connections.push_back(row_free_conn); + } + + mysql_free_result(proxy_res); + + // Do a random number of normal selects using the anotation and verify that the connections has increased by that number + srand(time(NULL)); + int rand_conn = rand() % 100; + for (int i = 0; i < rand_conn; i++) { + MYSQL_QUERY(proxysql_mysql, "SELECT /*+ ;create_new_connection=1 */ 1"); + proxy_res = mysql_store_result(proxysql_mysql); + mysql_free_result(proxy_res); + } + + MYSQL_QUERY(proxysql_admin, "SELECT ConnFree FROM stats.stats_mysql_connection_pool WHERE hostgroup=1"); + proxy_res = mysql_store_result(proxysql_admin); + std::vector new_free_connections {}; + + while ((row = mysql_fetch_row(proxy_res))) { + int row_free_conn = atoi(row[0]); + new_free_connections.push_back(row_free_conn); + } + + mysql_free_result(proxy_res); + + int new_total_free_conn = 0; + // Sum the differences between previous free and new free connections + for (int i = 0; i < free_connections.size(); i++) { + new_total_free_conn += new_free_connections[i] - free_connections[i]; + } + + ok(rand_conn == new_total_free_conn, "The number of queries executed with annotations should be equal to the number of new connections: %d == %d", rand_conn, new_total_free_conn); + + return exit_status(); +}