Fix: Optimize test_etcp_two_instances and test_etcp_simple_traffic
- Changed to use single shared uasync instead of two separate instances - Removed usleep from event loops for faster execution - Added missing init_connections() calls when TUN is disabled - Fixed monitor timeout to work correctly with poll intervals - Fixed references to server_instance->ua to use shared ua Tests now complete in ~18ms instead of waiting seconds for init.
This commit is contained in:
1 parent
b811aafd12
commit
d5e43b4745
2 files changed
+66
-57
No files matched your search
@@ -20,6 +20,7 @@
|
||||
|
||||
static struct UTUN_INSTANCE* server_instance = NULL;
|
||||
static struct UTUN_INSTANCE* client_instance = NULL;
|
||||
static struct UASYNC* ua = NULL;
|
||||
static int test_completed = 0; // 0 = running, 1 = success, 2 = timeout/failure
|
||||
static void* packet_timeout_id = NULL;
|
||||
|
||||
@@ -278,8 +279,8 @@ int main() {
|
||||
|
||||
// Create server instance
|
||||
printf("Creating server instance...\n");
|
||||
struct UASYNC* server_ua = uasync_create();
|
||||
server_instance = utun_instance_create(server_ua, "test_server.conf");
|
||||
ua = uasync_create();
|
||||
server_instance = utun_instance_create(ua, "test_server.conf");
|
||||
if (!server_instance) {
|
||||
printf("Failed to create server instance\n");
|
||||
return 1;
|
||||
@@ -296,19 +297,18 @@ int main() {
|
||||
utun_instance_destroy(server_instance);
|
||||
return 1;
|
||||
} else {
|
||||
printf("Server connections initialized: sockets=%p, connections=%p\n",
|
||||
printf("Server connections initialized: sockets=%p, connections=%p\n",
|
||||
server_instance->etcp_sockets, server_instance->connections);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
printf("✅ Server instance initialized successfully (node_id=%llx)\n", (unsigned long long)server_instance->node_id);
|
||||
printf("Server instance ready (node_id=%llx)\n\n", (unsigned long long)server_instance->node_id);
|
||||
|
||||
|
||||
// Create client instance
|
||||
printf("Creating client instance...\n");
|
||||
struct UASYNC* client_ua = uasync_create();
|
||||
client_instance = utun_instance_create(client_ua, "test_client.conf");
|
||||
client_instance = utun_instance_create(ua, "test_client.conf");
|
||||
if (!client_instance) {
|
||||
printf("Failed to create client instance\n");
|
||||
utun_instance_destroy(server_instance);
|
||||
@@ -372,8 +372,8 @@ int main() {
|
||||
|
||||
// Start monitoring and packet transmission
|
||||
printf("Starting packet transmission test...\n");
|
||||
packet_timeout_id = uasync_set_timeout(server_ua, 500, NULL, monitor_and_send);
|
||||
void* global_timeout_id = uasync_set_timeout(server_ua, TEST_TIMEOUT_MS, NULL, test_timeout);
|
||||
packet_timeout_id = uasync_set_timeout(ua, 500, NULL, monitor_and_send);
|
||||
void* global_timeout_id = uasync_set_timeout(ua, TEST_TIMEOUT_MS, NULL, test_timeout);
|
||||
|
||||
// Main event loop
|
||||
printf("Running event loop...\n\n");
|
||||
@@ -381,40 +381,36 @@ int main() {
|
||||
// Give instances time to initialize
|
||||
printf("Waiting 0.5 seconds for instances initialization...\n");
|
||||
for (int i = 0; i < 50; i++) {
|
||||
if (server_ua) uasync_poll(server_ua, 10);
|
||||
if (client_ua) uasync_poll(client_ua, 10);
|
||||
usleep(10000);
|
||||
if (ua) uasync_poll(ua, 10);
|
||||
}
|
||||
printf("Starting connection and packet transmission...\n");
|
||||
|
||||
|
||||
int elapsed = 0;
|
||||
int poll_interval = 5;
|
||||
while (!test_completed && elapsed < TEST_TIMEOUT_MS + 1000) {
|
||||
if (server_ua) uasync_poll(server_ua, poll_interval);
|
||||
if (client_ua) uasync_poll(client_ua, poll_interval);
|
||||
|
||||
usleep(poll_interval * 1000);
|
||||
if (ua) uasync_poll(ua, poll_interval);
|
||||
|
||||
elapsed += poll_interval;
|
||||
|
||||
|
||||
// Quick exit if packet received
|
||||
if (test_completed == 1) {
|
||||
printf("[TEST] Packet received, exiting early after %d ms\n", elapsed);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Cleanup
|
||||
printf("\nCleaning up...\n");
|
||||
|
||||
|
||||
// Cancel timeouts
|
||||
if (packet_timeout_id) {
|
||||
uasync_cancel_timeout(server_ua, packet_timeout_id);
|
||||
uasync_cancel_timeout(ua, packet_timeout_id);
|
||||
packet_timeout_id = NULL;
|
||||
}
|
||||
if (global_timeout_id) {
|
||||
uasync_cancel_timeout(server_ua, global_timeout_id);
|
||||
uasync_cancel_timeout(ua, global_timeout_id);
|
||||
}
|
||||
|
||||
|
||||
// Destroy instances
|
||||
if (server_instance) {
|
||||
server_instance->running = 0;
|
||||
@@ -424,6 +420,12 @@ int main() {
|
||||
client_instance->running = 0;
|
||||
utun_instance_destroy(client_instance);
|
||||
}
|
||||
|
||||
// Destroy shared uasync instance after both instances are destroyed
|
||||
if (ua) {
|
||||
uasync_destroy(ua, 0);
|
||||
ua = NULL;
|
||||
}
|
||||
|
||||
// Evaluate test result
|
||||
if (test_completed == 1) {
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
static struct UTUN_INSTANCE* server_instance = NULL;
|
||||
static struct UTUN_INSTANCE* client_instance = NULL;
|
||||
static struct UASYNC* ua = NULL;
|
||||
static int test_completed = 0; // 0 = running, 1 = success, 2 = timeout/failure
|
||||
static void* monitor_timeout_id = NULL;
|
||||
static void* test_timeout_id = NULL;
|
||||
@@ -88,7 +89,7 @@ static void monitor_connections(void* arg) {
|
||||
// Отменить таймаут теста для быстрого завершения
|
||||
if (test_timeout_id) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[TEST] Cancelling test timeout for immediate exit\n");
|
||||
uasync_cancel_timeout(server_instance->ua, test_timeout_id);
|
||||
uasync_cancel_timeout(ua, test_timeout_id);
|
||||
test_timeout_id = NULL;
|
||||
}
|
||||
return;
|
||||
@@ -100,7 +101,7 @@ static void monitor_connections(void* arg) {
|
||||
|
||||
// Schedule next check
|
||||
if (!test_completed) {
|
||||
monitor_timeout_id = uasync_set_timeout(server_instance->ua, 200, NULL, monitor_connections); // Check every 200ms for faster detection
|
||||
monitor_timeout_id = uasync_set_timeout(ua, 200, NULL, monitor_connections); // Check every 200ms for faster detection
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,7 +112,7 @@ static void test_timeout(void* arg) {
|
||||
test_completed = 2; // Timeout/failure
|
||||
// Cancel the monitoring timeout since we're done
|
||||
if (monitor_timeout_id) {
|
||||
uasync_cancel_timeout(server_instance->ua, monitor_timeout_id);
|
||||
uasync_cancel_timeout(ua, monitor_timeout_id);
|
||||
monitor_timeout_id = NULL;
|
||||
}
|
||||
}
|
||||
@@ -129,18 +130,24 @@ int main() {
|
||||
|
||||
// Create server instance
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Creating server instance...");
|
||||
struct UASYNC* server_ua = uasync_create();
|
||||
server_instance = utun_instance_create(server_ua, "test_server.conf");
|
||||
ua = uasync_create();
|
||||
server_instance = utun_instance_create(ua, "test_server.conf");
|
||||
if (!server_instance) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to create server instance");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Initialize ETCP connections regardless of TUN state
|
||||
if (init_connections(server_instance) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to initialize server connections");
|
||||
utun_instance_destroy(server_instance);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if TUN is disabled and handle accordingly
|
||||
if (server_instance->tun.fd < 0) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Server TUN disabled - skipping full initialization");
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Server TUN disabled - skipping TUN initialization");
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Server instance created successfully (node_id=%llx)", (unsigned long long)server_instance->node_id);
|
||||
// Skip to client creation
|
||||
} else {
|
||||
// Normal initialization for TUN-enabled mode
|
||||
if (utun_instance_init(server_instance) < 0) {
|
||||
@@ -159,19 +166,25 @@ int main() {
|
||||
|
||||
// Create client instance
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Creating client instance...");
|
||||
struct UASYNC* client_ua = uasync_create();
|
||||
client_instance = utun_instance_create(client_ua, "test_client.conf");
|
||||
client_instance = utun_instance_create(ua, "test_client.conf");
|
||||
if (!client_instance) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to create client instance");
|
||||
utun_instance_destroy(server_instance);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Initialize ETCP connections regardless of TUN state
|
||||
if (init_connections(client_instance) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to initialize client connections");
|
||||
utun_instance_destroy(server_instance);
|
||||
utun_instance_destroy(client_instance);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if TUN is disabled and handle accordingly
|
||||
if (client_instance->tun.fd < 0) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "ℹ️ Client TUN disabled - skipping full initialization\n");
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "ℹ️ Client TUN disabled - skipping TUN initialization\n");
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "✅ Client instance created successfully (node_id=%llx)\n", (unsigned long long)client_instance->node_id);
|
||||
// Skip to test execution
|
||||
} else {
|
||||
// Normal initialization for TUN-enabled mode
|
||||
if (utun_instance_init(client_instance) < 0) {
|
||||
@@ -192,8 +205,8 @@ int main() {
|
||||
|
||||
// Start monitoring
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Starting connection monitoring...\n");
|
||||
monitor_timeout_id = uasync_set_timeout(server_ua, 1000, NULL, monitor_connections);
|
||||
test_timeout_id = uasync_set_timeout(server_ua, TEST_TIMEOUT_MS, NULL, test_timeout);
|
||||
monitor_timeout_id = uasync_set_timeout(ua, 100, NULL, monitor_connections);
|
||||
test_timeout_id = uasync_set_timeout(ua, TEST_TIMEOUT_MS, NULL, test_timeout);
|
||||
|
||||
// Main event loop
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Running event loop...\n\n");
|
||||
@@ -201,19 +214,15 @@ int main() {
|
||||
// Give server time to fully initialize before client starts sending
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Waiting 0.5 seconds for server initialization...\n");
|
||||
for (int i = 0; i < 50; i++) { // Reduced from 200 to 50 iterations (0.5 seconds)
|
||||
if (server_ua) uasync_poll(server_ua, 10);
|
||||
if (client_ua) uasync_poll(client_ua, 10);
|
||||
usleep(10000); // 10ms
|
||||
if (ua) uasync_poll(ua, 10);
|
||||
}
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Starting connection attempts...\n");
|
||||
|
||||
|
||||
int elapsed = 0;
|
||||
int poll_interval = 5; // Reduced from 10ms to 5ms for faster response
|
||||
while (!test_completed && elapsed < TEST_TIMEOUT_MS + 500) {
|
||||
if (server_ua) uasync_poll(server_ua, poll_interval);
|
||||
if (client_ua) uasync_poll(client_ua, poll_interval);
|
||||
|
||||
usleep(poll_interval * 1000); // microseconds
|
||||
if (ua) uasync_poll(ua, poll_interval);
|
||||
|
||||
elapsed += poll_interval;
|
||||
|
||||
// Быстрый выход если подключение установлено
|
||||
@@ -225,31 +234,27 @@ int main() {
|
||||
|
||||
// Cleanup
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\nCleaning up...\n");
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] server_ua=%p, client_ua=%p\n", server_ua, client_ua);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] ua=%p\n", ua);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] server_instance=%p, client_instance=%p\n", server_instance, client_instance);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] monitor_timeout_id=%p, test_timeout_id=%p\n", monitor_timeout_id, test_timeout_id);
|
||||
|
||||
// СНАЧАЛА отменить таймеры пока uasync ещё жив
|
||||
if (monitor_timeout_id) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Canceling monitor timeout on valid uasync\n");
|
||||
uasync_cancel_timeout(server_ua, monitor_timeout_id);
|
||||
uasync_cancel_timeout(ua, monitor_timeout_id);
|
||||
monitor_timeout_id = NULL;
|
||||
}
|
||||
if (test_timeout_id) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Canceling test timeout on valid uasync\n");
|
||||
uasync_cancel_timeout(server_ua, test_timeout_id);
|
||||
uasync_cancel_timeout(ua, test_timeout_id);
|
||||
test_timeout_id = NULL;
|
||||
}
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Timeouts canceled\n");
|
||||
|
||||
// Диагностика ресурсов перед cleanup
|
||||
if (server_ua) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Server resources before destroy:\n");
|
||||
uasync_print_resources(server_ua, "SERVER");
|
||||
}
|
||||
if (client_ua) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Client resources before destroy:\n");
|
||||
uasync_print_resources(client_ua, "CLIENT");
|
||||
if (ua) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Shared uasync resources before destroy:\n");
|
||||
uasync_print_resources(ua, "SHARED");
|
||||
}
|
||||
|
||||
// ПОТОМ уничтожить instances
|
||||
@@ -265,8 +270,10 @@ int main() {
|
||||
}
|
||||
|
||||
// Cleanup uasync objects - now required since utun_instance_destroy no longer calls uasync_destroy
|
||||
if (server_ua) uasync_destroy(server_ua, 0);
|
||||
if (client_ua) uasync_destroy(client_ua, 0);
|
||||
if (ua) {
|
||||
uasync_destroy(ua, 0);
|
||||
ua = NULL;
|
||||
}
|
||||
|
||||
if (test_completed == 1) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\n=== TEST PASSED ===\n");
|
||||
|
||||
Reference in new issue
Block a user