ab ovo
This commit is contained in:
85
examples/integration_test.zig
Normal file
85
examples/integration_test.zig
Normal file
@@ -0,0 +1,85 @@
|
||||
//! P2P Integration Test
|
||||
//!
|
||||
//! Tests real peer-to-peer communication between two P2P instances.
|
||||
|
||||
const std = @import("std");
|
||||
const p2p_lib = @import("p2p");
|
||||
const core = @import("core");
|
||||
|
||||
const P2P = p2p_lib.P2P;
|
||||
const Address = core.address.Address;
|
||||
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.page_allocator;
|
||||
|
||||
std.debug.print("=== P2P Integration Test ===\n\n", .{});
|
||||
|
||||
// Test 1: Create two P2P instances
|
||||
std.debug.print("1. Creating two P2P instances...\n", .{});
|
||||
|
||||
var config1 = p2p_lib.P2PConfig.default();
|
||||
config1.network.listen_port = 29998; // Use different ports for discovery
|
||||
|
||||
var config2 = p2p_lib.P2PConfig.default();
|
||||
config2.network.listen_port = 29997; // Use different ports for discovery
|
||||
|
||||
var p2p1 = try P2P.init(allocator, config1);
|
||||
defer p2p1.deinit();
|
||||
|
||||
var p2p2 = try P2P.init(allocator, config2);
|
||||
defer p2p2.deinit();
|
||||
|
||||
std.debug.print(" Created P2P instance 1 and 2\n", .{});
|
||||
|
||||
// Test 2: Start instances
|
||||
std.debug.print("2. Starting P2P instances...\n", .{});
|
||||
try p2p1.start();
|
||||
try p2p2.start();
|
||||
std.debug.print(" Both instances started\n", .{});
|
||||
|
||||
// Test 3: P2P1 listens
|
||||
std.debug.print("3. P2P1 starting to listen...\n", .{});
|
||||
const listen_addr = try Address.parse("127.0.0.1:29999");
|
||||
try p2p1.startListening(listen_addr);
|
||||
std.debug.print(" P2P1 listening on port 29999\n", .{});
|
||||
|
||||
// Test 4: P2P2 connects to P2P1
|
||||
std.debug.print("4. P2P2 connecting to P2P1...\n", .{});
|
||||
const peer_id = try p2p2.connectToPeer(listen_addr);
|
||||
std.debug.print(" Connected! Peer ID: {x}\n", .{peer_id.bytes[0..8].*});
|
||||
|
||||
// Test 5: P2P1 accepts the connection
|
||||
std.debug.print("5. P2P1 accepting connection...\n", .{});
|
||||
try p2p1.update(); // Process accept
|
||||
std.debug.print(" Connection accepted\n", .{});
|
||||
|
||||
// Test 6: P2P2 sends message
|
||||
std.debug.print("6. P2P2 sending message...\n", .{});
|
||||
const test_message = "Hello from P2P2!";
|
||||
const msg_id = try p2p2.sendMessage(peer_id, 0x80, test_message);
|
||||
std.debug.print(" Sent message ID: {d}\n", .{msg_id});
|
||||
|
||||
// Test 7: P2P1 receives message
|
||||
std.debug.print("7. P2P1 receiving message...\n", .{});
|
||||
try p2p1.update(); // Process receive
|
||||
std.debug.print(" Message received\n", .{});
|
||||
|
||||
// Test 8: Check stats
|
||||
std.debug.print("8. Checking statistics...\n", .{});
|
||||
std.debug.print(" P2P1 connections: {d}\n", .{p2p1.stats.active_connections.load(.monotonic)});
|
||||
std.debug.print(" P2P2 connections: {d}\n", .{p2p2.stats.active_connections.load(.monotonic)});
|
||||
std.debug.print(" P2P2 messages sent: {d}\n", .{p2p2.stats.messages_sent.load(.monotonic)});
|
||||
|
||||
// Test 9: Disconnect
|
||||
std.debug.print("9. Disconnecting...\n", .{});
|
||||
try p2p2.disconnectPeer(peer_id);
|
||||
std.debug.print(" Disconnected\n", .{});
|
||||
|
||||
// Test 10: Stop
|
||||
std.debug.print("10. Stopping instances...\n", .{});
|
||||
try p2p1.stop();
|
||||
try p2p2.stop();
|
||||
std.debug.print(" Stopped\n", .{});
|
||||
|
||||
std.debug.print("\n=== All Integration Tests Passed ===\n", .{});
|
||||
}
|
||||
Reference in New Issue
Block a user