int main(void) { close(STDOUT_FILENO); if (getenv("TESTS_ENVIRONMENT") && strstr(getenv("TESTS_ENVIRONMENT"), "valgrind")) { return EXIT_SUCCESS; } drizzle_verbose_t verbose; drizzle_st *drizzle; drizzle_st drizzle_buffer; drizzle_st *clone; drizzle_st clone_buffer; drizzle_con_st *con; drizzle_con_st *listen_con; drizzle_return_t ret; printf("sizeof(drizzle_st) = %zu\n", sizeof(drizzle_st)); printf("sizeof(drizzle_return_t) = %zu\n", sizeof(drizzle_return_t)); printf("sizeof(drizzle_verbose_t) = %zu\n", sizeof(drizzle_verbose_t)); printf("sizeof(drizzle_options_t) = %zu\n", sizeof(drizzle_options_t)); drizzle_test("drizzle_version"); printf(" %s\n", drizzle_version()); drizzle_test("drizzle_bugreport"); printf(" %s\n", drizzle_bugreport()); drizzle_test("drizzle_verbose_name"); for (verbose= DRIZZLE_VERBOSE_NEVER; verbose <= DRIZZLE_VERBOSE_MAX; verbose++) { printf(" %s\n", drizzle_verbose_name(verbose)); } drizzle_test("drizzle_create buffer"); if ((drizzle= drizzle_create(&drizzle_buffer)) == NULL) { drizzle_test_error("returned NULL"); } drizzle_test("drizzle_free buffer"); drizzle_free(drizzle); drizzle_test("drizzle_create"); if ((drizzle= drizzle_create(NULL)) == NULL) { drizzle_test_error("returned NULL"); } drizzle_test("drizzle_clone"); if ((clone= drizzle_clone(NULL, drizzle)) == NULL) { drizzle_test_error("drizzle_clone"); } drizzle_test("drizzle_free"); drizzle_free(clone); drizzle_test("drizzle_clone buffer"); if ((clone= drizzle_clone(&clone_buffer, drizzle)) == NULL) { drizzle_test_error("returned NULL"); } drizzle_test("drizzle_free buffer"); drizzle_free(clone); drizzle_test("drizzle_error"); if (strcmp(drizzle_error(drizzle), "")) { drizzle_test_error("error not empty"); } drizzle_test("drizzle_errno"); if (drizzle_errno(drizzle) != 0) { drizzle_test_error("errno not 0"); } drizzle_test("drizzle_error_code"); if (drizzle_error_code(drizzle) != 0) { drizzle_test_error("error_code not 0"); } drizzle_test("drizzle_sqlstate"); if (strcmp(drizzle_sqlstate(drizzle), "")) { drizzle_test_error("sqlstate not empty"); } /* @todo remove this option with new API. */ drizzle_remove_options(drizzle, DRIZZLE_FREE_OBJECTS); drizzle_test("drizzle_options"); if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED) { drizzle_test_error("expected options not set"); } drizzle_test("drizzle_add_options"); drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING); drizzle_test("drizzle_options"); if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING)) { drizzle_test_error("expected options not set"); } drizzle_test("drizzle_remove_options"); drizzle_remove_options(drizzle, DRIZZLE_NON_BLOCKING); drizzle_test("drizzle_options"); if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED) { drizzle_test_error("expected options not set"); } drizzle_test("drizzle_set_options"); drizzle_set_options(drizzle, DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING); drizzle_test("drizzle_options"); if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING)) { drizzle_test_error("expected options not set"); } drizzle_test("drizzle_set_options"); drizzle_set_options(drizzle, DRIZZLE_ALLOCATED); drizzle_test("drizzle_options"); if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED) { drizzle_test_error("expected options not set"); } drizzle_test("drizzle_set_timeout"); drizzle_set_timeout(drizzle, 1234); drizzle_test("drizzle_timeout"); if (drizzle_timeout(drizzle) != 1234) { drizzle_test_error("expected timeout not set"); } drizzle_test("drizzle_set_verbose"); drizzle_set_verbose(drizzle, DRIZZLE_VERBOSE_CRAZY); drizzle_test("drizzle_verbose"); if (drizzle_verbose(drizzle) != DRIZZLE_VERBOSE_CRAZY) { drizzle_test_error("expected verbose not set"); } drizzle_test("drizzle_set_log_fn"); drizzle_set_log_fn(drizzle, _log, NULL); drizzle_test("drizzle_set_event_watch_fn"); drizzle_set_event_watch_fn(drizzle, _event_watch, NULL); /* Create a listening connection to verify that event_watch_fn gets called. */ listen_con= drizzle_con_create(drizzle, NULL); assert(listen_con != NULL); drizzle_con_set_tcp(listen_con, "localhost", DRIZZLE_TEST_PORT); ret= drizzle_con_listen(listen_con); assert(ret == DRIZZLE_RETURN_OK); if (_event_watch_read_bits == 0) { drizzle_test_error("event_watch_fn not called to wait for connections"); } _event_watch_read_bits= 0; /* Attempt a non-blocking connection. */ drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING); con= drizzle_con_add_tcp(drizzle, NULL, "localhost", DRIZZLE_TEST_PORT, "user", "pw", "db", DRIZZLE_CON_NONE); assert(con != NULL); ret= drizzle_con_connect(con); assert(ret == DRIZZLE_RETURN_IO_WAIT || ret == DRIZZLE_RETURN_COULD_NOT_CONNECT); if (ret == DRIZZLE_RETURN_IO_WAIT && _event_watch_read_bits == 0 && _event_watch_write_bits == 0) { drizzle_test_error("event_watch_fn not called to wait for I/O"); } drizzle_con_free(con); _event_watch_read_bits= 0; _event_watch_write_bits= 0; drizzle_con_free(listen_con); drizzle_test("drizzle_free"); drizzle_free(drizzle); return 0; }
static int _drizzle_ConnectionObject_Initialize( _drizzle_ConnectionObject *self, PyObject *args, PyObject *kwargs) { drizzle_st *drizzle= NULL; drizzle_con_st *con= NULL; drizzle_return_t ret; PyObject *conv = NULL; char *host= NULL, *user= NULL, *passwd= NULL, *db= NULL, *unix_socket= NULL, *init_command= NULL; uint16_t port= 0; int connect_timeout= 0, client_flag= 0; static char *kwlist[]= { "host", "user", "passwd", "db", "port", "conv", "unix_socket", "connect_timeout", "init_command", "client_flag", NULL }; self->open= 0; self->converter= NULL; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssHOsisi:connect", kwlist, &host, &user, &passwd, &db, &port, &conv, &unix_socket, &connect_timeout, &init_command, &client_flag)) { return -1; } drizzle = drizzle_create(NULL); con = drizzle_con_create(drizzle, NULL); if (unix_socket != NULL) { drizzle_con_set_uds(con, unix_socket); } else { drizzle_con_set_tcp(con, host, port); } drizzle_con_set_auth(con, user, passwd); drizzle_con_set_db(con, db); drizzle_con_add_options(con, client_flag); ret = drizzle_con_connect(con); if (ret != DRIZZLE_RETURN_OK) { _drizzle_Exception(self); return -1; } if (!conv) { conv= PyDict_New(); } else { Py_INCREF(conv); } if (!conv) { return -1; } self->converter = conv; self->con= con; self->open= 1; return 0; }
int wait_connect(drizzle_con_st *con) { drizzle_return_t ret; int fd = 0, events = 0; PyObject *fileno, *state, *args, *res; while (1) { ret = drizzle_con_connect(con); if (ret == DRIZZLE_RETURN_OK) { return 1; } else if(ret == DRIZZLE_RETURN_IO_WAIT) { events = con->events; YDEBUG("IO_WAIT con:%p events:%d", con, events); if (external_io_wait) { fd = drizzle_con_fd(con); if (fd == -1){ return -1; } fileno = PyLong_FromLong((long)fd); if (fileno == NULL) { return -1; } state = PyLong_FromLong((long)events); if (state == NULL) { Py_DECREF(fileno); return -1; } args = PyTuple_Pack(2, fileno, state); if (args == NULL) { Py_DECREF(fileno); Py_DECREF(state); return -1; } YDEBUG("call external_io_wait ..."); res = PyObject_CallObject(external_io_wait, args); Py_DECREF(args); if (res == NULL) { return -1; } Py_XDECREF(res); ret = drizzle_con_set_revents(con, events); if (ret != DRIZZLE_RETURN_OK){ RDEBUG("ret %d:%s", ret, drizzle_error(drizzle)); return -1; } return 1; } else { YDEBUG("call drizzle_con_wait ..."); ret = drizzle_con_wait(drizzle); if (ret != DRIZZLE_RETURN_OK){ RDEBUG("ret %d:%s", ret, drizzle_error(drizzle)); return -1; } } } else { RDEBUG("ret %d:%s", ret, drizzle_error(drizzle)); return -1; } } return 1; }