1 | /* |
2 | */ |
3 | |
4 | #include "my_test.h" |
5 | #include <ma_pthread.h> |
6 | |
7 | static int basic_connect(MYSQL *unused __attribute__((unused))) |
8 | { |
9 | MYSQL_ROW row; |
10 | MYSQL_RES *res; |
11 | MYSQL_FIELD *field; |
12 | int rc; |
13 | |
14 | MYSQL *my= mysql_init(NULL); |
15 | FAIL_IF(!my, "mysql_init() failed" ); |
16 | |
17 | FAIL_IF(!my_test_connect(my, hostname, username, password, schema, |
18 | port, socketname, 0), mysql_error(my)); |
19 | |
20 | rc= mysql_query(my, "SELECT @@version" ); |
21 | check_mysql_rc(rc, my); |
22 | |
23 | res= mysql_store_result(my); |
24 | FAIL_IF(!res, mysql_error(my)); |
25 | field= mysql_fetch_fields(res); |
26 | FAIL_IF(!field, "Couldn't fetch fields" ); |
27 | |
28 | while ((row= mysql_fetch_row(res)) != NULL) |
29 | { |
30 | FAIL_IF(mysql_num_fields(res) != 1, "Got the wrong number of fields" ); |
31 | } |
32 | FAIL_IF(mysql_errno(my), mysql_error(my)); |
33 | |
34 | mysql_free_result(res); |
35 | mysql_close(my); |
36 | |
37 | |
38 | return OK; |
39 | } |
40 | |
41 | pthread_mutex_t LOCK_test; |
42 | |
43 | #ifndef _WIN32 |
44 | int thread_conc27(void); |
45 | #else |
46 | DWORD WINAPI thread_conc27(void); |
47 | #endif |
48 | |
49 | #define THREAD_NUM 100 |
50 | |
51 | /* run this test as root and increase the number of handles (ulimit -n) */ |
52 | static int test_conc_27(MYSQL *mysql) |
53 | { |
54 | |
55 | int rc; |
56 | int i; |
57 | MYSQL_ROW row; |
58 | MYSQL_RES *res; |
59 | #ifndef _WIN32 |
60 | pthread_t threads[THREAD_NUM]; |
61 | #else |
62 | HANDLE hthreads[THREAD_NUM]; |
63 | DWORD threads[THREAD_NUM]; |
64 | #endif |
65 | |
66 | diag("please run this test manually as root" ); |
67 | return SKIP; |
68 | |
69 | rc= mysql_query(mysql, "DROP TABLE IF EXISTS t_conc27" ); |
70 | check_mysql_rc(rc, mysql); |
71 | |
72 | rc= mysql_query(mysql, "CREATE TABLE t_conc27(a int)" ); |
73 | check_mysql_rc(rc, mysql); |
74 | |
75 | rc= mysql_query(mysql, "INSERT INTO t_conc27 VALUES(0)" ); |
76 | check_mysql_rc(rc, mysql); |
77 | |
78 | rc= mysql_query(mysql, "SET @a:=@@max_connections" ); |
79 | check_mysql_rc(rc, mysql); |
80 | |
81 | rc= mysql_query(mysql, "SET GLOBAL max_connections=100000" ); |
82 | check_mysql_rc(rc, mysql); |
83 | |
84 | pthread_mutex_init(&LOCK_test, NULL); |
85 | for (i=0; i < THREAD_NUM; i++) |
86 | { |
87 | #ifndef _WIN32 |
88 | pthread_create(&threads[i], NULL, (void *)thread_conc27, NULL); |
89 | #else |
90 | hthreads[i]= CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_conc27, NULL, 0, &threads[i]); |
91 | if (hthreads[i]==NULL) |
92 | diag("error while starting thread" ); |
93 | #endif |
94 | } |
95 | for (i=0; i < THREAD_NUM; i++) |
96 | { |
97 | #ifndef _WIN32 |
98 | pthread_join(threads[i], NULL); |
99 | #else |
100 | WaitForSingleObject(hthreads[i], INFINITE); |
101 | #endif |
102 | } |
103 | |
104 | pthread_mutex_destroy(&LOCK_test); |
105 | |
106 | rc= mysql_query(mysql, "SET GLOBAL max_connections=@a" ); |
107 | check_mysql_rc(rc, mysql); |
108 | |
109 | rc= mysql_query(mysql, "SELECT a FROM t_conc27" ); |
110 | check_mysql_rc(rc,mysql); |
111 | |
112 | res= mysql_store_result(mysql); |
113 | FAIL_IF(!res, "invalid result" ); |
114 | |
115 | row= mysql_fetch_row(res); |
116 | FAIL_IF(!row, "can't fetch row" ); |
117 | |
118 | diag("row=%s" , row[0]); |
119 | FAIL_IF(atoi(row[0]) != THREAD_NUM, "expected value THREAD_NUM" ); |
120 | mysql_free_result(res); |
121 | rc= mysql_query(mysql, "DROP TABLE t_conc27" ); |
122 | check_mysql_rc(rc,mysql); |
123 | |
124 | return OK; |
125 | } |
126 | |
127 | #ifndef _WIN32 |
128 | int thread_conc27(void) |
129 | #else |
130 | DWORD WINAPI thread_conc27(void) |
131 | #endif |
132 | { |
133 | MYSQL *mysql; |
134 | int rc; |
135 | MYSQL_RES *res; |
136 | mysql_thread_init(); |
137 | mysql= mysql_init(NULL); |
138 | if(!my_test_connect(mysql, hostname, username, password, schema, |
139 | port, socketname, 0)) |
140 | { |
141 | diag(">Error: %s" , mysql_error(mysql)); |
142 | mysql_close(mysql); |
143 | mysql_thread_end(); |
144 | goto end; |
145 | } |
146 | pthread_mutex_lock(&LOCK_test); |
147 | rc= mysql_query(mysql, "UPDATE t_conc27 SET a=a+1" ); |
148 | check_mysql_rc(rc, mysql); |
149 | pthread_mutex_unlock(&LOCK_test); |
150 | check_mysql_rc(rc, mysql); |
151 | if ((res= mysql_store_result(mysql))) |
152 | mysql_free_result(res); |
153 | mysql_close(mysql); |
154 | end: |
155 | mysql_thread_end(); |
156 | return 0; |
157 | } |
158 | |
159 | struct my_tests_st my_tests[] = { |
160 | {"basic_connect" , basic_connect, TEST_CONNECTION_NONE, 0, NULL, NULL}, |
161 | {"test_conc_27" , test_conc_27, TEST_CONNECTION_NEW, 0, NULL, NULL}, |
162 | {NULL, NULL, 0, 0, NULL, NULL} |
163 | }; |
164 | |
165 | |
166 | int main(int argc, char **argv) |
167 | { |
168 | |
169 | mysql_library_init(0,0,NULL); |
170 | |
171 | if (argc > 1) |
172 | get_options(argc, argv); |
173 | |
174 | get_envvars(); |
175 | |
176 | run_tests(my_tests); |
177 | |
178 | mysql_server_end(); |
179 | return(exit_status()); |
180 | } |
181 | |