From d2f507c6b77af21308ca70937f6daa8cdff76cb9 Mon Sep 17 00:00:00 2001 From: evgeny Date: Tue, 27 Oct 2026 13:56:03 +0100 Subject: [PATCH] Test control backpressure, snapshot mutations and deferred cleanup --- tests/test_control_server.c | 154 ++++++++++++++++++++++++++++++++++-- 1 file changed, 148 insertions(+), 6 deletions(-) diff --git a/tests/test_control_server.c b/tests/test_control_server.c index 3cc290252..fbcb7ede7 100644 --- a/tests/test_control_server.c +++ b/tests/test_control_server.c @@ -10,7 +10,7 @@ static socket_t pending_accept = -1, capture_fd = -1; static int fail_registration, fail_timer, fail_entry, send_mode; static size_t send_chunk, wire_size; -static uint8_t wire[ETCPMON_MAX_MSG_SIZE]; +static uint8_t wire[ETCPMON_MAX_MSG_SIZE * 8]; static int nat_checks, stun_checks; void test_nat_check_all(struct NAT_DETECTION* detector) { assert(detector); nat_checks++; } @@ -90,6 +90,9 @@ static void fixture_destroy(struct fixture* f) { capture_fd = -1; send_mode = 0; control_server_shutdown(&f->server); if (f->peer >= 0) close(f->peer); + DEBUG_DEBUG(DEBUG_CATEGORY_CONTROL, "Fixture drain after shutdown: timers_alloc=%zu timers_free=%zu immediate=%p", + f->inst.ua->timer_alloc_count, f->inst.ua->timer_free_count, (void*)f->inst.ua->immediate_queue_head); + uasync_drain_immediate(f->inst.ua); TimeoutEntry pending; assert(timeout_heap_peek(f->inst.ua->timeout_heap, &pending) != 0); assert(timeout_heap_get_size(f->inst.ua->timeout_heap) == 0); @@ -109,29 +112,39 @@ static void test_partial_send(void) { struct fixture f; fixture_init(&f); struct control_client* client = fixture_accept(&f); assert(client); capture_fd = client->fd; send_chunk = 3; wire_size = 0; - enqueue(&f, client, "0123456789"); enqueue(&f, client, "abcdefgh"); + enqueue(&f, client, "0123456789"); uint8_t* base = client->output_queue->head->dgram; client_write_callback(client->fd, client); assert(client->output_offset == 3 && client->output_queue->head->dgram == base); - assert(queue_total_bytes(client->output_queue) == 18); + assert(queue_total_bytes(client->output_queue) == 10); send_mode = 1; client_write_callback(client->fd, client); assert(client->output_offset == 3 && wire_size == 3); send_mode = 0; while (client->output_queue->head) client_write_callback(client->fd, client); + enqueue(&f, client, "abcdefgh"); + while (client->output_queue->head) client_write_callback(client->fd, client); assert(wire_size == 18 && !memcmp(wire, "0123456789abcdefgh", 18)); assert(!client->output_offset && !client->write_registered && !queue_total_bytes(client->output_queue)); + uasync_drain_immediate(f.inst.ua); + assert(!client->read_paused && !client->output_waiter.internal && !client->output_waiter.call_soon_id); fixture_destroy(&f); puts("PASS partial send: exact wire bytes, EAGAIN, stable buffer and queue accounting"); } static void test_cleanup(void) { - for (int mode = 0; mode <= 3; mode++) { + for (int mode = 0; mode <= 4; mode++) { struct fixture f; fixture_init(&f); struct control_client* client = fixture_accept(&f); assert(client); - enqueue(&f, client, "first message"); enqueue(&f, client, "second message"); + enqueue(&f, client, "first message"); capture_fd = client->fd; send_chunk = 2; wire_size = 0; client_write_callback(client->fd, client); assert(client->output_offset == 2); - if (mode >= 2) { send_mode = mode; client_write_callback(client->fd, client); } + if (mode == 4) { + send_chunk = 100; + client_write_callback(client->fd, client); + assert(client->output_waiter.call_soon_id); + close_client(&f.server, client); + uasync_drain_immediate(f.inst.ua); + } else if (mode >= 2) { send_mode = mode; client_write_callback(client->fd, client); } else if (mode == 1) { uasync_cancel_timeout(f.inst.ua, client->idle_timer); control_client_idle_cb(client); @@ -287,9 +300,138 @@ static void test_actions(void) { puts("PASS Action: NAT acknowledgement, unavailable services, unknown/exact names and malformed payloads"); } +/* Пакет запросов остаётся в bounded RX до отправки предыдущего ответа; команды не продлевают TX deadline. */ +static void test_backpressure(void) { + struct fixture f; fixture_init(&f); + struct control_client* client = fixture_accept(&f); assert(client); + for (int i = 0; i < 8; i++) + etcpmon_build_header((void*)(client->recv_buffer + i * sizeof(struct etcpmon_msg_header)), 0, + ETCPMON_CMD_GET_DEBUG_CONFIG, i + 1); + client->recv_len = 8 * sizeof(struct etcpmon_msg_header); + assert(handle_client_data(&f.server, client) == 0); + assert(client->output_queue->count == 1 && client->read_paused && client->timer_is_tx); + assert(client->recv_len == 7 * sizeof(struct etcpmon_msg_header)); + void* deadline = client->idle_timer; + capture_fd = client->fd; send_mode = 1; wire_size = 0; + for (int i = 0; i < 10; i++) { + assert(handle_client_data(&f.server, client) == 0); + client_write_callback(client->fd, client); + assert(client->output_queue->count == 1 && client->idle_timer == deadline); + } + send_mode = 0; send_chunk = 3; + for (int step = 0; step < 5000 && (client->read_paused || client->output_queue->head); step++) { + if (client->output_queue->head) client_write_callback(client->fd, client); + uasync_drain_immediate(f.inst.ua); + assert(client->output_queue->count <= 1 && queue_total_bytes(client->output_queue) <= ETCPMON_MAX_MSG_SIZE); + } + assert(!client->read_paused && !client->recv_len && !client->timer_is_tx); + size_t offset = 0; + for (int i = 0; i < 8; i++) { + struct etcpmon_msg_header* header = (void*)(wire + offset); + assert(header->type == ETCPMON_RSP_DEBUG_CONFIG && header->seq_id == i + 1); + offset += header->size; + } + assert(offset == wire_size); + fixture_destroy(&f); + puts("PASS backpressure: one TX frame, paused buffered requests, exact ordered replies and independent TX deadline"); +} + +/* Минимальная настоящая топология для сериализации большого списка без сетевых служб/БД. */ +struct node_fixture { + struct TOPO_GROUPS groups; + struct TOPO_GROUP* group; + struct TOPO_NODE* records; +}; + +static void node_fixture_init(struct fixture* f, struct node_fixture* nodes, int count) { + memset(nodes, 0, sizeof(*nodes)); + nodes->groups.instance = &f->inst; f->inst.topo_groups = &nodes->groups; + nodes->groups.group_list = queue_new(f->inst.ua, 16, 0, 8, "test groups"); + nodes->groups.node_registry = queue_new(f->inst.ua, 16, 0, 8, "test registry"); + struct ll_entry* entry = queue_entry_new(sizeof(struct TOPO_GROUP)); assert(entry); + memset(entry->data, 0, entry->size); + nodes->group = (void*)entry; nodes->group->group_id = TOPO_GROUP_UTUN; nodes->group->instance = &f->inst; + nodes->group->nodes = queue_new(f->inst.ua, 16, offsetof(struct TOPO_GROUP_NODE, node_id) - sizeof(struct ll_entry), 8, "test nodes"); + assert(nodes->groups.group_list && nodes->groups.node_registry && nodes->group->nodes); + assert(queue_data_put_with_index(nodes->groups.group_list, entry) == 0); + nodes->records = u_calloc(count, sizeof(*nodes->records)); assert(nodes->records); + for (int i = 0; i < count; i++) { + struct TOPO_NODE* record = &nodes->records[i]; + record->node_id = 100 + i; record->timestamp = 1; record->node_name = "monitor-test"; + entry = queue_entry_new(8 + sizeof(record)); assert(entry); + memcpy(entry->data, &record->node_id, 8); memcpy(entry->data + 8, &record, sizeof(record)); + assert(queue_data_put_with_index(nodes->groups.node_registry, entry) == 0); + entry = queue_entry_new(sizeof(struct TOPO_GROUP_NODE)); assert(entry); + memset(entry->data, 0, entry->size); + ((struct TOPO_GROUP_NODE*)entry)->node_id = record->node_id; + assert(queue_data_put_with_index(nodes->group->nodes, entry) == 0); + } +} + +static void node_fixture_destroy(struct fixture* f, struct node_fixture* nodes) { + if (f->server.clients) close_client(&f->server, f->server.clients); + struct ll_entry* entry; + while ((entry = queue_data_get(nodes->group->nodes))) queue_entry_free(entry); + queue_free(nodes->group->nodes); + queue_entry_free(queue_data_get(nodes->groups.group_list)); queue_free(nodes->groups.group_list); + while ((entry = queue_data_get(nodes->groups.node_registry))) queue_entry_free(entry); + queue_free(nodes->groups.node_registry); u_free(nodes->records); f->inst.topo_groups = NULL; +} + +/* Снимок больше wire-лимита, удаление ещё не отправленного узла и объединение live-изменений. */ +static void test_node_snapshot(void) { + struct fixture f; fixture_init(&f); + struct node_fixture nodes; node_fixture_init(&f, &nodes, 400); + struct control_client* client = fixture_accept(&f); assert(client); + control_server_send_node_list(&f.server, client, 77); + assert(client->snapshot_count == 400 && !client->output_queue->head); + assert(control_wait_output(client) == 0); + uasync_drain_immediate(f.inst.ua); + assert(client->output_queue->count == 1); + struct TOPO_GROUP_NODE* removed = topo_node_find_by_id(nodes.group, 101); assert(removed); + control_server_notify_node_removed(&f.server, 101); + assert(queue_remove_data(nodes.group->nodes, &removed->ll) == 0); queue_entry_free(&removed->ll); + nodes.records[2].timestamp = 2; + for (int i = 0; i < 100; i++) control_server_notify_node_change(&f.server, topo_node_find_by_id(nodes.group, 102)); + assert(client->dirty_count == 2); + capture_fd = client->fd; send_chunk = ETCPMON_MAX_MSG_SIZE; wire_size = 0; + for (int step = 0; step < 2000 && client->read_paused; step++) { + if (client->output_queue->head) client_write_callback(client->fd, client); + uasync_drain_immediate(f.inst.ua); + assert(client->output_queue->count <= 1 && queue_total_bytes(client->output_queue) <= ETCPMON_MAX_MSG_SIZE); + } + assert(!client->read_paused && !client->snapshot_ids && !client->dirty_count && wire_size > ETCPMON_MAX_MSG_SIZE); + size_t offset = 0; int infos = 0, ends = 0, removals = 0, updated = 0; + while (offset < wire_size) { + struct etcpmon_msg_header* header = (void*)(wire + offset); + assert(etcpmon_validate_header(header) == 0 && offset + header->size <= wire_size); + if (header->type == ETCPMON_RSP_NODE_INFO) { + struct TOPOMSG_NODE* node = (void*)(wire + offset + sizeof(*header) + 2); + assert(node->node_id != 101); + if (node->node_id == 102) { assert(node->timestamp == 2); updated++; } + infos++; + } else if (header->type == ETCPMON_RSP_NODE_INFO_END) { + assert(header->seq_id == 77 && infos == 399); ends++; + } else { + uint64_t id; memcpy(&id, wire + offset + sizeof(*header), sizeof(id)); + assert(header->type == ETCPMON_RSP_NODE_REMOVED && id == 101 && ends == 1); removals++; + } + offset += header->size; + } + assert(infos == 400 && ends == 1 && removals == 1 && updated == 2); + for (int i = 0; i < CONTROL_NODE_DIRTY_MAX; i++) control_server_notify_node_removed(&f.server, 1000 + i); + assert(client->dirty_count == CONTROL_NODE_DIRTY_MAX); + control_server_notify_node_removed(&f.server, 1000 + CONTROL_NODE_DIRTY_MAX); + assert(!f.server.clients); + uasync_drain_immediate(f.inst.ua); + node_fixture_destroy(&f, &nodes); fixture_destroy(&f); + puts("PASS node snapshot: 400 nodes with one frame, removed/updated IDs, ordered end, coalescing and bounded overload cleanup"); +} + int main(void) { debug_config_init(); debug_set_level(DEBUG_LEVEL_WARN); debug_set_category_level(DEBUG_CATEGORY_CONTROL, DEBUG_LEVEL_DEBUG); test_partial_send(); test_cleanup(); test_debug_payload(); test_metrics(); test_socket_limit(); test_actions(); + test_backpressure(); test_node_snapshot(); return 0; }