#!/bin/bash # Test runner script for P2P library core networking module # This script compiles and runs all tests for the core networking components set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to print colored output print_status() { local color=$1 local message=$2 echo -e "${color}${message}${NC}" } print_status $GREEN "=== P2P Library Core Networking Tests ===" print_status $YELLOW "Running comprehensive test suite..." # Check if Zig is installed if ! command -v zig &> /dev/null; then print_status $RED "Error: Zig is not installed or not in PATH" exit 1 fi print_status $GREEN "Zig version: $(zig version)" # Change to the project root directory cd "$(dirname "$0")/.." # Clean up any previous build artifacts print_status $YELLOW "Cleaning up previous build artifacts..." rm -rf zig-cache/ zig-out/ # Run individual module tests print_status $GREEN "=== Running Error Module Tests ===" zig test test/core/test_error.zig -femit-bin=test/core/test_error -O ReleaseSafe 2>&1 | tee test_results/error.log if [ $? -eq 0 ]; then print_status $GREEN "✓ Error module tests passed" else print_status $RED "✗ Error module tests failed" fi print_status $GREEN "=== Running Address Module Tests ===" zig test test/core/test_address.zig -femit-bin=test/core/test_address -O ReleaseSafe 2>&1 | tee test_results/address.log if [ $? -eq 0 ]; then print_status $GREEN "✓ Address module tests passed" else print_status $RED "✗ Address module tests failed" fi print_status $GREEN "=== Running Buffer Module Tests ===" zig test test/core/test_buffer.zig -femit-bin=test/core/test_buffer -O ReleaseSafe 2>&1 | tee test_results/buffer.log if [ $? -eq 0 ]; then print_status $GREEN "✓ Buffer module tests passed" else print_status $RED "✗ Buffer module tests failed" fi print_status $GREEN "=== Running Socket Module Tests ===" zig test test/core/test_socket.zig -femit-bin=test/core/test_socket -O ReleaseSafe 2>&1 | tee test_results/socket.log if [ $? -eq 0 ]; then print_status $GREEN "✓ Socket module tests passed" else print_status $RED "✗ Socket module tests failed" fi print_status $GREEN "=== Running Connection Module Tests ===" zig test test/core/test_connection.zig -femit-bin=test/core/test_connection -O ReleaseSafe 2>&1 | tee test_results/connection.log if [ $? -eq 0 ]; then print_status $GREEN "✓ Connection module tests passed" else print_status $RED "✗ Connection module tests failed" fi # Run integration tests print_status $GREEN "=== Running Integration Tests ===" zig test test/core/test_main.zig -femit-bin=test/core/test_main -O ReleaseSafe 2>&1 | tee test_results/integration.log if [ $? -eq 0 ]; then print_status $GREEN "✓ Integration tests passed" else print_status $RED "✗ Integration tests failed" fi # Compile and test the complete core module print_status $GREEN "=== Compiling Core Module ===" zig build-lib src/core/*.zig -femit-bin=zig-out/lib/core.a -O ReleaseSafe 2>&1 | tee test_results/build.log if [ $? -eq 0 ]; then print_status $GREEN "✓ Core module compiled successfully" else print_status $RED "✗ Core module compilation failed" fi # Test the complete core module as a library print_status $GREEN "=== Testing Core Module as Library ===" zig test test/core/test_main.zig --pkg-begin core src/core/*.zig -O ReleaseSafe 2>&1 | tee test_results/library.log if [ $? -eq 0 ]; then print_status $GREEN "✓ Core module library tests passed" else print_status $RED "✗ Core module library tests failed" fi # Generate test coverage report print_status $GREEN "=== Generating Coverage Report ===" mkdir -p test_results/coverage zig test --test-coverage test/core/test_main.zig -O ReleaseSafe 2>&1 | tee test_results/coverage/coverage.log if [ $? -eq 0 ]; then print_status $GREEN "✓ Coverage report generated" else print_status $YELLOW "⚠ Coverage report generation skipped" fi # Performance benchmarks print_status $GREEN "=== Running Performance Benchmarks ===" zig build-exe benchmark.zig -O ReleaseSafe -femit-bin=benchmark 2>&1 | tee test_results/benchmark_build.log if [ $? -eq 0 ]; then ./benchmark 2>&1 | tee test_results/benchmark.log if [ $? -eq 0 ]; then print_status $GREEN "✓ Performance benchmarks completed" else print_status $YELLOW "⚠ Performance benchmarks failed" fi else print_status $YELLOW "⚠ Benchmark build failed" fi # Summary print_status $GREEN "=== Test Summary ===" total_tests=6 passed_tests=0 # Count passed tests for test_type in error address buffer socket connection integration; do if [ -f "test_results/$test_type.log" ]; then if grep -q "All tests passed" "test_results/$test_type.log"; then ((passed_tests++)) fi fi done print_status $YELLOW "Passed: $passed_tests/$total_tests test suites" print_status $YELLOW "Failed: $((total_tests - passed_tests))/$total_tests test suites" # Detailed test results print_status $GREEN "=== Detailed Test Results ===" for test_type in error address buffer socket connection integration; do if [ -f "test_results/$test_type.log" ]; then echo "--- $test_type module ---" grep -E "(PASSED|FAILED|All tests passed|tests passed)" "test_results/$test_type.log" || echo "No summary found" echo "" fi done # Memory usage analysis if command -v valgrind &> /dev/null; then print_status $GREEN "=== Running Memory Analysis ===" valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes zig test test/core/test_main.zig -O ReleaseSafe 2>&1 | tee test_results/valgrind.log if [ $? -eq 0 ]; then print_status $GREEN "✓ Memory analysis completed" else print_status $YELLOW "⚠ Memory analysis failed" fi else print_status $YELLOW "Valgrind not available, skipping memory analysis" fi # Create test report print_status $GREEN "=== Creating Test Report ===" cat > test_results/report.md << EOF # P2P Library Core Networking Test Report Generated: $(date) Zig Version: $(zig version) ## Test Results Summary - **Total Test Suites**: $total_tests - **Passed**: $passed_tests - **Failed**: $((total_tests - passed_tests)) - **Success Rate**: $((passed_tests * 100 / total_tests))% ## Module Coverage ### Error Handling Module - Comprehensive error types and handling - Error context and propagation - Error statistics and tracking - Global error management ### Address Module - IPv4/IPv6 address parsing and validation - Unix socket address support - Address comparison and conversion - Address resolution utilities ### Buffer Module - Zero-copy buffer operations - Memory pool allocation - Stream, packet, and message buffers - Buffer manager and pooling ### Socket Module - Cross-platform socket operations - TCP/UDP/Unix socket support - Socket options and configuration - Socket management and pooling ### Connection Module - High-level connection abstraction - Connection pooling and management - Health checking and monitoring - Graceful shutdown handling ## Performance Metrics $(if [ -f test_results/benchmark.log ]; then cat test_results/benchmark.log; fi) ## Memory Analysis $(if [ -f test_results/valgrind.log ]; then grep -A 10 "LEAK SUMMARY" test_results/valgrind.log; fi) ## Build Information - Compiler: Zig $(zig version) - Optimization: ReleaseSafe - Target: $(zig build --show-target 2>/dev/null || echo "Native") - Platform: $(uname -m) ## Test Environment - OS: $(uname -s) $(uname -r) - Architecture: $(uname -m) - Available Memory: $(free -h | grep "Mem:" | awk '{print $2}' | head -1) EOF print_status $GREEN "Test report saved to test_results/report.md" # Final status if [ $passed_tests -eq $total_tests ]; then print_status $GREEN "🎉 All tests passed successfully!" exit 0 else print_status $RED "❌ Some tests failed. Check test_results/ for details." exit 1 fi