|
| 1 | +#include <vector> |
| 2 | +#include <string> |
| 3 | +#include <stdio.h> |
| 4 | +#include <cstring> |
| 5 | +#include <unistd.h> |
| 6 | +#include <mysql.h> |
| 7 | + |
| 8 | +#include "tap.h" |
| 9 | +#include "command_line.h" |
| 10 | +#include "utils.h" |
| 11 | + |
| 12 | + |
| 13 | +/* |
| 14 | +This test verifies a variety of things: |
| 15 | +- the following queries run as expected: |
| 16 | + * SELECT LAST_INSERT_ID() |
| 17 | + * SELECT LAST_INSERT_ID() LIMIT 1 |
| 18 | + * SELECT @@IDENTITY |
| 19 | + * SELECT CONNECTION_ID() |
| 20 | +- that killing backend connections works |
| 21 | +*/ |
| 22 | + |
| 23 | +const int NUM_CONNS = 5; |
| 24 | + |
| 25 | +int run_q(MYSQL *mysql, const char *q) { |
| 26 | + MYSQL_QUERY(mysql,q); |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +int main(int argc, char** argv) { |
| 31 | + CommandLine cl; |
| 32 | + |
| 33 | + int np = NUM_CONNS ; // for last insert id |
| 34 | + np += NUM_CONNS -1 ; // to compare all last insert id |
| 35 | + np += NUM_CONNS ; // to get connection id |
| 36 | + np += NUM_CONNS -1 ; // failed query on killed connection |
| 37 | + |
| 38 | + plan(np); |
| 39 | + |
| 40 | + if (cl.getEnv()) { |
| 41 | + diag("Failed to get the required environmental variables."); |
| 42 | + return -1; |
| 43 | + } |
| 44 | + |
| 45 | + MYSQL * conns[NUM_CONNS]; |
| 46 | + unsigned long long last_id[NUM_CONNS]; |
| 47 | + unsigned long mythreadid[NUM_CONNS]; |
| 48 | + for (int i = 0; i < NUM_CONNS ; i++) { |
| 49 | + MYSQL * mysql = mysql_init(NULL); |
| 50 | + if (!mysql) { |
| 51 | + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(mysql)); |
| 52 | + return exit_status(); |
| 53 | + } |
| 54 | + |
| 55 | + if (!mysql_real_connect(mysql, cl.host, cl.username, cl.password, NULL, cl.port, NULL, 0)) { |
| 56 | + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(mysql)); |
| 57 | + return exit_status(); |
| 58 | + } |
| 59 | + conns[i] = mysql; |
| 60 | + } |
| 61 | + |
| 62 | + for (int i = 0; i < NUM_CONNS ; i++) { |
| 63 | + MYSQL * mysql = conns[i]; |
| 64 | + if (i == 0) { |
| 65 | + if (create_table_test_sbtest1(100,mysql)) { |
| 66 | + fprintf(stderr, "File %s, line %d, Error: create_table_test_sbtest1() failed\n", __FILE__, __LINE__); |
| 67 | + return exit_status(); |
| 68 | + } |
| 69 | + } else { |
| 70 | + if (add_more_rows_test_sbtest1(100,mysql)) { |
| 71 | + fprintf(stderr, "File %s, line %d, Error: add_more_rows_sbtest1() failed\n", __FILE__, __LINE__); |
| 72 | + return exit_status(); |
| 73 | + } |
| 74 | + } |
| 75 | + unsigned long long a, b, c; |
| 76 | + unsigned long tid; |
| 77 | + MYSQL_ROW row; |
| 78 | + MYSQL_QUERY(mysql, "SELECT LAST_INSERT_ID()"); |
| 79 | + MYSQL_RES* proxy_res = mysql_store_result(mysql); |
| 80 | + while ((row = mysql_fetch_row(proxy_res))) { |
| 81 | + a = atoll(row[0]); |
| 82 | + } |
| 83 | + mysql_free_result(proxy_res); |
| 84 | + MYSQL_QUERY(mysql, "SELECT LAST_INSERT_ID() LIMIT 1"); |
| 85 | + proxy_res = mysql_store_result(mysql); |
| 86 | + while ((row = mysql_fetch_row(proxy_res))) { |
| 87 | + b = atoll(row[0]); |
| 88 | + } |
| 89 | + mysql_free_result(proxy_res); |
| 90 | + MYSQL_QUERY(mysql, "SELECT @@IDENTITY"); |
| 91 | + proxy_res = mysql_store_result(mysql); |
| 92 | + while ((row = mysql_fetch_row(proxy_res))) { |
| 93 | + c = atoll(row[0]); |
| 94 | + } |
| 95 | + mysql_free_result(proxy_res); |
| 96 | + // the 3 queries above should all return the same result |
| 97 | + ok(a > 0 && a == b && b == c && a == mysql_insert_id(mysql), "LAST_INSERT_ID: %llu , LAST_INSERT_ID_LIMIT1: %llu , IDENTITY: %llu , mysql_insert_id: %llu", a, b, c, mysql_insert_id(mysql)); |
| 98 | + last_id[i] = a; |
| 99 | + } |
| 100 | + |
| 101 | + for (int i = 1; i < NUM_CONNS ; i++) { |
| 102 | + ok(last_id[i-1] < last_id[i], "%llu < %llu" , last_id[i-1] , last_id[i]); |
| 103 | + } |
| 104 | + |
| 105 | + for (int i = 0; i < NUM_CONNS ; i++) { |
| 106 | + MYSQL * mysql = conns[i]; |
| 107 | + unsigned long tid; |
| 108 | + MYSQL_ROW row; |
| 109 | + MYSQL_QUERY(mysql, "SELECT CONNECTION_ID()"); |
| 110 | + MYSQL_RES * proxy_res = mysql_store_result(mysql); |
| 111 | + while ((row = mysql_fetch_row(proxy_res))) { |
| 112 | + tid = atoll(row[0]); |
| 113 | + } |
| 114 | + mysql_free_result(proxy_res); |
| 115 | + ok(tid == mysql_thread_id(mysql), "tid: %lu, mysql_thread_id(): %lu", tid, mysql_thread_id(mysql)); |
| 116 | + mythreadid[i] = tid; |
| 117 | + } |
| 118 | + for (int i = 0; i < NUM_CONNS ; i++) { |
| 119 | + MYSQL * mysql = conns[i]; |
| 120 | + if (i == 0) { |
| 121 | + for (int j = 1 ; j < NUM_CONNS; j++) { |
| 122 | + std::string s = "KILL CONNECTION " + std::to_string(mythreadid[j]); |
| 123 | + diag("Running: %s", s.c_str()); |
| 124 | + MYSQL_QUERY(mysql, s.c_str()); |
| 125 | + } |
| 126 | + } else { |
| 127 | + int rc = run_q(mysql, "DO 1"); |
| 128 | + ok(rc != 0, "Connection killed"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return exit_status(); |
| 133 | +} |
0 commit comments