philosophers_prep/testers/test_4_state_monitor.sh

156 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
# Tester for state_monitor exercise
# Tests state monitoring and thread synchronization
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
EXERCISE="state_monitor"
PASSED=0
FAILED=0
echo "========================================"
echo "Testing: $EXERCISE"
echo "========================================"
if [ ! -f "./$EXERCISE" ]; then
echo -e "${RED}✗ Executable ./$EXERCISE not found${NC}"
echo "Please compile: gcc -Wall -Wextra -Werror -pthread state_monitor.c -o state_monitor"
exit 1
fi
# Test 1: Program runs for reasonable time
echo -n "Test 1: Program execution... "
timeout 15 ./$EXERCISE > /tmp/state_monitor_output.txt 2>&1 &
PID=$!
sleep 8
kill $PID 2>/dev/null
wait $PID 2>/dev/null
if [ -f /tmp/state_monitor_output.txt ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
((FAILED++))
fi
# Test 2: Check for state transitions
echo -n "Test 2: State transitions present... "
OUTPUT=$(cat /tmp/state_monitor_output.txt 2>/dev/null)
if echo "$OUTPUT" | grep -qi "WORKING" && \
echo "$OUTPUT" | grep -qi "RESTING" && \
echo "$OUTPUT" | grep -qi "THINKING"; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected to see WORKING, RESTING, and THINKING states"
((FAILED++))
fi
# Test 3: Check for timestamps
echo -n "Test 3: Timestamps present... "
if echo "$OUTPUT" | grep -qE "\[[0-9]+\]"; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected timestamps in format [timestamp]"
((FAILED++))
fi
# Test 4: Check for worker identification
echo -n "Test 4: Worker identification... "
WORKER_COUNT=$(echo "$OUTPUT" | grep -oE "Worker [0-9]+" | sort -u | wc -l)
if [ "$WORKER_COUNT" -ge 3 ]; then
echo -e "${GREEN}✓ PASSED${NC} (found $WORKER_COUNT workers)"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected at least 3 workers, found: $WORKER_COUNT"
((FAILED++))
fi
# Test 5: Check state duration logic
echo -n "Test 5: State duration verification... "
# WORKING should last ~2s, RESTING ~1s, THINKING ~1.5s
# Extract timestamps and check intervals
WORKING_LINES=$(echo "$OUTPUT" | grep -i "WORKING" | head -10)
if [ -n "$WORKING_LINES" ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${YELLOW}⚠ WARNING${NC} - Could not verify state durations"
((PASSED++))
fi
# Test 6: Monitor thread functionality
echo -n "Test 6: Monitor warnings (if applicable)... "
if echo "$OUTPUT" | grep -qi "WARNING"; then
echo -e "${GREEN}✓ PASSED${NC} (monitor detected stuck worker)"
((PASSED++))
elif echo "$OUTPUT" | grep -qi "monitor"; then
echo -e "${GREEN}✓ PASSED${NC} (monitor running)"
((PASSED++))
else
echo -e "${YELLOW}⚠ PARTIAL${NC} - No clear evidence of monitor warnings"
echo "This is OK if no worker got stuck during the test"
((PASSED++))
fi
# Test 7: Memory leak check
if command -v valgrind &> /dev/null; then
echo -n "Test 7: Memory leak check... "
timeout 10 valgrind --leak-check=full --error-exitcode=42 ./$EXERCISE > /dev/null 2>&1
VALGRIND_EXIT=$?
# Since we use timeout, we need to check differently
if [ $VALGRIND_EXIT -eq 124 ] || [ $VALGRIND_EXIT -eq 0 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Memory leaks detected!"
((FAILED++))
fi
else
echo -e "${YELLOW}⊘ Test 7: Valgrind not installed, skipping${NC}"
fi
# Test 8: Thread sanitizer (data race check)
if command -v gcc &> /dev/null; then
echo -n "Test 8: Data race detection... "
gcc -Wall -Wextra -Werror -pthread -fsanitize=thread -g state_monitor.c -o state_monitor_tsan 2>/dev/null
if [ $? -eq 0 ]; then
timeout 10 ./state_monitor_tsan > /dev/null 2>&1
TSAN_OUTPUT=$(timeout 10 ./state_monitor_tsan 2>&1)
if echo "$TSAN_OUTPUT" | grep -q "WARNING: ThreadSanitizer: data race"; then
echo -e "${RED}✗ FAILED${NC}"
echo "Data race detected!"
((FAILED++))
else
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
fi
rm -f state_monitor_tsan
else
echo -e "${YELLOW}⊘ Could not compile with thread sanitizer${NC}"
fi
else
echo -e "${YELLOW}⊘ Test 8: gcc not available, skipping${NC}"
fi
# Cleanup
rm -f /tmp/state_monitor_output.txt
# Summary
echo "========================================"
echo -e "Results: ${GREEN}$PASSED passed${NC}, ${RED}$FAILED failed${NC}"
echo "========================================"
[ $FAILED -eq 0 ] && exit 0 || exit 1