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,381 @@
const std = @import("std");
const testing = std.testing;
const topology_mod = @import("../src/network/topology.zig");
const NetworkTopology = topology_mod.NetworkTopology;
const NodePosition = topology_mod.NodePosition;
const PeerConnection = topology_mod.PeerConnection;
const TopologyType = topology_mod.TopologyType;
const TopologyRole = topology_mod.TopologyRole;
const peer_mod = @import("../src/network/peer.zig");
const PeerId = peer_mod.PeerId;
test "topology: node position initialization" {
const allocator = testing.allocator;
const peer_id = PeerId.init();
var position = NodePosition.init(allocator, peer_id);
defer position.deinit();
try testing.expectEqual(@as(usize, 0), position.getNeighborCount());
try testing.expectEqual(TopologyRole.Regular, position.role);
}
test "topology: node add and remove neighbor" {
const allocator = testing.allocator;
const peer_id = PeerId.init();
var position = NodePosition.init(allocator, peer_id);
defer position.deinit();
const neighbor_id = PeerId.init();
try position.addNeighbor(neighbor_id);
try testing.expectEqual(@as(usize, 1), position.getNeighborCount());
position.removeNeighbor(neighbor_id);
try testing.expectEqual(@as(usize, 0), position.getNeighborCount());
}
test "topology: add duplicate neighbor is safe" {
const allocator = testing.allocator;
const peer_id = PeerId.init();
var position = NodePosition.init(allocator, peer_id);
defer position.deinit();
const neighbor_id = PeerId.init();
try position.addNeighbor(neighbor_id);
try position.addNeighbor(neighbor_id); // Duplicate
try testing.expectEqual(@as(usize, 1), position.getNeighborCount());
}
test "topology: peer connection initialization" {
const source_id = PeerId.init();
const dest_id = PeerId.init();
const conn = PeerConnection.init(source_id, dest_id);
try testing.expectEqual(@as(u8, 50), conn.quality);
try testing.expect(conn.bidirectional);
try testing.expect(conn.isActive());
}
test "topology: peer connection update metrics" {
const source_id = PeerId.init();
const dest_id = PeerId.init();
var conn = PeerConnection.init(source_id, dest_id);
conn.updateMetrics(100, 1000000, 80);
try testing.expectEqual(@as(u32, 100), conn.latency_ms);
try testing.expectEqual(@as(u64, 1000000), conn.bandwidth_bps);
try testing.expectEqual(@as(u8, 80), conn.quality);
}
test "topology: peer connection activity tracking" {
const source_id = PeerId.init();
const dest_id = PeerId.init();
var conn = PeerConnection.init(source_id, dest_id);
try testing.expect(conn.isActive());
// Set old timestamp
conn.last_activity = std.time.timestamp() - 400;
try testing.expect(!conn.isActive());
}
test "topology: network topology initialization" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Mesh);
defer topo.deinit();
try testing.expectEqual(TopologyType.Mesh, topo.topology_type);
try testing.expectEqual(@as(usize, 0), topo.nodes.count());
}
test "topology: add node to topology" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Mesh);
defer topo.deinit();
const peer_id = PeerId.init();
_ = try topo.addNode(peer_id, .Regular);
try testing.expectEqual(@as(usize, 1), topo.nodes.count());
}
test "topology: remove node from topology" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Mesh);
defer topo.deinit();
const peer_id = PeerId.init();
_ = try topo.addNode(peer_id, .Regular);
try topo.removeNode(peer_id);
try testing.expectEqual(@as(usize, 0), topo.nodes.count());
}
test "topology: add connection between nodes" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Mesh);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
_ = try topo.addNode(peer1, .Regular);
_ = try topo.addNode(peer2, .Regular);
try topo.addConnection(peer1, peer2, 80);
try testing.expectEqual(@as(usize, 1), topo.connections.count());
try testing.expect(topo.areConnected(peer1, peer2));
}
test "topology: remove connection between nodes" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Mesh);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
_ = try topo.addNode(peer1, .Regular);
_ = try topo.addNode(peer2, .Regular);
try topo.addConnection(peer1, peer2, 80);
topo.removeConnection(peer1, peer2);
try testing.expectEqual(@as(usize, 0), topo.connections.count());
try testing.expect(!topo.areConnected(peer1, peer2));
}
test "topology: get node degree" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Mesh);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
const peer3 = PeerId.init();
_ = try topo.addNode(peer1, .Hub);
_ = try topo.addNode(peer2, .Regular);
_ = try topo.addNode(peer3, .Regular);
try topo.addConnection(peer1, peer2, 80);
try topo.addConnection(peer1, peer3, 70);
try testing.expectEqual(@as(usize, 2), topo.getNodeDegree(peer1));
}
test "topology: shortest path finding" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Tree);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
const peer3 = PeerId.init();
_ = try topo.addNode(peer1, .Hub);
_ = try topo.addNode(peer2, .Regular);
_ = try topo.addNode(peer3, .Leaf);
try topo.addConnection(peer1, peer2, 80);
try topo.addConnection(peer2, peer3, 70);
var path = try topo.findShortestPath(peer1, peer3);
defer path.deinit(allocator);
try testing.expectEqual(@as(usize, 3), path.items.len);
try testing.expect(std.mem.eql(u8, &peer1.bytes, &path.items[0].bytes));
try testing.expect(std.mem.eql(u8, &peer3.bytes, &path.items[2].bytes));
}
test "topology: shortest path when no path exists" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Mesh);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
_ = try topo.addNode(peer1, .Regular);
_ = try topo.addNode(peer2, .Regular);
// No connection between peers
var path = try topo.findShortestPath(peer1, peer2);
defer path.deinit(allocator);
try testing.expectEqual(@as(usize, 0), path.items.len);
}
test "topology: analyze topology metrics" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Mesh);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
const peer3 = PeerId.init();
_ = try topo.addNode(peer1, .Hub);
_ = try topo.addNode(peer2, .Regular);
_ = try topo.addNode(peer3, .Leaf);
try topo.addConnection(peer1, peer2, 80);
try topo.addConnection(peer2, peer3, 70);
try topo.addConnection(peer1, peer3, 60);
try topo.analyzeTopology();
try testing.expect(topo.density > 0.0);
try testing.expect(topo.clustering_coefficient >= 0.0);
}
test "topology: hub node tracking" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Hybrid);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
_ = try topo.addNode(peer1, .Hub);
_ = try topo.addNode(peer2, .Regular);
try testing.expectEqual(@as(usize, 1), topo.hub_nodes.items.len);
}
test "topology: optimize topology connects hubs" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Hybrid);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
const peer3 = PeerId.init();
_ = try topo.addNode(peer1, .Hub);
_ = try topo.addNode(peer2, .Hub);
_ = try topo.addNode(peer3, .Leaf);
try topo.addConnection(peer1, peer3, 70);
const initial_connections = topo.connections.count();
try topo.optimizeTopology();
const optimized_connections = topo.connections.count();
try testing.expect(optimized_connections > initial_connections);
try testing.expect(topo.areConnected(peer1, peer2));
}
test "topology: topology statistics" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Mesh);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
_ = try topo.addNode(peer1, .Hub);
_ = try topo.addNode(peer2, .Regular);
try topo.addConnection(peer1, peer2, 80);
const stats = topo.getStats();
try testing.expectEqual(@as(usize, 2), stats.node_count);
try testing.expectEqual(@as(usize, 1), stats.connection_count);
try testing.expectEqual(@as(usize, 1), stats.hub_count);
}
test "topology: concurrent modifications with mutex" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Mesh);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
_ = try topo.addNode(peer1, .Regular);
_ = try topo.addNode(peer2, .Regular);
// Multiple operations should be safe
try topo.addConnection(peer1, peer2, 80);
_ = topo.getNode(peer1);
_ = topo.getNodeDegree(peer1);
}
test "topology: remove node removes connections" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Mesh);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
_ = try topo.addNode(peer1, .Regular);
_ = try topo.addNode(peer2, .Regular);
try topo.addConnection(peer1, peer2, 80);
try topo.removeNode(peer1);
// Connection should be removed
try testing.expectEqual(@as(usize, 0), topo.connections.count());
}
test "topology: different topology types" {
const allocator = testing.allocator;
const types = [_]TopologyType{ .Mesh, .Star, .Tree, .Ring, .Hybrid, .DHT };
for (types) |topology_type| {
var topo = NetworkTopology.init(allocator, topology_type);
defer topo.deinit();
try testing.expectEqual(topology_type, topo.topology_type);
}
}
test "topology: network diameter calculation" {
const allocator = testing.allocator;
var topo = NetworkTopology.init(allocator, .Tree);
defer topo.deinit();
const peer1 = PeerId.init();
const peer2 = PeerId.init();
const peer3 = PeerId.init();
const peer4 = PeerId.init();
_ = try topo.addNode(peer1, .Hub);
_ = try topo.addNode(peer2, .Regular);
_ = try topo.addNode(peer3, .Regular);
_ = try topo.addNode(peer4, .Leaf);
try topo.addConnection(peer1, peer2, 80);
try topo.addConnection(peer2, peer3, 70);
try topo.addConnection(peer3, peer4, 60);
try topo.analyzeTopology();
try testing.expect(topo.diameter > 0);
}