< Summary - Backend C Tests - Coverage Report (WSL)

Information
Class: test_db_pool_fifo_c
Assembly: src.backend.tests.unit.database
File(s): ./src/backend/tests/unit/database/test_db_pool_fifo.c
Line coverage
100%
Covered lines: 156
Uncovered lines: 0
Coverable lines: 156
Total lines: 280
Line coverage: 100%
Branch coverage
57%
Covered branches: 46
Total branches: 80
Branch coverage: 57.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 2/18/2026 - 10:50:55 PM Line coverage: 100% (156/156) Branch coverage: 57.5% (46/80) Total lines: 280 2/18/2026 - 10:50:55 PM Line coverage: 100% (156/156) Branch coverage: 57.5% (46/80) Total lines: 280

File(s)

./src/backend/tests/unit/database/test_db_pool_fifo.c

#LineLine coverage
 1#include <stdio.h>
 2#include <stdlib.h>
 3#include <string.h>
 4#include <pthread.h>
 5#include <unistd.h>
 6#include <assert.h>
 7#include "db_connection.h"
 8
 9// ------------------------------------------------------------
 10// MOCK IMPLEMENTASYONLAR - db_connection.c'deki static fonksiyonlar
 11// ------------------------------------------------------------
 12
 13// Stack operasyonları için mock
 14#define MAX_POOL_SIZE 100
 15
 16static int mock_free_stack[MAX_POOL_SIZE];
 17static int mock_free_top = -1;
 18static int mock_used_stack[MAX_POOL_SIZE];
 19static int mock_used_top = -1;
 20
 21// Mock stack_push - gerçek implementasyonun aynısı
 2222int mock_stack_push(int stack[], int *top, int value) {
 2223  if (*top >= MAX_POOL_SIZE - 1) return -1;
 24
 2225  stack[++(*top)] = value;
 2226  return 0;
 27}
 28
 29// Mock stack_pop
 830int mock_stack_pop(int stack[], int *top) {
 831  if (*top < 0) return -1;
 32
 733  return stack[(*top)--];
 34}
 35
 36// ------------------------------------------------------------
 37// MOCK POOL STRUCT
 38// ------------------------------------------------------------
 39typedef struct db_conn_s {
 40  PGconn *pg_conn;
 41  bool in_use;
 42  int index;
 43} db_conn_t;
 44
 45typedef struct ConnectionPool {
 46  db_conn_t *connections;
 47  int free_stack[MAX_POOL_SIZE];
 48  int free_top;
 49  int used_stack[MAX_POOL_SIZE];
 50  int used_top;
 51  int size;
 52  int used_cnt;
 53  pthread_mutex_t lock;
 54  pthread_cond_t cond;
 55  int timeout_ms;
 56  PoolExhaustionPolicy policy;
 57} db_pool_t;
 58
 59// Mock global pool
 60static db_pool_t mock_pool;
 61
 62// ------------------------------------------------------------
 63// MOCK DB CONNECTION FUNCTIONS
 64// ------------------------------------------------------------
 65
 66// db_pool_acquire'in mock versiyonu
 867DBConnection *mock_db_pool_acquire(void) {
 868  if (mock_pool.free_top < 0) {
 69    // Pool boş, policy'e göre davran
 370    if (mock_pool.policy == FAIL_FAST) {
 171      return NULL;
 72    }
 73
 74    // QUEUE_REQUESTS için basit mock
 275    return NULL;
 76  }
 77
 578  int idx = mock_stack_pop(mock_pool.free_stack, &mock_pool.free_top);
 79
 580  if (idx < 0) return NULL;
 81
 582  mock_pool.connections[idx].in_use = true;
 583  mock_stack_push(mock_pool.used_stack, &mock_pool.used_top, idx);
 584  mock_pool.used_cnt++;
 585  return (DBConnection *)&mock_pool.connections[idx];
 86}
 87
 88// db_pool_release'in mock versiyonu
 589void mock_db_pool_release(DBConnection *conn) {
 590  db_conn_t *db_conn = (db_conn_t *)conn;
 591  int idx = db_conn->index;
 592  db_conn->in_use = false;
 593  mock_pool.used_cnt--;
 94  // Used stack'ten çıkar
 95  int i;
 96
 597  for (i = 0; i <= mock_pool.used_top; i++) {
 598    if (mock_pool.used_stack[i] == idx) {
 799      for (int j = i; j < mock_pool.used_top; j++) {
 2100        mock_pool.used_stack[j] = mock_pool.used_stack[j + 1];
 101      }
 102
 5103      mock_pool.used_top--;
 5104      break;
 105    }
 106  }
 107
 108  // Free stack'e ekle
 5109  mock_stack_push(mock_pool.free_stack, &mock_pool.free_top, idx);
 5110}
 111
 112// db_pool_init'in mock versiyonu
 4113void mock_db_pool_init(int pool_size, PoolExhaustionPolicy policy) {
 4114  memset(&mock_pool, 0, sizeof(mock_pool));
 4115  mock_pool.size = pool_size;
 4116  mock_pool.policy = policy;
 4117  mock_pool.free_top = -1;
 4118  mock_pool.used_top = -1;
 4119  mock_pool.used_cnt = 0;
 4120  mock_pool.connections = calloc(pool_size, sizeof(db_conn_t));
 121
 13122  for (int i = 0; i < pool_size; i++) {
 9123    mock_pool.connections[i].index = i;
 9124    mock_pool.connections[i].in_use = false;
 9125    mock_pool.connections[i].pg_conn = NULL;
 9126    mock_stack_push(mock_pool.free_stack, &mock_pool.free_top, i);
 127  }
 4128}
 129
 130// db_pool_destroy'in mock versiyonu
 4131void mock_db_pool_destroy(void) {
 4132  if (mock_pool.connections) {
 4133    free(mock_pool.connections);
 4134    mock_pool.connections = NULL;
 135  }
 136
 4137  mock_pool.free_top = -1;
 4138  mock_pool.used_top = -1;
 4139  mock_pool.used_cnt = 0;
 4140}
 141
 142// ------------------------------------------------------------
 143// TEST FONKSIYONLARI
 144// ------------------------------------------------------------
 145
 2146void *waiting_worker(void *arg) {
 2147  DBConnection *c = mock_db_pool_acquire();
 2148  *(DBConnection **)arg = c;
 2149  return NULL;
 150}
 151
 1152void test_fifo_logic() {
 1153  printf("Testing FIFO logic...\n");
 154  // Pool'u başlat
 1155  mock_db_pool_init(1, QUEUE_REQUESTS);
 156  // Mock connection
 1157  mock_pool.connections[0].pg_conn = (PGconn *)0xDEADBEEF;
 1158  mock_pool.connections[0].in_use = false;
 159  // Free stack'i temizle ve baştan ekle
 1160  mock_pool.free_top = -1;
 1161  mock_stack_push(mock_pool.free_stack, &mock_pool.free_top, 0);
 162  // Thread A acquires the only connection
 1163  DBConnection *c1 = mock_db_pool_acquire();
 1164  assert(c1 != NULL);
 1165  assert(mock_pool.used_cnt == 1);
 166  // Thread B tries to acquire and should block (mock'ta blocking yok, NULL döner)
 167  pthread_t t1, t2;
 1168  DBConnection *res1 = NULL, *res2 = NULL;
 1169  pthread_create(&t1, NULL, waiting_worker, &res1);
 1170  usleep(100000);
 171  // Thread C tries to acquire and should block behind B
 1172  pthread_create(&t2, NULL, waiting_worker, &res2);
 1173  usleep(100000);
 174  // Release connection
 1175  mock_db_pool_release(c1);
 1176  pthread_join(t1, NULL);
 1177  pthread_join(t2, NULL);
 1178  mock_db_pool_destroy();
 1179  printf("FIFO Logic Test Passed\n");
 1180}
 181
 1182void test_stack_operations() {
 1183  printf("Testing stack operations...\n");
 184  int stack[10];
 1185  int top = -1;
 186  // Push test
 1187  int r1 = mock_stack_push(stack, &top, 5);
 1188  assert(r1 == 0);
 1189  assert(top == 0);
 1190  assert(stack[0] == 5);
 1191  r1 = mock_stack_push(stack, &top, 10);
 1192  assert(r1 == 0);
 1193  assert(top == 1);
 1194  assert(stack[1] == 10);
 195  // Pop test
 1196  int val = mock_stack_pop(stack, &top);
 1197  assert(val == 10);
 1198  assert(top == 0);
 1199  val = mock_stack_pop(stack, &top);
 1200  assert(val == 5);
 1201  assert(top == -1);
 202  // Empty pop test
 1203  val = mock_stack_pop(stack, &top);
 1204  assert(val == -1);
 1205  printf("Stack Operations Test Passed\n");
 1206}
 207
 1208void test_pool_initialization() {
 1209  printf("Testing pool initialization...\n");
 1210  mock_db_pool_init(5, FAIL_FAST);
 1211  assert(mock_pool.size == 5);
 1212  assert(mock_pool.free_top == 4);  // 0-4 arası 5 eleman
 1213  assert(mock_pool.used_top == -1);
 1214  assert(mock_pool.used_cnt == 0);
 215
 216  // Free stack kontrolü
 6217  for (int i = 0; i <= mock_pool.free_top; i++) {
 5218    assert(mock_pool.free_stack[i] == i);
 219  }
 220
 1221  mock_db_pool_destroy();
 1222  printf("Pool Initialization Test Passed\n");
 1223}
 224
 1225void test_acquire_release() {
 1226  printf("Testing acquire/release cycle...\n");
 1227  mock_db_pool_init(2, FAIL_FAST);
 228  // İki connection al
 1229  DBConnection *c1 = mock_db_pool_acquire();
 1230  DBConnection *c2 = mock_db_pool_acquire();
 1231  assert(c1 != NULL);
 1232  assert(c2 != NULL);
 1233  assert(mock_pool.used_cnt == 2);
 1234  assert(mock_pool.free_top == -1);  // Tüm connection'lar kullanımda
 235  // Birini release et
 1236  mock_db_pool_release(c1);
 1237  assert(mock_pool.used_cnt == 1);
 1238  assert(mock_pool.free_top == 0);  // 1 connection free
 239  // Tekrar acquire et
 1240  DBConnection *c3 = mock_db_pool_acquire();
 1241  assert(c3 != NULL);
 1242  assert(mock_pool.used_cnt == 2);
 1243  assert(mock_pool.free_top == -1);
 1244  mock_db_pool_release(c2);
 1245  mock_db_pool_release(c3);
 1246  mock_db_pool_destroy();
 1247  printf("Acquire/Release Test Passed\n");
 1248}
 249
 1250void test_fail_fast() {
 1251  printf("Testing FAIL_FAST policy...\n");
 1252  mock_db_pool_init(1, FAIL_FAST);
 1253  DBConnection *c1 = mock_db_pool_acquire();
 1254  assert(c1 != NULL);
 255  // İkinci acquire hemen NULL dönmeli
 1256  DBConnection *c2 = mock_db_pool_acquire();
 1257  assert(c2 == NULL);
 1258  mock_db_pool_release(c1);
 1259  mock_db_pool_destroy();
 1260  printf("FAIL_FAST Test Passed\n");
 1261}
 262
 263// ------------------------------------------------------------
 264// MAIN
 265// ------------------------------------------------------------
 266
 1267int main() {
 1268  printf("\n========================================\n");
 1269  printf("   DB POOL FIFO TESTS - MOCK VERSION\n");
 1270  printf("========================================\n\n");
 1271  test_stack_operations();
 1272  test_pool_initialization();
 1273  test_acquire_release();
 1274  test_fail_fast();
 1275  test_fifo_logic();
 1276  printf("\n========================================\n");
 1277  printf("   ALL TESTS PASSED!\n");
 1278  printf("========================================\n\n");
 1279  return 0;
 280}

Methods/Properties