mod common; #[test] fn test_rate_limiting_with_concurrent_requests() { let env = common::setup_test_environment(); // Create config with rate limiting enabled let config_content = format!(r#" root = "{}" cert = "{}" key = "{}" hostname = "localhost" bind_host = "127.0.0.1" port = {} max_concurrent_requests = 1 "#, env.content_path.display(), env.cert_path.display(), env.key_path.display(), env.port); std::fs::write(&env.config_path, config_content).unwrap(); // Start server binary with test delay to simulate processing time let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux")) .arg("--config") .arg(&env.config_path) .arg("--test-processing-delay") .arg("1") // 1 second delay per request .spawn() .expect("Failed to start server"); // Wait for server to start std::thread::sleep(std::time::Duration::from_millis(500)); // Spawn 5 concurrent client processes let mut handles = vec![]; for _ in 0..5 { let url = format!("gemini://localhost:{}/test.gmi", env.port); let handle = std::thread::spawn(move || { std::process::Command::new("python3") .arg("tests/gemini_test_client.py") .arg(url) .output() }); handles.push(handle); } // Collect results let mut results = vec![]; for handle in handles { let output = handle.join().unwrap().unwrap(); let status = String::from_utf8(output.stdout).unwrap(); results.push(status.trim().to_string()); } // Kill server let _ = server_process.kill(); // Analyze results let success_count = results.iter().filter(|r| r.starts_with("20")).count(); let rate_limited_count = results.iter().filter(|r| r.starts_with("41")).count(); // Validation assert!(success_count >= 1, "At least 1 request should succeed, got results: {:?}", results); assert!(rate_limited_count >= 1, "At least 1 request should be rate limited, got results: {:?}", results); assert_eq!(success_count + rate_limited_count, 5, "All requests should get valid responses, got results: {:?}", results); }