julia-env-test / test_julia_env.sh
wukaixingxp's picture
Upload folder using huggingface_hub
f58914c verified
#!/bin/bash
# Julia Environment Test Script
# Tests infinite loop timeout handling and worker recovery
#
# Usage: ./test_julia_env.sh [HOST] [PORT]
# Default: localhost:8000
set -e
HOST="${1:-localhost}"
PORT="${2:-8000}"
BASE_URL="http://${HOST}:${PORT}"
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "=============================================="
echo "Julia Environment Test Suite"
echo "Testing: ${BASE_URL}"
echo "=============================================="
echo ""
# Function to print test results
pass() {
echo -e "${GREEN}✓ PASS${NC}: $1"
}
fail() {
echo -e "${RED}✗ FAIL${NC}: $1"
FAILED=1
}
warn() {
echo -e "${YELLOW}! WARN${NC}: $1"
}
FAILED=0
# Test 1: Health Check
echo "--- Test 1: Health Check ---"
HEALTH=$(curl -s --max-time 10 "${BASE_URL}/health" 2>/dev/null || echo "TIMEOUT")
if [[ "$HEALTH" == *"healthy"* ]]; then
pass "Server is healthy"
echo " Response: $HEALTH"
else
fail "Health check failed: $HEALTH"
fi
echo ""
# Test 2: Pool Status
echo "--- Test 2: Pool Status ---"
POOL=$(curl -s --max-time 10 "${BASE_URL}/pool_status" 2>/dev/null || echo "TIMEOUT")
if [[ "$POOL" == *"pool_enabled"* ]]; then
pass "Pool status retrieved"
echo " Response: $POOL"
else
fail "Pool status failed: $POOL"
fi
echo ""
# Test 3: Basic Execution
echo "--- Test 3: Basic Julia Execution ---"
BASIC_RESULT=$(curl -s --max-time 30 -X POST "${BASE_URL}/step" \
-H "Content-Type: application/json" \
-d '{"action": {"core_code": "println(\"Hello from Julia!\")", "test_code": ""}}' 2>/dev/null || echo "TIMEOUT")
if [[ "$BASIC_RESULT" == *"Hello from Julia"* ]]; then
pass "Basic execution works"
echo " Response: $BASIC_RESULT"
else
fail "Basic execution failed: $BASIC_RESULT"
fi
echo ""
# Test 4: Tests Passing
echo "--- Test 4: Test Execution (Passing) ---"
TEST_RESULT=$(curl -s --max-time 30 -X POST "${BASE_URL}/step" \
-H "Content-Type: application/json" \
-d '{
"action": {
"core_code": "function fib(n)\n n <= 1 ? n : fib(n-1) + fib(n-2)\nend",
"test_code": "using Test\n@testset \"Fibonacci\" begin\n @test fib(0) == 0\n @test fib(1) == 1\n @test fib(5) == 5\n @test fib(10) == 55\nend"
}
}' 2>/dev/null || echo "TIMEOUT")
if [[ "$TEST_RESULT" == *"tests_passed"* ]] && [[ "$TEST_RESULT" == *"4"* ]]; then
pass "Test execution works (4 tests passed)"
echo " Response: $TEST_RESULT"
else
fail "Test execution failed: $TEST_RESULT"
fi
echo ""
# Test 5: Syntax Error Detection
echo "--- Test 5: Syntax Error Detection ---"
SYNTAX_RESULT=$(curl -s --max-time 30 -X POST "${BASE_URL}/step" \
-H "Content-Type: application/json" \
-d '{"action": {"core_code": "function broken(\n # missing end", "test_code": ""}}' 2>/dev/null || echo "TIMEOUT")
if [[ "$SYNTAX_RESULT" == *"code_compiles"*"false"* ]] || [[ "$SYNTAX_RESULT" == *"error"* ]] || [[ "$SYNTAX_RESULT" == *"Error"* ]]; then
pass "Syntax error detected"
echo " Response: $SYNTAX_RESULT"
else
warn "Syntax error may not be properly detected: $SYNTAX_RESULT"
fi
echo ""
# Test 6: Infinite Loop Timeout (CRITICAL TEST)
echo "--- Test 6: Infinite Loop Timeout (CRITICAL) ---"
echo " This test checks if infinite loops are properly terminated."
echo " Expected: Request should timeout and return an error within pool timeout."
echo " Starting..."
START_TIME=$(date +%s)
TIMEOUT_RESULT=$(curl -s --max-time 300 -X POST "${BASE_URL}/step" \
-H "Content-Type: application/json" \
-d '{"action": {"core_code": "while true\n sleep(1)\nend", "test_code": ""}}' 2>/dev/null || echo "CURL_TIMEOUT")
END_TIME=$(date +%s)
ELAPSED=$((END_TIME - START_TIME))
if [[ "$TIMEOUT_RESULT" == "CURL_TIMEOUT" ]]; then
fail "Request timed out after ${ELAPSED}s (curl timeout). Server may be stuck."
elif [[ "$TIMEOUT_RESULT" == *"timed out"* ]] || [[ "$TIMEOUT_RESULT" == *"Execution timed out"* ]]; then
pass "Infinite loop properly timed out after ${ELAPSED}s"
echo " Response: $TIMEOUT_RESULT"
elif [[ "$TIMEOUT_RESULT" == *"exit_code"*"-1"* ]]; then
pass "Infinite loop terminated with exit_code -1 after ${ELAPSED}s"
echo " Response: $TIMEOUT_RESULT"
else
warn "Unexpected response after ${ELAPSED}s: $TIMEOUT_RESULT"
fi
echo ""
# Test 7: Worker Recovery After Timeout
echo "--- Test 7: Worker Recovery After Timeout ---"
echo " Testing if the server can handle requests after a timeout..."
RECOVERY_RESULT=$(curl -s --max-time 30 -X POST "${BASE_URL}/step" \
-H "Content-Type: application/json" \
-d '{"action": {"core_code": "println(\"Recovery test successful!\")", "test_code": ""}}' 2>/dev/null || echo "TIMEOUT")
if [[ "$RECOVERY_RESULT" == *"Recovery test successful"* ]]; then
pass "Worker recovery successful - server still operational"
echo " Response: $RECOVERY_RESULT"
else
fail "Worker recovery failed: $RECOVERY_RESULT"
fi
echo ""
# Test 8: Pool Status After Timeout
echo "--- Test 8: Pool Status After Timeout ---"
POOL_AFTER=$(curl -s --max-time 10 "${BASE_URL}/pool_status" 2>/dev/null || echo "TIMEOUT")
if [[ "$POOL_AFTER" == *"pool_enabled"*"true"* ]]; then
pass "Pool is still enabled after timeout"
echo " Response: $POOL_AFTER"
else
fail "Pool status check failed: $POOL_AFTER"
fi
echo ""
# Test 9: Multiple Consecutive Timeouts
echo "--- Test 9: Multiple Consecutive Timeouts (3x) ---"
echo " Testing if multiple timeouts exhaust the worker pool..."
for i in 1 2 3; do
echo " Timeout request $i/3..."
curl -s --max-time 300 -X POST "${BASE_URL}/step" \
-H "Content-Type: application/json" \
-d '{"action": {"core_code": "while true; sleep(1); end", "test_code": ""}}' >/dev/null 2>&1
done
echo " Testing normal operation after 3 timeouts..."
AFTER_MULTI=$(curl -s --max-time 30 -X POST "${BASE_URL}/step" \
-H "Content-Type: application/json" \
-d '{"action": {"core_code": "println(\"Still working after 3 timeouts!\")", "test_code": ""}}' 2>/dev/null || echo "TIMEOUT")
if [[ "$AFTER_MULTI" == *"Still working after 3 timeouts"* ]]; then
pass "Server handles multiple consecutive timeouts correctly"
else
fail "Server failed after multiple timeouts: $AFTER_MULTI"
fi
echo ""
# Test 10: Concurrent Request Handling
echo "--- Test 10: Concurrent Request Handling ---"
echo " Sending 4 requests in parallel..."
# Create temp files for results
TMPDIR=$(mktemp -d)
for i in 1 2 3 4; do
curl -s --max-time 60 -X POST "${BASE_URL}/step" \
-H "Content-Type: application/json" \
-d "{\"action\": {\"core_code\": \"println(\\\"Request $i\\\")\", \"test_code\": \"\"}}" > "${TMPDIR}/result_${i}.txt" 2>&1 &
done
wait
CONCURRENT_SUCCESS=0
for i in 1 2 3 4; do
if grep -q "Request $i" "${TMPDIR}/result_${i}.txt" 2>/dev/null; then
((CONCURRENT_SUCCESS++))
fi
done
rm -rf "$TMPDIR"
if [[ $CONCURRENT_SUCCESS -eq 4 ]]; then
pass "All 4 concurrent requests succeeded"
else
fail "Only $CONCURRENT_SUCCESS/4 concurrent requests succeeded"
fi
echo ""
# Final Summary
echo "=============================================="
echo "Test Summary"
echo "=============================================="
if [[ $FAILED -eq 0 ]]; then
echo -e "${GREEN}All tests passed!${NC}"
exit 0
else
echo -e "${RED}Some tests failed. Please review the output above.${NC}"
exit 1
fi