ab ovo
This commit is contained in:
441
tests/network/test_discovery.zig
Normal file
441
tests/network/test_discovery.zig
Normal file
@@ -0,0 +1,441 @@
|
||||
//! Comprehensive tests for peer discovery system
|
||||
//!
|
||||
//! Tests cover discovery message encoding/decoding, discovery manager operations,
|
||||
//! multicast discovery, and network discovery scenarios.
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const DiscoveryRequest = @import("../../src/network/discovery.zig").DiscoveryRequest;
|
||||
const DiscoveryResponse = @import("../../src/network/discovery.zig").DiscoveryResponse;
|
||||
const DiscoveryManager = @import("../../src/network/discovery.zig").DiscoveryManager;
|
||||
const DiscoveryConfig = @import("../../src/network/discovery.zig").DiscoveryConfig;
|
||||
const DiscoveryStats = @import("../../src/network/discovery.zig").DiscoveryStats;
|
||||
const DiscoveryState = @import("../../src/network/discovery.zig").DiscoveryState;
|
||||
const DiscoveryMessageType = @import("../../src/network/discovery.zig").DiscoveryMessageType;
|
||||
const MulticastDiscovery = @import("../../src/network/discovery.zig").MulticastDiscovery;
|
||||
const PeerId = @import("../../src/network/peer.zig").PeerId;
|
||||
const Address = @import("../../src/core/address.zig").Address;
|
||||
const Buffer = @import("../../src/core/buffer.zig").Buffer;
|
||||
|
||||
test "Discovery request creation and encoding" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
const requester_id = PeerId.init();
|
||||
const request = DiscoveryRequest{
|
||||
.requester_id = requester_id,
|
||||
.port = 8080,
|
||||
.protocol_version = 1,
|
||||
.metadata = [_]u8{1, 2, 3, 4, 5, 6, 7, 8} ++ [_]u8{0} ** 24,
|
||||
};
|
||||
|
||||
var buffer = Buffer.init(allocator);
|
||||
defer buffer.deinit();
|
||||
|
||||
try request.encode(&buffer);
|
||||
|
||||
// Check that buffer has data
|
||||
try testing.expect(buffer.size > 0);
|
||||
|
||||
// Decode and verify
|
||||
const decoded = try DiscoveryRequest.decode(&buffer);
|
||||
try testing.expectEqual(requester_id, decoded.requester_id);
|
||||
try testing.expectEqual(@as(u16, 8080), decoded.port);
|
||||
try testing.expectEqual(@as(u16, 1), decoded.protocol_version);
|
||||
try testing.expect(std.mem.eql(u8, &request.metadata, &decoded.metadata));
|
||||
}
|
||||
|
||||
test "Discovery response creation and encoding" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
const responder_id = PeerId.init();
|
||||
var address = Address.init();
|
||||
address.port = 9090;
|
||||
|
||||
var capabilities: [8]u8 = undefined;
|
||||
capabilities[0] = 1; // relay
|
||||
capabilities[1] = 1; // storage
|
||||
capabilities[2..].* = [_]u8{0} ** 6;
|
||||
|
||||
const response = DiscoveryResponse{
|
||||
.responder_id = responder_id,
|
||||
.address = address,
|
||||
.capabilities = capabilities,
|
||||
.protocol_version = 2,
|
||||
.timestamp = std.time.timestamp(),
|
||||
};
|
||||
|
||||
var buffer = Buffer.init(allocator);
|
||||
defer buffer.deinit();
|
||||
|
||||
try response.encode(&buffer);
|
||||
|
||||
// Decode and verify
|
||||
const decoded = try DiscoveryResponse.decode(&buffer);
|
||||
try testing.expectEqual(responder_id, decoded.responder_id);
|
||||
try testing.expectEqual(address, decoded.address);
|
||||
try testing.expectEqual(@as(u16, 2), decoded.protocol_version);
|
||||
try testing.expect(decoded.timestamp > 0);
|
||||
try testing.expectEqual(@as(u8, 1), decoded.capabilities[0]);
|
||||
try testing.expectEqual(@as(u8, 1), decoded.capabilities[1]);
|
||||
}
|
||||
|
||||
test "Discovery message type handling" {
|
||||
// Test that we can distinguish between different message types
|
||||
try testing.expectEqual(@as(u8, 1), @intFromEnum(DiscoveryMessageType.DiscoveryRequest));
|
||||
try testing.expectEqual(@as(u8, 2), @intFromEnum(DiscoveryMessageType.DiscoveryResponse));
|
||||
try testing.expectEqual(@as(u8, 3), @intFromEnum(DiscoveryMessageType.PeerAdvertisement));
|
||||
try testing.expectEqual(@as(u8, 4), @intFromEnum(DiscoveryMessageType.KeepAlive));
|
||||
try testing.expectEqual(@as(u8, 5), @intFromEnum(DiscoveryMessageType.Goodbye));
|
||||
}
|
||||
|
||||
test "Discovery configuration" {
|
||||
var config = DiscoveryConfig{
|
||||
.broadcast_address = Address.init(),
|
||||
.discovery_port = 9999,
|
||||
.timeout_ms = 5000,
|
||||
.max_peers = 100,
|
||||
.interval_seconds = 30,
|
||||
.multicast_ttl = 4,
|
||||
};
|
||||
|
||||
try testing.expectEqual(@as(u16, 9999), config.discovery_port);
|
||||
try testing.expectEqual(@as(u32, 5000), config.timeout_ms);
|
||||
try testing.expectEqual(@as(u32, 100), config.max_peers);
|
||||
try testing.expectEqual(@as(u32, 30), config.interval_seconds);
|
||||
try testing.expectEqual(@as(u8, 4), config.multicast_ttl);
|
||||
}
|
||||
|
||||
test "Multicast discovery configuration" {
|
||||
const link_config = MulticastDiscovery.createConfig(.link_local, 8080);
|
||||
try testing.expectEqual(@as(u16, 8080), link_config.discovery_port);
|
||||
try testing.expectEqual(@as(u8, 1), link_config.multicast_ttl);
|
||||
|
||||
const site_config = MulticastDiscovery.createConfig(.site_local, 9090);
|
||||
try testing.expectEqual(@as(u16, 9090), site_config.discovery_port);
|
||||
try testing.expectEqual(@as(u8, 4), site_config.multicast_ttl);
|
||||
}
|
||||
|
||||
test "Discovery manager initialization" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = DiscoveryConfig{
|
||||
.broadcast_address = Address.init(),
|
||||
.discovery_port = 9999,
|
||||
};
|
||||
|
||||
var manager = try DiscoveryManager.init(allocator, config);
|
||||
defer manager.deinit();
|
||||
|
||||
// Check initial state
|
||||
try testing.expectEqual(@as(DiscoveryState, .Stopped), manager.state);
|
||||
try testing.expect(!manager.isRunning());
|
||||
|
||||
const stats = manager.getStats();
|
||||
try testing.expectEqual(@as(u32, 0), stats.requests_sent);
|
||||
try testing.expectEqual(@as(u32, 0), stats.responses_received);
|
||||
try testing.expectEqual(@as(u32, 0), stats.peers_discovered);
|
||||
}
|
||||
|
||||
test "Discovery manager lifecycle" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = DiscoveryConfig{
|
||||
.broadcast_address = Address.init(),
|
||||
.discovery_port = 9999,
|
||||
.timeout_ms = 100, // Short timeout for testing
|
||||
};
|
||||
|
||||
var manager = try DiscoveryManager.init(allocator, config);
|
||||
defer manager.deinit();
|
||||
|
||||
// Start discovery
|
||||
try manager.start();
|
||||
try testing.expect(manager.isRunning());
|
||||
try testing.expectEqual(@as(DiscoveryState, .Scanning), manager.state);
|
||||
|
||||
// Stop discovery
|
||||
manager.stop();
|
||||
try testing.expect(!manager.isRunning());
|
||||
try testing.expectEqual(@as(DiscoveryState, .Stopped), manager.state);
|
||||
}
|
||||
|
||||
test "Discovery manager discovered peers tracking" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = DiscoveryConfig{
|
||||
.broadcast_address = Address.init(),
|
||||
.discovery_port = 9999,
|
||||
.max_peers = 10,
|
||||
};
|
||||
|
||||
var manager = try DiscoveryManager.init(allocator, config);
|
||||
defer manager.deinit();
|
||||
|
||||
try manager.start();
|
||||
|
||||
// Initially no discovered peers
|
||||
const initial_peers = manager.getDiscoveredPeers();
|
||||
try testing.expectEqual(@as(usize, 0), initial_peers.len);
|
||||
|
||||
// Add some discovered peers manually (simulating discovery)
|
||||
var address1 = Address.init();
|
||||
address1.port = 8080;
|
||||
var address2 = Address.init();
|
||||
address2.port = 9090;
|
||||
|
||||
try manager.discovered_peers.append(address1);
|
||||
try manager.discovered_peers.append(address2);
|
||||
|
||||
const discovered_peers = manager.getDiscoveredPeers();
|
||||
try testing.expectEqual(@as(usize, 2), discovered_peers.len);
|
||||
|
||||
// Check peer addresses
|
||||
var found_address1 = false;
|
||||
var found_address2 = false;
|
||||
|
||||
for (discovered_peers) |addr| {
|
||||
if (addr.port == 8080) found_address1 = true;
|
||||
if (addr.port == 9090) found_address2 = true;
|
||||
}
|
||||
|
||||
try testing.expect(found_address1);
|
||||
try testing.expect(found_address2);
|
||||
}
|
||||
|
||||
test "Discovery statistics tracking" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = DiscoveryConfig{
|
||||
.broadcast_address = Address.init(),
|
||||
.discovery_port = 9999,
|
||||
};
|
||||
|
||||
var manager = try DiscoveryManager.init(allocator, config);
|
||||
defer manager.deinit();
|
||||
|
||||
try manager.start();
|
||||
|
||||
// Simulate discovery activity
|
||||
manager.stats.requests_sent = 10;
|
||||
manager.stats.responses_received = 7;
|
||||
manager.stats.peers_discovered = 5;
|
||||
|
||||
const stats = manager.getStats();
|
||||
try testing.expectEqual(@as(u32, 10), stats.requests_sent);
|
||||
try testing.expectEqual(@as(u32, 7), stats.responses_received);
|
||||
try testing.expectEqual(@as(u32, 5), stats.peers_discovered);
|
||||
|
||||
// Calculate success rate
|
||||
try testing.expectEqual(@as(u8, 70), stats.success_rate); // 7/10 * 100
|
||||
}
|
||||
|
||||
test "Discovery manager peer cleanup" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = DiscoveryConfig{
|
||||
.broadcast_address = Address.init(),
|
||||
.discovery_port = 9999,
|
||||
.max_peers = 5,
|
||||
};
|
||||
|
||||
var manager = try DiscoveryManager.init(allocator, config);
|
||||
defer manager.deinit();
|
||||
|
||||
try manager.start();
|
||||
|
||||
// Add peers up to the limit
|
||||
for (0..5) |i| {
|
||||
var address = Address.init();
|
||||
address.port = @as(u16, 8000 + i);
|
||||
try manager.discovered_peers.append(address);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 5), manager.discovered_peers.items.len);
|
||||
|
||||
// Add one more peer (should trigger cleanup)
|
||||
var extra_address = Address.init();
|
||||
extra_address.port = 9999;
|
||||
try manager.discovered_peers.append(extra_address);
|
||||
|
||||
// Cleanup should remove excess peers
|
||||
manager.cleanup();
|
||||
|
||||
// Should still have at most max_peers
|
||||
try testing.expect(manager.discovered_peers.items.len <= config.max_peers);
|
||||
}
|
||||
|
||||
test "Discovery manager maintenance operations" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = DiscoveryConfig{
|
||||
.broadcast_address = Address.init(),
|
||||
.discovery_port = 9999,
|
||||
.interval_seconds = 0, // Zero interval for fast testing
|
||||
};
|
||||
|
||||
var manager = try DiscoveryManager.init(allocator, config);
|
||||
defer manager.deinit();
|
||||
|
||||
try manager.start();
|
||||
|
||||
const local_peer_id = PeerId.init();
|
||||
const local_port = 8080;
|
||||
|
||||
// First maintenance call should trigger discovery
|
||||
const start_time = std.time.timestamp();
|
||||
_ = start_time;
|
||||
try manager.maintain(local_peer_id, local_port);
|
||||
|
||||
// Should have sent a request
|
||||
const stats1 = manager.getStats();
|
||||
try testing.expect(stats1.requests_sent >= 1);
|
||||
|
||||
// Brief pause then call maintenance again (interval=0 allows immediate re-send)
|
||||
std.time.sleep(10 * std.time.ns_per_ms);
|
||||
try manager.maintain(local_peer_id, local_port);
|
||||
|
||||
// Should have sent another request
|
||||
const stats2 = manager.getStats();
|
||||
try testing.expect(stats2.requests_sent >= 2);
|
||||
}
|
||||
|
||||
test "Discovery message decoding error handling" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Test with invalid message type
|
||||
var buffer = Buffer.init(allocator);
|
||||
defer buffer.deinit();
|
||||
|
||||
try buffer.writeByte(255); // Invalid message type
|
||||
|
||||
// Should fail to decode
|
||||
const result = DiscoveryRequest.decode(&buffer);
|
||||
try testing.expectError(error.InvalidMessageType, result);
|
||||
}
|
||||
|
||||
test "Discovery response with different capabilities" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var response = DiscoveryResponse{
|
||||
.responder_id = PeerId.init(),
|
||||
.address = Address.init(),
|
||||
.capabilities = [8]u8{1, 0, 1, 0, 1, 0, 0, 0}, // Mixed capabilities
|
||||
.protocol_version = 3,
|
||||
.timestamp = std.time.timestamp(),
|
||||
};
|
||||
|
||||
var buffer = Buffer.init(allocator);
|
||||
defer buffer.deinit();
|
||||
|
||||
try response.encode(&buffer);
|
||||
const decoded = try DiscoveryResponse.decode(&buffer);
|
||||
|
||||
try testing.expectEqual(@as(u8, 1), decoded.capabilities[0]); // relay
|
||||
try testing.expectEqual(@as(u8, 0), decoded.capabilities[1]); // storage
|
||||
try testing.expectEqual(@as(u8, 1), decoded.capabilities[2]); // discovery
|
||||
try testing.expectEqual(@as(u16, 3), decoded.protocol_version);
|
||||
}
|
||||
|
||||
test "Discovery manager multiple instances" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config1 = DiscoveryConfig{
|
||||
.broadcast_address = Address.init(),
|
||||
.discovery_port = 9999,
|
||||
};
|
||||
|
||||
var config2 = DiscoveryConfig{
|
||||
.broadcast_address = Address.init(),
|
||||
.discovery_port = 9998,
|
||||
};
|
||||
|
||||
var manager1 = try DiscoveryManager.init(allocator, config1);
|
||||
defer manager1.deinit();
|
||||
|
||||
var manager2 = try DiscoveryManager.init(allocator, config2);
|
||||
defer manager2.deinit();
|
||||
|
||||
// Both should be independent
|
||||
try manager1.start();
|
||||
try manager2.start();
|
||||
|
||||
try testing.expect(manager1.isRunning());
|
||||
try testing.expect(manager2.isRunning());
|
||||
|
||||
// They should have different states
|
||||
manager1.stop();
|
||||
try testing.expect(!manager1.isRunning());
|
||||
try testing.expect(manager2.isRunning()); // manager2 should still be running
|
||||
|
||||
manager2.stop();
|
||||
}
|
||||
|
||||
test "Discovery performance test" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = DiscoveryConfig{
|
||||
.broadcast_address = Address.init(),
|
||||
.discovery_port = 9999,
|
||||
.max_peers = 1000,
|
||||
};
|
||||
|
||||
var manager = try DiscoveryManager.init(allocator, config);
|
||||
defer manager.deinit();
|
||||
|
||||
try manager.start();
|
||||
|
||||
const start_time = std.time.timestamp();
|
||||
|
||||
// Simulate discovering many peers
|
||||
for (0..1000) |i| {
|
||||
var address = Address.init();
|
||||
address.port = @as(u16, 8000 + i);
|
||||
try manager.discovered_peers.append(address);
|
||||
}
|
||||
|
||||
const end_time = std.time.timestamp();
|
||||
|
||||
// Should be able to handle many peers efficiently
|
||||
try testing.expect(end_time - start_time < 1); // Less than 1 second
|
||||
|
||||
const peers = manager.getDiscoveredPeers();
|
||||
try testing.expectEqual(@as(usize, 1000), peers.len);
|
||||
|
||||
// Test cleanup performance
|
||||
const cleanup_start = std.time.timestamp();
|
||||
manager.cleanup();
|
||||
const cleanup_end = std.time.timestamp();
|
||||
|
||||
try testing.expect(cleanup_end - cleanup_start < 1); // Cleanup should be fast
|
||||
}
|
||||
|
||||
test "Discovery edge cases" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = DiscoveryConfig{
|
||||
.broadcast_address = Address.init(),
|
||||
.discovery_port = 9999,
|
||||
.timeout_ms = 0, // Edge case: zero timeout
|
||||
.max_peers = 0, // Edge case: zero max peers
|
||||
};
|
||||
|
||||
var manager = try DiscoveryManager.init(allocator, config);
|
||||
defer manager.deinit();
|
||||
|
||||
try manager.start();
|
||||
|
||||
// Should handle edge case configurations gracefully
|
||||
try testing.expect(manager.isRunning());
|
||||
|
||||
const stats = manager.getStats();
|
||||
try testing.expectEqual(@as(u32, 0), stats.requests_sent);
|
||||
|
||||
// Test with zero timeout - listen should return quickly
|
||||
const listen_start = std.time.timestamp();
|
||||
_ = manager.listen(100) catch {}; // 100ms timeout
|
||||
const listen_end = std.time.timestamp();
|
||||
|
||||
// Should return quickly even with zero timeout (bounded by provided timeout)
|
||||
try testing.expect(listen_end - listen_start < 1); // Less than 1 second
|
||||
}
|
||||
Reference in New Issue
Block a user