const std = @import("std"); const testing = std.testing; const core = @import("core"); const Connection = core.connection.Connection; const ConnectionType = core.connection.ConnectionType; const ConnectionState = core.connection.ConnectionState; const ConnectionConfig = core.connection.ConnectionConfig; const ConnectionPool = core.connection.ConnectionPool; const ConnectionStats = core.connection.ConnectionStats; const HealthStatus = core.connection.HealthStatus; const Address = core.address.Address; const Error = core.errors.Error; test "connection: create TCP connection succeeds" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); try testing.expectEqual(ConnectionState.disconnected, conn.state); try testing.expectEqual(ConnectionType.tcp, conn.connection_type); } test "connection: create UDP connection succeeds" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .udp }; var conn = try Connection.init(.udp, config, allocator); defer conn.deinit(); try testing.expectEqual(ConnectionType.udp, conn.connection_type); } test "connection: connection has unique ID" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn1 = try Connection.init(.tcp, config, allocator); defer conn1.deinit(); var conn2 = try Connection.init(.tcp, config, allocator); defer conn2.deinit(); try testing.expect(conn1.getId() != conn2.getId()); } test "connection: stats initialization" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); const stats = conn.getStats(); try testing.expectEqual(@as(u64, 0), stats.getBytesSent()); try testing.expectEqual(@as(u64, 0), stats.getBytesReceived()); try testing.expectEqual(@as(u64, 0), stats.getConnectionAttempts()); } test "connection: stats record send" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); const stats = conn.getStatsMut(); stats.recordSend(1024, 1); try testing.expectEqual(@as(u64, 1024), conn.getStats().getBytesSent()); try testing.expectEqual(@as(u64, 1), conn.getStats().getMessagesSent()); } test "connection: stats record receive" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); const stats = conn.getStatsMut(); stats.recordReceive(2048, 2); try testing.expectEqual(@as(u64, 2048), conn.getStats().getBytesReceived()); try testing.expectEqual(@as(u64, 2), conn.getStats().getMessagesReceived()); } test "connection: stats record connection attempt" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); const stats = conn.getStatsMut(); stats.recordConnectionAttempt(); try testing.expectEqual(@as(u64, 1), conn.getStats().getConnectionAttempts()); } test "connection: stats record successful connection" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); const stats = conn.getStatsMut(); stats.recordSuccessfulConnection(); try testing.expectEqual(@as(u64, 1), conn.getStats().getSuccessfulConnections()); } test "connection: stats record failed connection" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); const stats = conn.getStatsMut(); stats.recordFailedConnection(); try testing.expectEqual(@as(u64, 1), conn.getStats().getFailedConnections()); } test "connection: stats record error" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); const stats = conn.getStatsMut(); stats.recordError(); try testing.expectEqual(@as(u64, 1), conn.getStats().getErrors()); } test "connection: state machine from disconnected to failed" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); try testing.expectEqual(ConnectionState.disconnected, conn.getState()); // Try to connect to invalid address const addr = try Address.parse("127.0.0.1:59999"); _ = conn.connect(addr) catch {}; try testing.expectEqual(ConnectionState.failed, conn.getState()); } test "connection: UDP connection state transitions" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .udp }; var conn = try Connection.init(.udp, config, allocator); defer conn.deinit(); try testing.expectEqual(ConnectionState.disconnected, conn.getState()); const addr = try Address.parse("127.0.0.1:0"); try conn.connect(addr); try testing.expectEqual(ConnectionState.connected, conn.getState()); } test "connection: disconnect closes connection" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .udp }; var conn = try Connection.init(.udp, config, allocator); defer conn.deinit(); const addr = try Address.parse("127.0.0.1:0"); try conn.connect(addr); try conn.disconnect(); try testing.expectEqual(ConnectionState.closed, conn.getState()); } test "connection: send on disconnected connection fails" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); const result = conn.send("test"); try testing.expectError(Error.InvalidConnectionState, result); } test "connection: isActive returns false for disconnected" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); try testing.expect(!conn.isActive()); } test "connection: isActive returns true for connected" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .udp }; var conn = try Connection.init(.udp, config, allocator); defer conn.deinit(); const addr = try Address.parse("127.0.0.1:0"); try conn.connect(addr); try testing.expect(conn.isActive()); } test "connection: health check on disconnected returns unhealthy" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); const health = conn.checkHealth(); try testing.expectEqual(HealthStatus.unhealthy, health.status); } test "connection: health check on connected UDP returns healthy" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .udp }; var conn = try Connection.init(.udp, config, allocator); defer conn.deinit(); const addr = try Address.parse("127.0.0.1:0"); try conn.connect(addr); const health = conn.checkHealth(); try testing.expectEqual(HealthStatus.healthy, health.status); } test "connection: update health status changes connection state" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); const health = conn.checkHealth(); conn.updateHealthStatus(health); try testing.expectEqual(HealthStatus.unhealthy, conn.getHealthStatus()); } test "connection: graceful shutdown on disconnected is safe" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); try conn.gracefulShutdown(1000); try testing.expectEqual(ConnectionState.disconnected, conn.getState()); } test "connection pool: create and destroy" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var pool = ConnectionPool.init(config, allocator); defer pool.deinit(); try testing.expectEqual(@as(usize, 0), pool.connections.items.len); } test "connection pool: create connection" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var pool = ConnectionPool.init(config, allocator); defer pool.deinit(); _ = try pool.createConnection(.tcp); try testing.expectEqual(@as(usize, 1), pool.connections.items.len); } test "connection pool: create multiple connections" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var pool = ConnectionPool.init(config, allocator); defer pool.deinit(); _ = try pool.createConnection(.tcp); _ = try pool.createConnection(.udp); try testing.expectEqual(@as(usize, 2), pool.connections.items.len); } test "connection pool: remove connection" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var pool = ConnectionPool.init(config, allocator); defer pool.deinit(); const conn = try pool.createConnection(.tcp); pool.removeConnection(conn); try testing.expectEqual(@as(usize, 0), pool.connections.items.len); } test "connection pool: get active connections" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .udp }; var pool = ConnectionPool.init(config, allocator); defer pool.deinit(); const conn = try pool.createConnection(.udp); const addr = try Address.parse("127.0.0.1:0"); try conn.connect(addr); var active = try pool.getActiveConnections(); defer active.deinit(allocator); try testing.expectEqual(@as(usize, 1), active.items.len); } test "connection pool: pool stats tracking" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var pool = ConnectionPool.init(config, allocator); defer pool.deinit(); _ = try pool.createConnection(.tcp); _ = try pool.createConnection(.tcp); const stats = pool.getPoolStats(); try testing.expectEqual(@as(usize, 2), stats.total_connections); } test "connection: buffer creation" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp, .buffer_size = 4096, }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); var buffer = try conn.createSendBuffer(); defer buffer.release(); try testing.expect(buffer.base.capacity >= 4096); } test "connection: concurrent stats updates" { const allocator = testing.allocator; const config = ConnectionConfig{ .connection_type = .tcp }; var conn = try Connection.init(.tcp, config, allocator); defer conn.deinit(); const stats = conn.getStatsMut(); stats.recordSend(100, 1); stats.recordSend(200, 1); stats.recordReceive(300, 1); try testing.expectEqual(@as(u64, 300), conn.getStats().getBytesSent()); try testing.expectEqual(@as(u64, 2), conn.getStats().getMessagesSent()); try testing.expectEqual(@as(u64, 300), conn.getStats().getBytesReceived()); }