This commit is contained in:
2026-02-06 21:03:59 -03:00
commit 2b7ab5f570
267 changed files with 176026 additions and 0 deletions

View File

@@ -0,0 +1,326 @@
//! Comprehensive tests for message routing functionality
//! Tests routing to known/unknown destinations, route expiration, updates, concurrency, and metrics
const std = @import("std");
const testing = std.testing;
const protocol = @import("protocol");
const message = protocol.message;
const router_mod = protocol.router;
test "router: route message to known destination" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var source: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
source[0] = 1;
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = 2;
// Add route to destination
try router.addRoute(dest, dest, 1);
// Create message
const header = message.MessageHeader.init(.ping, 123, source, dest);
const base_msg = message.BaseMessage{ .header = header, .payload = &.{} };
// Route should succeed
const result = router.routeMessage(base_msg);
try testing.expect(result.success);
try testing.expectEqual(dest, result.next_hop.?);
try testing.expectEqual(@as(u32, 1), result.cost.?);
}
test "router: route message to unknown destination initiates discovery" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var source: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
source[0] = 1;
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = 99; // Unknown destination
const header = message.MessageHeader.init(.ping, 456, source, dest);
const base_msg = message.BaseMessage{ .header = header, .payload = &.{} };
// Route should fail (no route exists)
const result = router.routeMessage(base_msg);
try testing.expect(!result.success);
try testing.expectEqual(protocol.protocol.ErrorCode.routing_error, result.err.?);
}
test "router: add and retrieve route" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = 5;
var next_hop: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
next_hop[0] = 3;
try router.addRoute(dest, next_hop, 2);
const retrieved_hop = router.getNextHop(dest);
try testing.expect(retrieved_hop != null);
try testing.expectEqual(next_hop, retrieved_hop.?);
}
test "router: route expiration after max age" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = 7;
try router.addRoute(dest, dest, 1);
// Verify route exists
try testing.expect(router.isReachable(dest));
// Clean up expired routes with 0 second max age (expire immediately)
router.cleanupExpiredRoutes(0);
// Route should still exist but marked as invalid
try testing.expect(!router.isReachable(dest));
}
test "router: route update with better cost" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = 8;
var hop1: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
hop1[0] = 10;
var hop2: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
hop2[0] = 11;
// Add initial route with cost 5
try router.addRoute(dest, hop1, 5);
var next_hop = router.getNextHop(dest);
try testing.expectEqual(hop1, next_hop.?);
// Add better route with cost 2
try router.addRoute(dest, hop2, 2);
next_hop = router.getNextHop(dest);
try testing.expectEqual(hop2, next_hop.?);
}
test "router: route update preserves worse cost" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = 9;
var hop1: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
hop1[0] = 12;
var hop2: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
hop2[0] = 13;
// Add initial route with cost 2
try router.addRoute(dest, hop1, 2);
// Try to add worse route with cost 5 (should be ignored)
try router.addRoute(dest, hop2, 5);
const next_hop = router.getNextHop(dest);
try testing.expectEqual(hop1, next_hop.?);
}
test "router: remove route" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = 15;
try router.addRoute(dest, dest, 1);
try testing.expect(router.isReachable(dest));
router.removeRoute(dest);
try testing.expect(!router.isReachable(dest));
}
test "router: invalidate route" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = 16;
try router.addRoute(dest, dest, 1);
try testing.expect(router.isReachable(dest));
router.invalidateRoute(dest);
try testing.expect(!router.isReachable(dest));
// Route still exists but marked invalid
const metrics = router.getMetrics();
try testing.expect(metrics.total_routes > 0);
}
test "router: metrics accuracy under load" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
// Add 100 routes
var i: u8 = 0;
while (i < 100) : (i += 1) {
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = i;
try router.addRoute(dest, dest, @as(u32, i));
}
const metrics = router.getMetrics();
try testing.expectEqual(@as(usize, 100), metrics.total_routes);
try testing.expectEqual(@as(usize, 100), metrics.active_routes);
}
test "router: successful lookup metrics" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var source: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
source[0] = 1;
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = 2;
try router.addRoute(dest, dest, 1);
const header = message.MessageHeader.init(.ping, 789, source, dest);
const base_msg = message.BaseMessage{ .header = header, .payload = &.{} };
_ = router.routeMessage(base_msg);
const metrics = router.getMetrics();
try testing.expectEqual(@as(u64, 1), metrics.successful_lookups);
try testing.expectEqual(@as(u64, 0), metrics.failed_lookups);
}
test "router: failed lookup metrics" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var source: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
source[0] = 1;
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = 99; // Unknown
const header = message.MessageHeader.init(.ping, 999, source, dest);
const base_msg = message.BaseMessage{ .header = header, .payload = &.{} };
_ = router.routeMessage(base_msg);
const metrics = router.getMetrics();
try testing.expectEqual(@as(u64, 0), metrics.successful_lookups);
try testing.expectEqual(@as(u64, 1), metrics.failed_lookups);
}
test "router: broadcast message handling" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var source: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
source[0] = 1;
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
var header = message.MessageHeader.init(.ping, 111, source, dest);
header.flags |= protocol.protocol.MessageFlags.BROADCAST;
const base_msg = message.BaseMessage{ .header = header, .payload = &.{} };
const result = router.routeMessage(base_msg);
try testing.expect(result.success);
}
test "router: local message detection" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var source: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
source[0] = 1;
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE; // All zeros = local
const header = message.MessageHeader.init(.ping, 222, source, dest);
const base_msg = message.BaseMessage{ .header = header, .payload = &.{} };
const result = router.routeMessage(base_msg);
try testing.expect(result.success);
try testing.expect(result.next_hop == null); // Local, no next hop
}
test "router: route stats accuracy" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
// Add some routes
var i: u8 = 0;
while (i < 10) : (i += 1) {
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = i;
try router.addRoute(dest, dest, 1);
}
// Invalidate half
i = 0;
while (i < 5) : (i += 1) {
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = i;
router.invalidateRoute(dest);
}
const stats = router.getRouteStats();
try testing.expectEqual(@as(usize, 10), stats.total);
try testing.expectEqual(@as(usize, 5), stats.active);
}
test "router: add neighbor" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var neighbor: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
neighbor[0] = 20;
try router.addNeighbor(neighbor);
try testing.expectEqual(@as(usize, 1), router.neighbors.items.len);
}
test "router: route response handling" {
const allocator = testing.allocator;
var router = router_mod.MessageRouter.init(allocator);
defer router.deinit();
var dest: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
dest[0] = 30;
var next_hop: message.NodeId = .{0} ** protocol.protocol.Network.NODE_ID_SIZE;
next_hop[0] = 31;
const query_id: u64 = 5555;
// Add to pending queries
try router.pending_queries.put(query_id, dest);
// Create route response
var response = message.RouteResponse.init(query_id, "route_info");
response.next_hop = next_hop;
// Handle response
try router.handleRouteResponse(response);
// Route should now exist
const retrieved_hop = router.getNextHop(dest);
try testing.expectEqual(next_hop, retrieved_hop.?);
}