switch_core_db.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2018, Anthony Minessale II <anthm@freeswitch.org>
  4. *
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Anthony Minessale II <anthm@freeswitch.org>
  21. * Portions created by the Initial Developer are Copyright (C)
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Chris Rienzo <chris@signalwire.com>
  26. * Andrey Volk <andrey@signalwire.com>
  27. *
  28. *
  29. * switch_core_db.c -- tests core db functions
  30. *
  31. */
  32. #include <switch.h>
  33. #include <stdlib.h>
  34. #include <test/switch_test.h>
  35. int max_rows = 150;
  36. int status = 0;
  37. int table_count_func(void *pArg, int argc, char **argv, char **columnNames){
  38. if (argc > 0) {
  39. status = atoi(argv[0]);
  40. }
  41. return -1;
  42. }
  43. FST_CORE_DB_BEGIN("./conf")
  44. {
  45. FST_SUITE_BEGIN(switch_core_db)
  46. {
  47. FST_SETUP_BEGIN()
  48. {
  49. }
  50. FST_SETUP_END()
  51. FST_TEARDOWN_BEGIN()
  52. {
  53. }
  54. FST_TEARDOWN_END()
  55. FST_TEST_BEGIN(test_switch_cache_db_execute_sql2str)
  56. {
  57. switch_cache_db_handle_t *dbh = NULL;
  58. char *dsn = "test_switch_cache_db_execute_sql2str.db";
  59. char res1[20] = "test";
  60. char res2[20] = "test";
  61. if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) == SWITCH_STATUS_SUCCESS) {
  62. switch_cache_db_execute_sql2str(dbh, "SELECT 1", (char *)&res1, 20, NULL);
  63. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "SELECT 1: %s\n", switch_str_nil(res1));
  64. switch_cache_db_execute_sql2str(dbh, "SELECT NULL", (char *)&res2, 20, NULL);
  65. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "SELECT NULL: %s\n", switch_str_nil(res2));
  66. }
  67. fst_check_string_equals(res1, "1");
  68. fst_check_string_equals(res2, "");
  69. }
  70. FST_TEST_END()
  71. FST_TEST_BEGIN(test_switch_cache_db_queue_manager_race)
  72. {
  73. int i;
  74. switch_sql_queue_manager_t *qm = NULL;
  75. switch_sql_queue_manager_init_name("TEST",
  76. &qm,
  77. 4,
  78. "test_switch_cache_db_queue_manager_race",
  79. SWITCH_MAX_TRANS,
  80. NULL, NULL, NULL, NULL);
  81. switch_sql_queue_manager_start(qm);
  82. switch_sql_queue_manager_push_confirm(qm, "DROP TABLE IF EXISTS t;", 0, SWITCH_TRUE);
  83. switch_sql_queue_manager_push_confirm(qm, "CREATE TABLE t (col1 INT);", 0, SWITCH_TRUE);
  84. for (i = 0; i < max_rows; i++) {
  85. switch_sql_queue_manager_push(qm, "INSERT INTO t (col1) VALUES (1);", 0, SWITCH_TRUE);
  86. }
  87. switch_sleep(1 * 1000 * 1000);
  88. switch_sql_queue_manager_execute_sql_callback(qm, "SELECT COUNT(col1) FROM t;", table_count_func, NULL);
  89. while (switch_sql_queue_manager_size(qm, 0)) {
  90. switch_cond_next();
  91. }
  92. switch_sql_queue_manager_stop(qm);
  93. switch_sql_queue_manager_destroy(&qm);
  94. fst_check_int_equals(status, max_rows);
  95. }
  96. FST_TEST_END()
  97. }
  98. FST_SUITE_END()
  99. }
  100. FST_CORE_END()