]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[Core] Wait sql threads to spin up. Fixes module_exists, show_codec commands.
authorAndrey Volk <andywolk@gmail.com>
Thu, 9 Jan 2020 18:48:34 +0000 (22:48 +0400)
committerAndrey Volk <andywolk@gmail.com>
Wed, 6 May 2020 22:26:22 +0000 (02:26 +0400)
src/switch_core_sqldb.c
tests/unit/switch_core_db.c

index 7f5c2aff525bf61b471956967e2e5baed9a500b3..98c50c0d8d2864aadf761fcbc61e953c21708545 100644 (file)
@@ -1616,6 +1616,7 @@ struct switch_sql_queue_manager {
        uint32_t numq;
        char *dsn;
        switch_thread_t *thread;
+       int thread_initiated;
        int thread_running;
        switch_thread_cond_t *cond;
        switch_mutex_t *cond_mutex;
@@ -1899,8 +1900,15 @@ SWITCH_DECLARE(switch_status_t) switch_sql_queue_manager_start(switch_sql_queue_
                switch_threadattr_create(&thd_attr, qm->pool);
                switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
                switch_threadattr_priority_set(thd_attr, SWITCH_PRI_NORMAL);
-               switch_thread_create(&qm->thread, thd_attr, switch_user_sql_thread, qm, qm->pool);
-               return SWITCH_STATUS_SUCCESS;
+               if (switch_thread_create(&qm->thread, thd_attr, switch_user_sql_thread, qm, qm->pool) == SWITCH_STATUS_SUCCESS) {
+                       while (!qm->thread_initiated) {
+                               switch_cond_next();
+                       }
+
+                       if (qm->event_db) {
+                               return SWITCH_STATUS_SUCCESS;
+                       }
+               }
        }
 
        return SWITCH_STATUS_FALSE;
@@ -2274,11 +2282,10 @@ static void *SWITCH_THREAD_FUNC switch_user_sql_thread(switch_thread_t *thread,
 
        if (!qm->event_db) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "%s Error getting db handle\n", qm->name);
+               qm->thread_initiated = 1;
                return NULL;
        }
 
-       qm->thread_running = 1;
-
        switch_mutex_lock(qm->cond_mutex);
 
        switch (qm->event_db->type) {
@@ -2296,6 +2303,8 @@ static void *SWITCH_THREAD_FUNC switch_user_sql_thread(switch_thread_t *thread,
                break;
        }
 
+       qm->thread_initiated = 1;
+       qm->thread_running = 1;
 
        while (qm->thread_running == 1) {
                uint32_t i, lc;
index 244ab38398d80da41db7812cdc104f62a04dbd3d..5ebf41c8ab8cbdc48cb209e5341e6210aa4638ab 100644 (file)
 
 #include <test/switch_test.h>
 
+int max_rows = 150;
+
+int status = 0;
+
+int table_count_func(void *pArg, int argc, char **argv, char **columnNames){
+       if (argc > 0) {
+               status = atoi(argv[0]);
+       }
+
+       return -1;
+}
+
 FST_CORE_DB_BEGIN("./conf")
 {
        FST_SUITE_BEGIN(switch_core_db)
@@ -69,6 +81,44 @@ FST_CORE_DB_BEGIN("./conf")
                        fst_check_string_equals(res2, "");
                }
                FST_TEST_END()
+
+               FST_TEST_BEGIN(test_switch_cache_db_queue_manager_race)
+               {
+                       int i;
+                       switch_sql_queue_manager_t *qm = NULL;
+
+                       switch_sql_queue_manager_init_name("TEST",
+                               &qm,
+                               4,
+                               "test_switch_cache_db_queue_manager_race",
+                               SWITCH_MAX_TRANS,
+                               NULL, NULL, NULL, NULL);
+
+                       switch_sql_queue_manager_start(qm);
+
+                       switch_sql_queue_manager_push_confirm(qm, "DROP TABLE IF EXISTS t;", 0, SWITCH_TRUE);
+                       switch_sql_queue_manager_push_confirm(qm, "CREATE TABLE t (col1 INT);", 0, SWITCH_TRUE);
+
+                       for (i = 0; i < max_rows; i++) {
+                               switch_sql_queue_manager_push(qm, "INSERT INTO t (col1) VALUES (1);", 0, SWITCH_TRUE);
+                       }
+
+                       switch_sleep(1 * 1000 * 1000);
+
+                       switch_sql_queue_manager_execute_sql_callback(qm, "SELECT COUNT(col1) FROM t;", table_count_func, NULL);
+
+                       while (switch_sql_queue_manager_size(qm, 0)) {
+                               switch_cond_next();
+                       }
+
+                       switch_sql_queue_manager_stop(qm);
+                       switch_sql_queue_manager_destroy(&qm);
+
+                       fst_check_int_equals(status, max_rows);
+               }
+               FST_TEST_END()
+
+
        }
        FST_SUITE_END()
 }