/** * Test for memory pool optimization and configuration file support */ #include "test_utils.h" #include #include #include #include #include #include "../lib/ll_queue.h" #include "../lib/memory_pool.h" #include "../lib/u_async.h" #include "../lib/debug_config.h" static int test_callback_count = 0; static void test_callback(struct ll_queue* q, void* data, void* arg) { (void)q; (void)data; (void)arg; test_callback_count++; } static void test_waiter_callback(struct ll_queue* q, void* arg) { (void)q; (void)arg; test_callback_count++; } int main() { debug_config_init(); debug_set_level(DEBUG_LEVEL_TRACE); debug_set_categories(DEBUG_CATEGORY_ALL); DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "=== Memory Pool and Config File Test ==="); // Test 1: Memory pool optimization DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "Test 1: Memory pool optimization..."); uasync_t* ua = uasync_create(); if (!ua) { DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to create uasync"); return 1; } // Create queue with memory pools enabled struct ll_queue* queue = queue_new(ua, 0,"q1"); if (!queue) { DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to create queue with pools"); uasync_destroy(ua, 0); return 1; } // Test multiple waiter allocations to trigger pool usage struct queue_waiter* waiters[10]; for (int i = 0; i < 10; i++) { waiters[i] = queue_wait_threshold(queue, i * 2, 0, test_waiter_callback, NULL); } // Add some entries and trigger waiters for (int i = 0; i < 5; i++) { void* data = queue_entry_new(10); queue_data_put(queue, data); // Используем ID = i } // Remove entries to trigger waiter callbacks for (int i = 0; i < 5; i++) { void* retrieved = queue_data_get(queue); if (retrieved) { queue_entry_free(retrieved); } } // Cancel remaining waiters for (int i = 0; i < 10; i++) { if (waiters[i]) { queue_cancel_wait(queue, waiters[i]); } } // Get pool statistics size_t allocations, reuse_count; DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "Pool statistics: allocations=%zu, reuse_count=%zu", allocations, reuse_count); DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "Pool efficiency: %.1f%%", allocations > 0 ? (100.0 * reuse_count / allocations) : 0.0); queue_free(queue); uasync_destroy(ua, 0); DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "Memory pool test: PASS"); // Test 2: Configuration file support DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Test 2: Configuration file support..."); // Create a test configuration file const char* config_file = "/tmp/debug_test.conf"; FILE* f = fopen(config_file, "w"); if (f) { fprintf(f, "# Test debug configuration file\n"); fprintf(f, "ll_queue:debug\n"); fprintf(f, "uasync:info\n"); fprintf(f, "memory:warn\n"); fprintf(f, "# This is a comment\n"); fprintf(f, "etcp:error\n"); fclose(f); // Parse the configuration string (we don't have file parsing, so use string parsing) const char* config_string = "ll_queue:debug,uasync:info,memory:warn,etcp:error"; if (debug_parse_config(config_string) == 0) { DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Configuration parsed successfully"); // Test that configuration was applied if (debug_should_output(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_LL_QUEUE)) { DEBUG_INFO(DEBUG_CATEGORY_LL_QUEUE, "LL_QUEUE debug level correctly set"); } if (debug_should_output(DEBUG_LEVEL_INFO, DEBUG_CATEGORY_UASYNC)) { DEBUG_INFO(DEBUG_CATEGORY_UASYNC, "UASYNC info level correctly set"); } if (!debug_should_output(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_ETCP)) { DEBUG_INFO(DEBUG_CATEGORY_ETCP, "ETCP correctly limited to error level"); } DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Note: Hot reload functionality not implemented in current debug system"); } else { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to parse configuration"); } test_unlink(config_file); } else { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to create test configuration file"); } DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Configuration file test: PASS"); DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "=== All tests completed successfully ==="); return 0; }