#!/bin/bash # Tester for death_monitor exercise # Tests death detection system RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' EXERCISE="death_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 death_monitor.c -o death_monitor" exit 1 fi # Test 1: Program runs echo -n "Test 1: Program execution... " timeout 15 ./$EXERCISE > /tmp/death_monitor_output.txt 2>&1 & PID=$! sleep 8 kill $PID 2>/dev/null wait $PID 2>/dev/null if [ -f /tmp/death_monitor_output.txt ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" ((FAILED++)) fi OUTPUT=$(cat /tmp/death_monitor_output.txt 2>/dev/null) # Test 2: 4 worker threads echo -n "Test 2: 4 workers present... " WORKER_COUNT=$(echo "$OUTPUT" | grep -oiE "worker [0-9]+" | sort -u | wc -l) if [ "$WORKER_COUNT" -ge 4 ]; then echo -e "${GREEN}✓ PASSED${NC} (found $WORKER_COUNT workers)" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected 4 workers, found: $WORKER_COUNT" ((FAILED++)) fi # Test 3: Workers are active echo -n "Test 3: Worker activity... " ACTIVITY_COUNT=$(echo "$OUTPUT" | grep -ciE "(worker|work|activity|iteration)") if [ "$ACTIVITY_COUNT" -gt 10 ]; then echo -e "${GREEN}✓ PASSED${NC} ($ACTIVITY_COUNT activities)" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Not enough worker activity detected" ((FAILED++)) fi # Test 4: Monitor thread exists echo -n "Test 4: Monitor thread... " if echo "$OUTPUT" | grep -qiE "(monitor|checking|detect)"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${YELLOW}⚠ WARNING${NC}" echo "Could not clearly identify monitor thread activity" ((PASSED++)) fi # Test 5: Death detection echo -n "Test 5: Death detection... " if echo "$OUTPUT" | grep -qiE "(die|dead|death|warning)"; then echo -e "${GREEN}✓ PASSED${NC} (death/warning detected)" ((PASSED++)) else echo -e "${YELLOW}⚠ PARTIAL${NC}" echo "No death detected (might be OK if worker didn't die during test)" ((PASSED++)) fi # Test 6: Timestamps present echo -n "Test 6: Timestamps... " if echo "$OUTPUT" | grep -qE "\[[0-9]+\]"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected timestamps" ((FAILED++)) fi # Test 7: Last activity time tracking echo -n "Test 7: Activity time tracking... " if echo "$OUTPUT" | grep -qiE "(last.*activity|time|update)"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${YELLOW}⚠ WARNING${NC}" echo "Could not verify activity time tracking" ((PASSED++)) fi # Test 8: Thread sanitizer if command -v gcc &> /dev/null; then echo -n "Test 8: Data race detection... " gcc -Wall -Wextra -Werror -pthread -fsanitize=thread -g death_monitor.c -o death_monitor_tsan 2>/dev/null if [ $? -eq 0 ]; then timeout 10 ./death_monitor_tsan > /dev/null 2>&1 TSAN_OUTPUT=$(timeout 10 ./death_monitor_tsan 2>&1) if echo "$TSAN_OUTPUT" | grep -q "WARNING: ThreadSanitizer: data race"; then echo -e "${RED}✗ FAILED${NC}" echo "Data race detected!" echo "$TSAN_OUTPUT" | grep -A 5 "data race" | head -10 ((FAILED++)) else echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) fi rm -f death_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 # Test 9: Multiple runs (consistency) echo -n "Test 9: Consistency (3 runs)... " CONSISTENT=true for i in {1..3}; do timeout 10 ./$EXERCISE > /dev/null 2>&1 if [ $? -eq 124 ]; then CONSISTENT=false break fi done if [ "$CONSISTENT" = true ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Program hung on run $i" ((FAILED++)) fi # Test 10: Memory leak check if command -v valgrind &> /dev/null; then echo -n "Test 10: Memory leak check... " timeout 12 valgrind --leak-check=full --error-exitcode=42 ./$EXERCISE > /dev/null 2>&1 VALGRIND_EXIT=$? if [ $VALGRIND_EXIT -eq 0 ] || [ $VALGRIND_EXIT -eq 124 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Memory leaks detected!" ((FAILED++)) fi else echo -e "${YELLOW}⊘ Test 10: Valgrind not installed, skipping${NC}" fi # Cleanup rm -f /tmp/death_monitor_output.txt # Summary echo "========================================" echo -e "Results: ${GREEN}$PASSED passed${NC}, ${RED}$FAILED failed${NC}" echo "========================================" [ $FAILED -eq 0 ] && exit 0 || exit 1