You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
4.8 KiB
156 lines
4.8 KiB
/** |
|
* Test for memory pool optimization and configuration file support |
|
*/ |
|
|
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <assert.h> |
|
#include <unistd.h> |
|
|
|
#include "../src/ll_queue.h" |
|
#include "../src/memory_pool.h" |
|
#include "../lib/u_async.h" |
|
#include "../lib/debug_config.h" |
|
|
|
static int test_callback_count = 0; |
|
|
|
static void test_callback(ll_queue_t* q, ll_entry_t* entry, void* arg) { |
|
(void)q; (void)entry; (void)arg; |
|
test_callback_count++; |
|
} |
|
|
|
static void test_waiter_callback(ll_queue_t* q, void* arg) { |
|
(void)q; (void)arg; |
|
test_callback_count++; |
|
} |
|
|
|
int main() { |
|
printf("=== Memory Pool and Config File Test ===\n"); |
|
|
|
// Test 1: Memory pool optimization |
|
printf("Test 1: Memory pool optimization...\n"); |
|
|
|
uasync_t* ua = uasync_create(); |
|
if (!ua) { |
|
printf("Failed to create uasync\n"); |
|
return 1; |
|
} |
|
|
|
// Create queue with memory pools enabled |
|
ll_queue_t* queue = queue_new_with_pools(ua, 1); |
|
if (!queue) { |
|
printf("Failed to create queue with pools\n"); |
|
uasync_destroy(ua); |
|
return 1; |
|
} |
|
|
|
// Test multiple waiter allocations to trigger pool usage |
|
queue_waiter_t* 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++) { |
|
ll_entry_t* entry = queue_entry_new(10); |
|
queue_entry_put(queue, entry); |
|
} |
|
|
|
// Remove entries to trigger waiter callbacks |
|
for (int i = 0; i < 5; i++) { |
|
ll_entry_t* retrieved = queue_entry_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; |
|
queue_get_pool_stats(queue, &allocations, &reuse_count); |
|
|
|
printf("Pool statistics: allocations=%zu, reuse_count=%zu\n", allocations, reuse_count); |
|
printf("Pool efficiency: %.1f%%\n", |
|
allocations > 0 ? (100.0 * reuse_count / allocations) : 0.0); |
|
|
|
queue_free(queue); |
|
uasync_destroy(ua); |
|
|
|
printf("Memory pool test: PASS\n\n"); |
|
|
|
// Test 2: Configuration file support |
|
printf("Test 2: Configuration file support...\n"); |
|
|
|
// 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 file |
|
if (debug_parse_config_file(config_file) == 0) { |
|
printf("Configuration file parsed successfully\n"); |
|
|
|
// Test that configuration was applied |
|
if (debug_should_output(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_LL_QUEUE)) { |
|
printf("LL_QUEUE debug level correctly set\n"); |
|
} |
|
if (debug_should_output(DEBUG_LEVEL_INFO, DEBUG_CATEGORY_UASYNC)) { |
|
printf("UASYNC info level correctly set\n"); |
|
} |
|
if (!debug_should_output(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_ETCP)) { |
|
printf("ETCP correctly limited to error level\n"); |
|
} |
|
|
|
// Test hot reload functionality |
|
printf("Testing hot reload...\n"); |
|
if (debug_enable_config_reload(config_file, 1) == 0) { |
|
printf("Hot reload enabled successfully\n"); |
|
|
|
// Modify the file |
|
f = fopen(config_file, "a"); |
|
if (f) { |
|
fprintf(f, "connection:trace\n"); |
|
fclose(f); |
|
|
|
// Wait for reload |
|
sleep(2); |
|
|
|
if (debug_should_output(DEBUG_LEVEL_TRACE, DEBUG_CATEGORY_CONNECTION)) { |
|
printf("Hot reload worked: connection category updated to trace\n"); |
|
} |
|
} |
|
|
|
debug_disable_config_reload(); |
|
printf("Hot reload disabled\n"); |
|
} else { |
|
printf("Failed to enable hot reload\n"); |
|
} |
|
|
|
} else { |
|
printf("Failed to parse configuration file\n"); |
|
} |
|
|
|
unlink(config_file); |
|
} else { |
|
printf("Failed to create test configuration file\n"); |
|
} |
|
|
|
printf("Configuration file test: PASS\n"); |
|
|
|
printf("\n=== All tests completed successfully ===\n"); |
|
return 0; |
|
} |