133 lines
4.0 KiB
C
133 lines
4.0 KiB
C
/**
|
|
* 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 "../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() {
|
|
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
|
|
struct ll_queue* queue = queue_new(ua, 0);
|
|
if (!queue) {
|
|
printf("Failed to create queue with pools\n");
|
|
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_data_new(10);
|
|
queue_data_put(queue, data, i); // Используем ID = i
|
|
}
|
|
|
|
// Remove entries to trigger waiter callbacks
|
|
for (int i = 0; i < 5; i++) {
|
|
void* retrieved = queue_data_get(queue);
|
|
if (retrieved) {
|
|
queue_data_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;
|
|
|
|
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, 0);
|
|
|
|
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 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) {
|
|
printf("Configuration 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");
|
|
}
|
|
|
|
printf("Note: Hot reload functionality not implemented in current debug system\n");
|
|
|
|
} else {
|
|
printf("Failed to parse configuration\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;
|
|
} |