| | | 1 | | #include <stdarg.h> |
| | | 2 | | #include <stddef.h> |
| | | 3 | | #include <setjmp.h> |
| | | 4 | | #include <cmocka.h> |
| | | 5 | | #include <string.h> |
| | | 6 | | #include <stdio.h> |
| | | 7 | | #include <stdlib.h> |
| | | 8 | | #include <pthread.h> |
| | | 9 | | |
| | | 10 | | // PostgreSQL'i tamamen devre dışı bırak |
| | | 11 | | #define LIBPQ_FE_H |
| | | 12 | | #define PQconninfoOption void |
| | | 13 | | #define PGconn void |
| | | 14 | | #define PGresult void |
| | | 15 | | #define ConnStatusType int |
| | | 16 | | #define ExecStatusType int |
| | | 17 | | #define CONNECTION_OK 0 |
| | | 18 | | #define CONNECTION_BAD 1 |
| | | 19 | | #define PGRES_COMMAND_OK 1 |
| | | 20 | | #define PGRES_TUPLES_OK 2 |
| | | 21 | | #define PGRES_FATAL_ERROR 3 |
| | | 22 | | |
| | | 23 | | #include "db_connection.h" |
| | | 24 | | |
| | | 25 | | // Logger mock'ları |
| | 0 | 26 | | void LOG_INFO(const char *format, ...) {} |
| | | 27 | | |
| | 0 | 28 | | void LOG_ERROR(const char *format, ...) {} |
| | | 29 | | |
| | | 30 | | /* ==================================================================== |
| | | 31 | | * MOCK FONKSİYONLAR |
| | | 32 | | * ==================================================================== */ |
| | | 33 | | |
| | | 34 | | static int mock_connection_status = CONNECTION_OK; |
| | | 35 | | static char mock_error_message[256] = ""; |
| | | 36 | | static int mock_connect_fail = 0; |
| | | 37 | | static void *mock_conn_ptr = (void *)0x12345678; |
| | | 38 | | |
| | | 39 | | static int mock_PQconnectdb_called = 0; |
| | | 40 | | static int mock_PQfinish_called = 0; |
| | | 41 | | static int mock_PQstatus_called = 0; |
| | | 42 | | |
| | 11 | 43 | | void *PQconnectdb(const char *conninfo) { |
| | 11 | 44 | | mock_PQconnectdb_called++; |
| | | 45 | | |
| | 11 | 46 | | if (mock_connect_fail) { |
| | 0 | 47 | | return NULL; |
| | | 48 | | } |
| | | 49 | | |
| | 11 | 50 | | return mock_conn_ptr; |
| | | 51 | | } |
| | | 52 | | |
| | 11 | 53 | | void PQfinish(void *conn) { |
| | 11 | 54 | | mock_PQfinish_called++; |
| | 11 | 55 | | } |
| | | 56 | | |
| | 15 | 57 | | int PQstatus(const void *conn) { |
| | 15 | 58 | | mock_PQstatus_called++; |
| | 15 | 59 | | return mock_connection_status; |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | char *PQerrorMessage(const void *conn) { |
| | 0 | 63 | | return mock_error_message; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /* ==================================================================== |
| | | 67 | | * TEST SETUP |
| | | 68 | | * ==================================================================== */ |
| | | 69 | | |
| | 5 | 70 | | static void reset_mocks(void) { |
| | 5 | 71 | | mock_connection_status = CONNECTION_OK; |
| | 5 | 72 | | mock_error_message[0] = '\0'; |
| | 5 | 73 | | mock_connect_fail = 0; |
| | 5 | 74 | | mock_PQconnectdb_called = 0; |
| | 5 | 75 | | mock_PQfinish_called = 0; |
| | 5 | 76 | | mock_PQstatus_called = 0; |
| | 5 | 77 | | } |
| | | 78 | | |
| | 5 | 79 | | static void setup_default_config(DatabaseConfig *cfg) { |
| | 5 | 80 | | memset(cfg, 0, sizeof(DatabaseConfig)); |
| | 5 | 81 | | strcpy(cfg->host, "localhost"); |
| | 5 | 82 | | cfg->port = 5432; |
| | 5 | 83 | | strcpy(cfg->dbname, "testdb"); |
| | 5 | 84 | | strcpy(cfg->user, "testuser"); |
| | 5 | 85 | | strcpy(cfg->password, "testpass"); |
| | 5 | 86 | | strcpy(cfg->sslmode, "disable"); |
| | 5 | 87 | | cfg->connect_timeout = 5; |
| | 5 | 88 | | cfg->pool_min = 2; |
| | 5 | 89 | | cfg->pool_max = 5; |
| | 5 | 90 | | } |
| | | 91 | | |
| | | 92 | | /* ==================================================================== |
| | | 93 | | * TEST CASES |
| | | 94 | | * ==================================================================== */ |
| | | 95 | | |
| | 1 | 96 | | static void test_pool_init_success(void **state) { |
| | 1 | 97 | | reset_mocks(); |
| | | 98 | | DatabaseConfig cfg; |
| | 1 | 99 | | setup_default_config(&cfg); |
| | 1 | 100 | | ConnectionPool *pool = db_pool_init(&cfg, 0, 0); |
| | 1 | 101 | | assert_non_null(pool); |
| | 1 | 102 | | db_pool_destroy(); |
| | 1 | 103 | | } |
| | | 104 | | |
| | 1 | 105 | | static void test_acquire_success(void **state) { |
| | 1 | 106 | | reset_mocks(); |
| | | 107 | | DatabaseConfig cfg; |
| | 1 | 108 | | setup_default_config(&cfg); |
| | 1 | 109 | | ConnectionPool *pool = db_pool_init(&cfg, 0, 0); |
| | 1 | 110 | | DBConnection *conn = db_pool_acquire(); |
| | 1 | 111 | | assert_non_null(conn); |
| | 1 | 112 | | db_pool_release(conn); |
| | 1 | 113 | | db_pool_destroy(); |
| | 1 | 114 | | } |
| | | 115 | | |
| | 1 | 116 | | static void test_acquire_all_and_exhaust(void **state) { |
| | 1 | 117 | | reset_mocks(); |
| | | 118 | | DatabaseConfig cfg; |
| | 1 | 119 | | setup_default_config(&cfg); |
| | 1 | 120 | | cfg.pool_max = 3; |
| | 1 | 121 | | ConnectionPool *pool = db_pool_init(&cfg, 0, 0); |
| | | 122 | | DBConnection *conns[5]; |
| | | 123 | | |
| | 4 | 124 | | for (int i = 0; i < cfg.pool_max; i++) { |
| | 3 | 125 | | conns[i] = db_pool_acquire(); |
| | 3 | 126 | | assert_non_null(conns[i]); |
| | | 127 | | } |
| | | 128 | | |
| | 1 | 129 | | DBConnection *exhausted = db_pool_acquire(); |
| | 1 | 130 | | assert_null(exhausted); |
| | | 131 | | |
| | 4 | 132 | | for (int i = 0; i < cfg.pool_max; i++) { |
| | 3 | 133 | | db_pool_release(conns[i]); |
| | | 134 | | } |
| | | 135 | | |
| | 1 | 136 | | db_pool_destroy(); |
| | 1 | 137 | | } |
| | | 138 | | |
| | 1 | 139 | | static void test_metrics(void **state) { |
| | 1 | 140 | | reset_mocks(); |
| | | 141 | | DatabaseConfig cfg; |
| | 1 | 142 | | setup_default_config(&cfg); |
| | 1 | 143 | | ConnectionPool *pool = db_pool_init(&cfg, 0, 0); |
| | 1 | 144 | | PoolMetrics metrics = db_pool_get_metrics(); |
| | 1 | 145 | | DBConnection *conn = db_pool_acquire(); |
| | 1 | 146 | | assert_non_null(conn); |
| | 1 | 147 | | metrics = db_pool_get_metrics(); |
| | 1 | 148 | | printf("\nDEBUG: acquire_cnt=%zu, used_cnt=%d\n", metrics.acquire_cnt, metrics.used_cnt); |
| | 1 | 149 | | db_pool_release(conn); |
| | 1 | 150 | | metrics = db_pool_get_metrics(); |
| | 1 | 151 | | printf("DEBUG: release_cnt=%zu, used_cnt=%d\n", metrics.release_cnt, metrics.used_cnt); |
| | 1 | 152 | | db_pool_destroy(); |
| | 1 | 153 | | } |
| | | 154 | | |
| | 1 | 155 | | static void test_release_null(void **state) { |
| | 1 | 156 | | reset_mocks(); |
| | | 157 | | DatabaseConfig cfg; |
| | 1 | 158 | | setup_default_config(&cfg); |
| | 1 | 159 | | ConnectionPool *pool = db_pool_init(&cfg, 0, 0); |
| | 1 | 160 | | db_pool_release(NULL); |
| | 1 | 161 | | db_pool_destroy(); |
| | 1 | 162 | | } |
| | | 163 | | |
| | | 164 | | /* ==================================================================== |
| | | 165 | | * MAIN |
| | | 166 | | * ==================================================================== */ |
| | | 167 | | |
| | 1 | 168 | | int main(void) { |
| | 1 | 169 | | const struct CMUnitTest tests[] = { |
| | | 170 | | cmocka_unit_test(test_pool_init_success), |
| | | 171 | | cmocka_unit_test(test_acquire_success), |
| | | 172 | | cmocka_unit_test(test_acquire_all_and_exhaust), |
| | | 173 | | cmocka_unit_test(test_metrics), |
| | | 174 | | cmocka_unit_test(test_release_null), |
| | | 175 | | }; |
| | 1 | 176 | | return cmocka_run_group_tests(tests, NULL, NULL); |
| | | 177 | | } |