Skip to content

Commit 92d2f49

Browse files
renecannaoJavierJF
authored andcommitted
More code testing
Added test that verifies a variety of things: - the following queries run as expected: * SELECT LAST_INSERT_ID() * SELECT LAST_INSERT_ID() LIMIT 1 * SELECT @@IDENTITY * SELECT CONNECTION_ID() - that killing backend connections works
1 parent ec58c81 commit 92d2f49

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

test/tap/tap/utils.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,55 @@ int read_pipe(int pipe_fd, std::string& sbuffer) {
157157
return res;
158158
}
159159

160+
int add_more_rows_test_sbtest1(int num_rows, MYSQL *mysql) {
161+
std::random_device rd;
162+
std::mt19937 mt(rd());
163+
std::uniform_int_distribution<int> dist(0.0, 9.0);
164+
165+
diag("Creating %d rows in sbtest1", num_rows);
166+
while (num_rows) {
167+
std::stringstream q;
168+
169+
q << "INSERT INTO test.sbtest1 (k, c, pad) values ";
170+
bool put_comma = false;
171+
int i=0;
172+
for (i=0; i<num_rows && i<20+rand()%50 ; ++i) {
173+
num_rows--;
174+
int k = dist(mt);
175+
std::stringstream c;
176+
for (int j=0; j<10; j++) {
177+
for (int k=0; k<11; k++) {
178+
c << dist(mt);
179+
}
180+
if (j<9)
181+
c << "-";
182+
}
183+
std::stringstream pad;
184+
for (int j=0; j<5; j++) {
185+
for (int k=0; k<11; k++) {
186+
pad << dist(mt);
187+
}
188+
if (j<4)
189+
pad << "-";
190+
}
191+
if (put_comma) q << ",";
192+
if (!put_comma) put_comma=true;
193+
q << "(" << k << ",'" << c.str() << "','" << pad.str() << "')";
194+
}
195+
MYSQL_QUERY(mysql, q.str().c_str());
196+
diag("Inserted %d rows ...", i);
197+
}
198+
diag("Done!");
199+
return 0;
200+
}
201+
int create_table_test_sbtest1(int num_rows, MYSQL *mysql) {
202+
MYSQL_QUERY(mysql, "CREATE DATABASE IF NOT EXISTS test");
203+
MYSQL_QUERY(mysql, "DROP TABLE IF EXISTS test.sbtest1");
204+
MYSQL_QUERY(mysql, "CREATE TABLE if not exists test.sbtest1 (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `k` int(10) unsigned NOT NULL DEFAULT '0', `c` char(120) NOT NULL DEFAULT '', `pad` char(60) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `k_1` (`k`))");
205+
206+
return add_more_rows_test_sbtest1(num_rows, mysql);
207+
}
208+
160209
int wexecvp(const std::string& file, const std::vector<const char*>& argv, const to_opts* opts, std::string& s_stdout, std::string& s_stderr) {
161210
// Pipes definition
162211
constexpr uint8_t NUM_PIPES = 3;

test/tap/tap/utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <mysql.h>
55
#include <string>
6+
#include <random>
7+
#include <sstream>
68
#include <vector>
79

810
#define MYSQL_QUERY(mysql, query) \
@@ -66,4 +68,8 @@ int execvp(const std::string& file, const std::vector<const char*>& argv, std::s
6668
*/
6769
int exec(const std::string& cmd, std::string& result);
6870

71+
// create table test.sbtest1 with num_rows rows
72+
int create_table_test_sbtest1(int num_rows, MYSQL *mysql);
73+
int add_more_rows_test_sbtest1(int num_rows, MYSQL *mysql);
74+
6975
#endif // #define UTILS_H
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)