summaryrefslogtreecommitdiffstats
path: root/libmariadb/unittest/libmariadb/async.c
blob: bc6beaffd429986f1105767e2b0ce589624d9213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
  Copyright 2011 Kristian Nielsen and Monty Program Ab.

  This file is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "my_test.h"
#include "ma_common.h"


#ifndef _WIN32
#include <poll.h>
#else
#include <winsock2.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <mysql.h>

my_bool skip_async= 0;

static int test_async(MYSQL *mysql)
{
  int type;
  mariadb_get_info(mysql, MARIADB_CONNECTION_PVIO_TYPE, &type);
  if (type > MARIADB_CONNECTION_TCP)
  {
    skip_async= 1;
    diag("Async IO not supported");
  }
  return OK;
}

static int
wait_for_mysql(MYSQL *mysql, int status)
{
#ifdef _WIN32
  fd_set rs, ws, es;
  int res;
  struct timeval tv, *timeout;
  my_socket s= mysql_get_socket(mysql);
  FD_ZERO(&rs);
  FD_ZERO(&ws);
  FD_ZERO(&es);
  if (status & MYSQL_WAIT_READ)
    FD_SET(s, &rs);
  if (status & MYSQL_WAIT_WRITE)
    FD_SET(s, &ws);
  if (status & MYSQL_WAIT_EXCEPT)
    FD_SET(s, &es);
  if (status & MYSQL_WAIT_TIMEOUT)
  {
    tv.tv_sec= mysql_get_timeout_value(mysql);
    tv.tv_usec= 0;
    timeout= &tv;
  }
  else
    timeout= NULL;
  res= select(1, &rs, &ws, &es, timeout);
  if (res == 0)
    return MYSQL_WAIT_TIMEOUT;
  else if (res == SOCKET_ERROR)
  {
    /*
      In a real event framework, we should handle errors and re-try the select.
    */
    return MYSQL_WAIT_TIMEOUT;
  }
  else
  {
    int status= 0;
    if (FD_ISSET(s, &rs))
      status|= MYSQL_WAIT_READ;
    if (FD_ISSET(s, &ws))
      status|= MYSQL_WAIT_WRITE;
    if (FD_ISSET(s, &es))
      status|= MYSQL_WAIT_EXCEPT;
    return status;
  }
#else
  struct pollfd pfd;
  int timeout;
  int res= -1;

  pfd.fd= mysql_get_socket(mysql);
  pfd.events=
    (status & MYSQL_WAIT_READ ? POLLIN : 0) |
    (status & MYSQL_WAIT_WRITE ? POLLOUT : 0) |
    (status & MYSQL_WAIT_EXCEPT ? POLLPRI : 0);
  if (status & MYSQL_WAIT_TIMEOUT)
  {
    timeout= mysql_get_timeout_value_ms(mysql);
  }
  else
    timeout= -1;
  do {
    res= poll(&pfd, 1, timeout);
  } while (res == -1 && errno == EINTR);
  if (res == 0)
    return MYSQL_WAIT_TIMEOUT;
  else if (res < 0)
  {
    /*
      In a real event framework, we should handle EINTR and re-try the poll.
    */
    return MYSQL_WAIT_TIMEOUT;
  }
  else
  {
    int status= 0;
    if (pfd.revents & POLLIN)
      status|= MYSQL_WAIT_READ;
    if (pfd.revents & POLLOUT)
      status|= MYSQL_WAIT_WRITE;
    if (pfd.revents & POLLPRI)
      status|= MYSQL_WAIT_EXCEPT;
    return status;
  }
#endif
}

static int async1(MYSQL *unused __attribute__((unused)))
{
  int err= 0, rc;
  MYSQL mysql, *ret;
  MYSQL_RES *res;
  MYSQL_ROW row;
  int status;
  uint default_timeout;
  int i;

  if (skip_async)
    return SKIP;

  for (i=0; i < 100; i++)
  {

    mysql_init(&mysql);
    rc= mysql_options(&mysql, MYSQL_OPT_NONBLOCK, 0);
    check_mysql_rc(rc, (MYSQL *)&mysql);

    /* set timeouts to 300 microseconds */
    default_timeout= 3;
    mysql_options(&mysql, MYSQL_OPT_READ_TIMEOUT, &default_timeout);
    mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, &default_timeout);
    mysql_options(&mysql, MYSQL_OPT_WRITE_TIMEOUT, &default_timeout);
    mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "myapp");
    if (force_tls)
      mysql_ssl_set(&mysql, NULL, NULL, NULL, NULL,NULL);

    /* Returns 0 when done, else flag for what to wait for when need to block. */
    status= mysql_real_connect_start(&ret, &mysql, hostname, username, password, schema, port, socketname, 0);
    while (status)
    {
      status= wait_for_mysql(&mysql, status);
      status= mysql_real_connect_cont(&ret, &mysql, status);
    }
    if (!ret)
    {
      diag("Error: %s", mysql_error(&mysql));
      FAIL_IF(!ret, "Failed to mysql_real_connect()");
    }

    if (force_tls && !mysql_get_ssl_cipher(&mysql))
    {
      diag("Error: No tls connection");
      return FAIL;
    }

    status= mysql_real_query_start(&err, &mysql, SL("SHOW STATUS"));
    while (status)
    {
      status= wait_for_mysql(&mysql, status);
      status= mysql_real_query_cont(&err, &mysql, status);
    }
    FAIL_IF(err, "mysql_real_query() returns error");

    /* This method cannot block. */
    res= mysql_use_result(&mysql);
    FAIL_IF(!res, "mysql_use_result() returns error");

    for (;;)
    {
      status= mysql_fetch_row_start(&row, res);
      while (status)
      {
        status= wait_for_mysql(&mysql, status);
        status= mysql_fetch_row_cont(&row, res, status);
      }
      if (!row)
        break;
    }
    FAIL_IF(mysql_errno(&mysql), "Got error while retrieving rows");
    mysql_free_result(res);

    /*
      mysql_close() sends a COM_QUIT packet, and so in principle could block
      waiting for the socket to accept the data.
      In practise, for many applications it will probably be fine to use the
      blocking mysql_close().
     */
    status= mysql_close_start(&mysql);
    while (status)
    {
      status= wait_for_mysql(&mysql, status);
      status= mysql_close_cont(&mysql, status);
    }
  }
  return OK;
}

static int test_conc131(MYSQL *unused __attribute__((unused)))
{
  int rc;
  /* this test needs to run under valgrind */
  MYSQL *mysql;
  
  if (skip_async)
    return SKIP;

  mysql= mysql_init(NULL);
  rc= mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0);
  check_mysql_rc(rc, mysql);
  mysql_close(mysql);
  return OK;
}

static int test_conc129(MYSQL *unused __attribute__((unused)))
{
  MYSQL *mysql;
  
  if (skip_async)
    return SKIP;

  mysql= mysql_init(NULL);
  FAIL_IF(mysql_close_start(mysql), "No error expected");
  return OK;
}

static int test_conc622(MYSQL *my __attribute__((unused)))
{
  int rc;
  MYSQL mysql, *ret;
  int status;
  uint default_timeout;
  int i;
  const char *invalid_host= "1.0.0.0";

  if (skip_async)
    return SKIP;

  for (i=0; i < 5; i++)
  {
    mysql_init(&mysql);
    rc= mysql_options(&mysql, MYSQL_OPT_NONBLOCK, 0);
    check_mysql_rc(rc, (MYSQL *)&mysql);

    /* set timeouts to 300 microseconds */
    default_timeout= 3;
    mysql_options(&mysql, MYSQL_OPT_READ_TIMEOUT, &default_timeout);
    mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, &default_timeout);
    mysql_options(&mysql, MYSQL_OPT_WRITE_TIMEOUT, &default_timeout);
    mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "myapp");

    /* Returns 0 when done, else flag for what to wait for when need to block. */
    status= mysql_real_connect_start(&ret, &mysql, invalid_host, username, password, schema, port, socketname, 0);
    while (status)
    {
      status= wait_for_mysql(&mysql, status);
      status= mysql_real_connect_cont(&ret, &mysql, status);
    }
    if (!ret)
    {
      status= mysql_close_start(&mysql);
      while (status)
      {
        status= wait_for_mysql(&mysql, status);
        status= mysql_close_cont(&mysql, status);
      }
    } else {
      diag("Expected error when connecting to host '%s'", invalid_host);
      diag("Connected to %s", mysql_get_server_info(&mysql));
      return FAIL;
    }
  }
  return OK;
}


struct my_tests_st my_tests[] = {
  {"test_async", test_async, TEST_CONNECTION_DEFAULT, 0,  NULL,  NULL},
  {"async1", async1, TEST_CONNECTION_DEFAULT, 0,  NULL,  NULL},
  {"test_conc131", test_conc131, TEST_CONNECTION_NONE, 0,  NULL,  NULL},
  {"test_conc129", test_conc129, TEST_CONNECTION_NONE, 0,  NULL,  NULL},
  {"test_conc622", test_conc622, TEST_CONNECTION_NONE, 0,  NULL,  NULL},
  {NULL, NULL, 0, 0, NULL, NULL}
};


int main(int argc, char **argv)
{
  if (argc > 1)
    get_options(argc, argv);

  get_envvars();

  run_tests(my_tests);

  return(exit_status());
}