ab ovo
This commit is contained in:
445
tests/integration/test_concurrent_operations.zig
Normal file
445
tests/integration/test_concurrent_operations.zig
Normal file
@@ -0,0 +1,445 @@
|
||||
//! Concurrent Operations Integration Tests
|
||||
//!
|
||||
//! Tests that multiple operations can run concurrently without race conditions,
|
||||
//! deadlocks, or data corruption.
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
const p2p_lib = @import("p2p");
|
||||
const core = @import("core");
|
||||
const network = @import("network");
|
||||
|
||||
const P2P = p2p_lib.P2P;
|
||||
const P2PConfig = p2p_lib.P2PConfig;
|
||||
const Address = core.Address;
|
||||
const PeerId = network.peer.PeerId;
|
||||
|
||||
test "concurrent: multiple sequential message sends" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = P2PConfig.default();
|
||||
config.network.listen_port = 19401;
|
||||
config.protocol.max_message_size = 1024;
|
||||
|
||||
var peer = try P2P.init(allocator, config);
|
||||
defer peer.deinit();
|
||||
|
||||
try peer.start();
|
||||
_ = peer.processAllEvents();
|
||||
|
||||
const num_messages = 100;
|
||||
const test_msg = "Test message for concurrent sending";
|
||||
|
||||
// Send multiple messages sequentially (simulating rapid sends)
|
||||
var sent_count: usize = 0;
|
||||
for (0..num_messages) |_| {
|
||||
// Send without a destination (broadcast attempt)
|
||||
_ = peer.sendMessage(null, 0x01, test_msg) catch |_| {
|
||||
// Expected to fail without connections
|
||||
continue;
|
||||
};
|
||||
sent_count += 1;
|
||||
}
|
||||
|
||||
// Update to process
|
||||
for (0..10) |_| {
|
||||
try peer.update();
|
||||
std.time.sleep(1 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
const stats = peer.getStats();
|
||||
std.debug.print("Sent {} messages, stats show {} messages_sent\n", .{ sent_count, stats.messages_sent });
|
||||
|
||||
// Verify no crashes and stats are reasonable
|
||||
try testing.expect(stats.messages_sent <= num_messages);
|
||||
}
|
||||
|
||||
test "concurrent: rapid connect and disconnect" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config_server = P2PConfig.default();
|
||||
config_server.network.listen_port = 19411;
|
||||
|
||||
var server = try P2P.init(allocator, config_server);
|
||||
defer server.deinit();
|
||||
|
||||
try server.start();
|
||||
_ = server.processAllEvents();
|
||||
|
||||
var listen_addr = Address.init();
|
||||
listen_addr.port = 19411;
|
||||
listen_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } };
|
||||
try server.startListening(listen_addr);
|
||||
|
||||
std.time.sleep(50 * std.time.ns_per_ms);
|
||||
|
||||
var config_client = P2PConfig.default();
|
||||
config_client.network.listen_port = 19412;
|
||||
|
||||
var client = try P2P.init(allocator, config_client);
|
||||
defer client.deinit();
|
||||
|
||||
try client.start();
|
||||
_ = client.processAllEvents();
|
||||
|
||||
// Rapid connect/disconnect cycles
|
||||
const num_cycles = 5;
|
||||
for (0..num_cycles) |i| {
|
||||
var connect_addr = Address.init();
|
||||
connect_addr.port = 19411;
|
||||
connect_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } };
|
||||
|
||||
const peer_id = client.connectToPeer(connect_addr) catch |err| {
|
||||
std.debug.print("Cycle {} connect failed: {}\n", .{ i, err });
|
||||
continue;
|
||||
};
|
||||
|
||||
// Short wait
|
||||
for (0..10) |_| {
|
||||
try server.update();
|
||||
try client.update();
|
||||
std.time.sleep(5 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
// Disconnect
|
||||
client.disconnectPeer(peer_id) catch |err| {
|
||||
std.debug.print("Cycle {} disconnect failed: {}\n", .{ i, err });
|
||||
};
|
||||
|
||||
// Short wait
|
||||
for (0..10) |_| {
|
||||
try server.update();
|
||||
try client.update();
|
||||
std.time.sleep(5 * std.time.ns_per_ms);
|
||||
}
|
||||
}
|
||||
|
||||
const final_stats = client.getStats();
|
||||
std.debug.print("After {} cycles: {} total connections\n", .{ num_cycles, final_stats.total_connections });
|
||||
|
||||
// Verify system is still operational
|
||||
try testing.expect(client.isOperational());
|
||||
}
|
||||
|
||||
test "concurrent: multiple event processing" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = P2PConfig.default();
|
||||
config.network.listen_port = 19421;
|
||||
config.events.max_event_queue = 1000;
|
||||
|
||||
var peer = try P2P.init(allocator, config);
|
||||
defer peer.deinit();
|
||||
|
||||
try peer.start();
|
||||
|
||||
// Generate many events through state changes
|
||||
for (0..10) |_| {
|
||||
try peer.enterMaintenanceMode();
|
||||
try peer.exitMaintenanceMode();
|
||||
}
|
||||
|
||||
// Process all events
|
||||
const processed = peer.processAllEvents();
|
||||
std.debug.print("Processed {} events\n", .{processed});
|
||||
|
||||
// Verify no crashes
|
||||
try testing.expect(peer.isOperational());
|
||||
}
|
||||
|
||||
test "concurrent: stats updates during operations" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = P2PConfig.default();
|
||||
config.network.listen_port = 19431;
|
||||
config.library.enable_statistics = true;
|
||||
|
||||
var peer = try P2P.init(allocator, config);
|
||||
defer peer.deinit();
|
||||
|
||||
try peer.start();
|
||||
_ = peer.processAllEvents();
|
||||
|
||||
// Continuously read stats while doing operations
|
||||
for (0..100) |i| {
|
||||
// Get stats
|
||||
const stats = peer.getStats();
|
||||
_ = stats;
|
||||
|
||||
// Do operation
|
||||
if (i % 2 == 0) {
|
||||
try peer.update();
|
||||
} else {
|
||||
_ = peer.processAllEvents();
|
||||
}
|
||||
|
||||
std.time.sleep(1 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
std.debug.print("Stats updates completed without issues\n", .{});
|
||||
}
|
||||
|
||||
test "concurrent: multiple file operations" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = P2PConfig.default();
|
||||
config.network.listen_port = 19441;
|
||||
config.file_sharing.max_concurrent_uploads = 5;
|
||||
|
||||
var peer = try P2P.init(allocator, config);
|
||||
defer peer.deinit();
|
||||
|
||||
try peer.start();
|
||||
|
||||
// Create multiple small test files
|
||||
const num_files = 3;
|
||||
for (0..num_files) |i| {
|
||||
const path = try std.fmt.allocPrint(allocator, "test_concurrent_file_{}.txt", .{i});
|
||||
defer allocator.free(path);
|
||||
|
||||
// Create file
|
||||
var file = try std.fs.cwd().createFile(path, .{});
|
||||
defer file.close();
|
||||
try file.writeAll("Test content for concurrent file operations");
|
||||
|
||||
defer std.fs.cwd().deleteFile(path) catch {};
|
||||
|
||||
// Add to peer
|
||||
_ = peer.addFile(path) catch |err| {
|
||||
std.debug.print("Add file {} failed: {}\n", .{ i, err });
|
||||
continue;
|
||||
};
|
||||
}
|
||||
|
||||
// Update to process
|
||||
for (0..10) |_| {
|
||||
try peer.update();
|
||||
std.time.sleep(10 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
std.debug.print("Multiple file operations completed\n", .{});
|
||||
}
|
||||
|
||||
test "concurrent: simultaneous start and stop" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = P2PConfig.default();
|
||||
config.network.listen_port = 19451;
|
||||
|
||||
var peer = try P2P.init(allocator, config);
|
||||
defer peer.deinit();
|
||||
|
||||
// Start
|
||||
try peer.start();
|
||||
try testing.expect(peer.isOperational());
|
||||
|
||||
// Immediate stop
|
||||
try peer.stop();
|
||||
try testing.expect(!peer.isOperational());
|
||||
|
||||
// Start again
|
||||
try peer.start();
|
||||
try testing.expect(peer.isOperational());
|
||||
|
||||
// Stop again
|
||||
try peer.stop();
|
||||
try testing.expect(!peer.isOperational());
|
||||
|
||||
std.debug.print("Rapid start/stop completed successfully\n", .{});
|
||||
}
|
||||
|
||||
test "concurrent: event queue overflow handling" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = P2PConfig.default();
|
||||
config.network.listen_port = 19461;
|
||||
config.events.max_event_queue = 10; // Small queue for testing
|
||||
|
||||
var peer = try P2P.init(allocator, config);
|
||||
defer peer.deinit();
|
||||
|
||||
try peer.start();
|
||||
|
||||
// Generate more events than queue can hold
|
||||
for (0..50) |_| {
|
||||
try peer.enterMaintenanceMode();
|
||||
try peer.exitMaintenanceMode();
|
||||
}
|
||||
|
||||
// Check dropped events counter
|
||||
const stats = peer.getStats();
|
||||
const dropped = stats.events_dropped.load(.monotonic);
|
||||
std.debug.print("Events dropped due to overflow: {}\n", .{dropped});
|
||||
|
||||
// Verify system still works
|
||||
try testing.expect(peer.isOperational());
|
||||
|
||||
// Clear events
|
||||
_ = peer.processAllEvents();
|
||||
}
|
||||
|
||||
test "concurrent: config updates during runtime" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = P2PConfig.default();
|
||||
config.network.listen_port = 19471;
|
||||
config.file_sharing.max_bandwidth_download = 1024 * 1024;
|
||||
|
||||
var peer = try P2P.init(allocator, config);
|
||||
defer peer.deinit();
|
||||
|
||||
try peer.start();
|
||||
|
||||
// Update config multiple times
|
||||
for (0..10) |i| {
|
||||
var new_config = config;
|
||||
new_config.file_sharing.max_bandwidth_download = @intCast(1024 * 1024 + i * 1000);
|
||||
|
||||
try peer.updateConfig(new_config);
|
||||
|
||||
// Do some work
|
||||
try peer.update();
|
||||
std.time.sleep(5 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
std.debug.print("Runtime config updates completed\n", .{});
|
||||
}
|
||||
|
||||
test "concurrent: connection pool stress" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = P2PConfig.default();
|
||||
config.network.listen_port = 19481;
|
||||
config.network.max_connections = 100;
|
||||
|
||||
var peer = try P2P.init(allocator, config);
|
||||
defer peer.deinit();
|
||||
|
||||
try peer.start();
|
||||
_ = peer.processAllEvents();
|
||||
|
||||
var listen_addr = Address.init();
|
||||
listen_addr.port = 19481;
|
||||
listen_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } };
|
||||
try peer.startListening(listen_addr);
|
||||
|
||||
// Attempt many connections to non-existent peers (should fail gracefully)
|
||||
for (0..20) |i| {
|
||||
var addr = Address.init();
|
||||
addr.port = @intCast(20000 + i);
|
||||
addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } };
|
||||
|
||||
_ = peer.connectToPeer(addr) catch |_| {
|
||||
// Expected to fail
|
||||
continue;
|
||||
};
|
||||
|
||||
if (i % 5 == 0) {
|
||||
try peer.update();
|
||||
}
|
||||
}
|
||||
|
||||
// Update to process all failures
|
||||
for (0..20) |_| {
|
||||
try peer.update();
|
||||
std.time.sleep(5 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
const stats = peer.getStats();
|
||||
std.debug.print("Connection pool stress: {} errors\n", .{stats.errors_total.load(.monotonic)});
|
||||
|
||||
// Verify no crashes
|
||||
try testing.expect(peer.isOperational());
|
||||
}
|
||||
|
||||
test "concurrent: health check during heavy load" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = P2PConfig.default();
|
||||
config.network.listen_port = 19491;
|
||||
config.network.health_check_interval_ms = 100; // Frequent checks
|
||||
|
||||
var peer = try P2P.init(allocator, config);
|
||||
defer peer.deinit();
|
||||
|
||||
try peer.start();
|
||||
_ = peer.processAllEvents();
|
||||
|
||||
// Generate load while health checks run
|
||||
for (0..100) |_| {
|
||||
try peer.update(); // Triggers health checks periodically
|
||||
|
||||
// Do some work
|
||||
_ = peer.sendMessage(null, 0x01, "test") catch {};
|
||||
_ = peer.processAllEvents();
|
||||
|
||||
std.time.sleep(2 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
const stats = peer.getStats();
|
||||
std.debug.print("Completed heavy load with health checks, uptime: {}s\n", .{stats.uptime_seconds});
|
||||
|
||||
try testing.expect(peer.isOperational());
|
||||
}
|
||||
|
||||
test "concurrent: callback execution during event processing" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = P2PConfig.default();
|
||||
config.network.listen_port = 19501;
|
||||
|
||||
var peer = try P2P.init(allocator, config);
|
||||
defer peer.deinit();
|
||||
|
||||
// Register multiple callbacks
|
||||
const TestCallback = struct {
|
||||
fn callback1(_: p2p_lib.P2PEvent) void {}
|
||||
fn callback2(_: p2p_lib.P2PEvent) void {}
|
||||
fn callback3(_: p2p_lib.P2PEvent) void {}
|
||||
};
|
||||
|
||||
try peer.registerCallback(TestCallback.callback1, null);
|
||||
try peer.registerCallback(TestCallback.callback2, null);
|
||||
try peer.registerCallback(TestCallback.callback3, null);
|
||||
|
||||
try peer.start();
|
||||
|
||||
// Generate events
|
||||
for (0..20) |_| {
|
||||
try peer.enterMaintenanceMode();
|
||||
try peer.exitMaintenanceMode();
|
||||
}
|
||||
|
||||
// Process - all callbacks should be called for each event
|
||||
const processed = peer.processAllEvents();
|
||||
std.debug.print("Processed {} events with multiple callbacks\n", .{processed});
|
||||
|
||||
try testing.expect(processed > 0);
|
||||
}
|
||||
|
||||
test "concurrent: memory pressure handling" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var config = P2PConfig.default();
|
||||
config.network.listen_port = 19511;
|
||||
config.library.max_memory = 10 * 1024 * 1024; // 10MB limit
|
||||
|
||||
var peer = try P2P.init(allocator, config);
|
||||
defer peer.deinit();
|
||||
|
||||
try peer.start();
|
||||
|
||||
// Perform many operations to generate memory pressure
|
||||
for (0..100) |_| {
|
||||
try peer.update();
|
||||
_ = peer.processAllEvents();
|
||||
|
||||
const stats = peer.getStats();
|
||||
_ = stats;
|
||||
|
||||
std.time.sleep(1 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
std.debug.print("Memory pressure test completed\n", .{});
|
||||
try testing.expect(peer.isOperational());
|
||||
}
|
||||
Reference in New Issue
Block a user