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.
 
 
 
 
 
 

146 lines
5.1 KiB

/**
* Test for memory pool optimization and configuration file support
*/
#include "test_utils.h"
#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"
#include "../lib/mem.h"
static int test_callback_count = 0;
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_SYS, "=== Memory Pool and Config File Test ===");
/* Любой размер блока, включая невыровненный хвост, допустим для malloc/realloc/free. */
for (uint32_t size = 1; size <= 17; size++) {
unsigned char* p = u_malloc(size);
assert(p);
memset(p, 0x5a, size);
u_check(p, "odd-size allocation", __FILE__);
p = u_realloc(p, size + 3);
assert(p);
for (uint32_t i = 0; i < size; i++) assert(p[i] == 0x5a);
u_free(p);
struct memory_pool* pool = memory_pool_init(sizeof(void*) + size, "odd-size pool");
assert(pool);
void* obj = memory_pool_alloc(pool);
assert(obj);
memset(obj, 0x5a, pool->object_size);
memory_pool_free(pool, obj);
obj = memory_pool_alloc(pool);
assert(obj);
memory_pool_free(pool, obj);
memory_pool_destroy(pool);
}
DEBUG_INFO(DEBUG_CATEGORY_SYS, "Unaligned tail canary/realloc: PASS");
// Test 1: Memory pool optimization
DEBUG_INFO(DEBUG_CATEGORY_SYS, "Test 1: Memory pool optimization...");
uasync_t* ua = uasync_create();
if (!ua) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "Failed to create uasync");
return 1;
}
// Create queue with memory pools enabled
struct ll_queue* queue = queue_new(ua, 0, 0, 0,"q1");
if (!queue) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "Failed to create queue with pools");
uasync_destroy(ua, 0);
return 1;
}
queue_set_threshold(queue, 2, 0);
// Test multiple waiters with new linked-list backpressure
struct queue_waiter_handle handles[10] = {0};
for (int i = 0; i < 10; i++) {
queue_waiter_wait(queue, &handles[i], test_waiter_callback, NULL); // queue empty => immediate callback
}
// Add some entries — waiters already fired, none queued
for (int i = 0; i < 5; i++) {
void* data = queue_entry_new(10);
queue_data_put(queue, data);
}
// Remove entries
for (int i = 0; i < 5; i++) {
void* retrieved = queue_data_get(queue);
if (retrieved) queue_entry_free(retrieved);
}
// Cancel (safe call even if not waiting)
for (int i = 0; i < 10; i++) {
queue_waiter_cancel(queue, &handles[i]);
}
queue_free(queue);
uasync_destroy(ua, 0);
DEBUG_INFO(DEBUG_CATEGORY_SYS, "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_SYS)) {
DEBUG_INFO(DEBUG_CATEGORY_SYS, "LL_QUEUE debug level correctly set");
}
if (debug_should_output(DEBUG_LEVEL_INFO, DEBUG_CATEGORY_SYS)) {
DEBUG_INFO(DEBUG_CATEGORY_SYS, "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_SYS, "=== All tests completed successfully ===");
return 0;
}