-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_tests_isolated.sh
More file actions
executable file
·222 lines (185 loc) · 6.36 KB
/
run_tests_isolated.sh
File metadata and controls
executable file
·222 lines (185 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash
# Script to run tests individually in separate processes to avoid resource exhaustion
# Usage: ./run_tests_isolated.sh [filter_pattern]
# Example: ./run_tests_isolated.sh hnsw
# Example: ./run_tests_isolated.sh apps::hnsw::recovery
# Enable pipefail so pipeline returns exit code of first failed command
set -o pipefail
# Handle Ctrl+C and other signals
CURRENT_TEST_PID=""
CURRENT_TEST_NAME=""
cleanup() {
echo ""
if [ -n "$CURRENT_TEST_NAME" ]; then
echo "In-progress test: $CURRENT_TEST_NAME"
fi
echo "=========================================="
echo "Interrupted! Cleaning up..."
echo "=========================================="
# Kill the current test if running
if [ -n "$CURRENT_TEST_PID" ]; then
echo "Stopping current test (PID: $CURRENT_TEST_PID)..."
kill -TERM "$CURRENT_TEST_PID" 2>/dev/null || true
wait "$CURRENT_TEST_PID" 2>/dev/null || true
fi
# Kill any other background processes
jobs -p | xargs -r kill -TERM 2>/dev/null || true
# Clean up temp file
rm -f /tmp/test_output.log
echo "Cleanup complete"
exit 130
}
trap cleanup SIGINT SIGTERM
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Parse arguments
FILTER="${1:-}"
TEST_THREADS="${TEST_THREADS:-1}"
TIMEOUT="${TIMEOUT:-180}" # Default 3 minute timeout per test
echo "=========================================="
echo "Running tests in isolated processes"
echo "Filter: ${FILTER:-<none>}"
echo "Test threads: ${TEST_THREADS}"
echo "Timeout per test: ${TIMEOUT}s"
echo "=========================================="
echo ""
# Build tests once and stop immediately on compilation failure.
BUILD_LOG=$(mktemp /tmp/morpheus-isolated-build.XXXXXX.log)
BUILD_STAMP=$(mktemp /tmp/morpheus-isolated-build.XXXXXX.stamp)
touch "$BUILD_STAMP"
echo "Building tests..."
if [ -z "$FILTER" ]; then
cargo test --lib --no-run >"$BUILD_LOG" 2>&1
BUILD_EXIT_CODE=$?
else
cargo test --lib "$FILTER" --no-run >"$BUILD_LOG" 2>&1
BUILD_EXIT_CODE=$?
fi
if [ $BUILD_EXIT_CODE -ne 0 ]; then
echo -e "${RED}Compilation failed. Aborting without running stale test binaries.${NC}"
echo ""
cat "$BUILD_LOG"
rm -f "$BUILD_LOG" "$BUILD_STAMP"
exit $BUILD_EXIT_CODE
fi
grep -E "Compiling|Finished" "$BUILD_LOG" || true
# Find the test binary
# Try to use jq if available, otherwise fall back to binaries built after BUILD_STAMP
if command -v jq >/dev/null 2>&1; then
TEST_BINARY=$(cargo test --lib --no-run --message-format=json 2>/dev/null | \
jq -r 'select(.profile.test == true) | select(.target.kind | contains(["lib"])) | .executable' | \
grep -v "^null$" | head -1)
fi
if [ -z "$TEST_BINARY" ] || [ ! -f "$TEST_BINARY" ]; then
# Fallback: find the most recently modified binary produced by this build.
TEST_BINARY=$(find target/debug/deps -name 'morpheus-*' -type f -executable -not -name '*.d' -newer "$BUILD_STAMP" | \
xargs ls -t 2>/dev/null | head -1)
if [ -z "$TEST_BINARY" ] || [ ! -f "$TEST_BINARY" ]; then
echo "Error: Could not find a freshly built test binary in target/debug/deps"
echo "Compilation succeeded, but no current test executable was discovered"
rm -f "$BUILD_LOG" "$BUILD_STAMP"
exit 1
fi
fi
rm -f "$BUILD_LOG" "$BUILD_STAMP"
echo "Using test binary: $TEST_BINARY"
echo ""
# Get list of tests
echo "Discovering tests..."
if [ -z "$FILTER" ]; then
TEST_LIST=$("$TEST_BINARY" --list 2>/dev/null | grep ': test$' | sed 's/: test$//')
else
TEST_LIST=$("$TEST_BINARY" --list 2>/dev/null | grep "$FILTER" | grep ': test$' | sed 's/: test$//')
fi
# Count total tests
TOTAL_TESTS=$(echo "$TEST_LIST" | wc -l)
echo "Found $TOTAL_TESTS tests to run"
echo ""
# Track results
PASSED=0
FAILED=0
IGNORED=0
TIMED_OUT=0
FAILED_TESTS=()
TIMED_OUT_TESTS=()
# Run each test individually
CURRENT=0
while IFS= read -r test_name; do
CURRENT=$((CURRENT + 1))
# Skip empty lines
[ -z "$test_name" ] && continue
echo "[$CURRENT/$TOTAL_TESTS] Running: $test_name"
# Run test with timeout (directly using test binary)
# Run in background so we can track PID and handle signals
timeout "$TIMEOUT" "$TEST_BINARY" "$test_name" --test-threads="$TEST_THREADS" --nocapture 2>&1 | tee /tmp/test_output.log &
CURRENT_TEST_NAME="$test_name"
CURRENT_TEST_PID=$!
# Wait for test to complete and capture exit code
wait $CURRENT_TEST_PID
EXIT_CODE=$?
CURRENT_TEST_PID=""
CURRENT_TEST_NAME=""
if [ $EXIT_CODE -eq 0 ]; then
# Check if test was actually ignored (look for "test result: ok. 0 passed" with ignored > 0)
if grep -q "test result: ok. 0 passed; 0 failed; 1 ignored" /tmp/test_output.log || \
grep -q "... ignored$" /tmp/test_output.log; then
echo -e "${YELLOW}IGNORED${NC}"
IGNORED=$((IGNORED + 1))
else
echo -e "${GREEN}PASSED${NC}"
PASSED=$((PASSED + 1))
fi
elif [ $EXIT_CODE -eq 124 ]; then
# Timeout exit code
echo -e "${RED}TIMED OUT after ${TIMEOUT}s${NC}"
TIMED_OUT=$((TIMED_OUT + 1))
TIMED_OUT_TESTS+=("$test_name")
elif [ $EXIT_CODE -eq 130 ]; then
# User interrupted (Ctrl+C)
echo -e "${RED}INTERRUPTED${NC}"
cleanup
else
# Test failed
echo -e "${RED}FAILED (exit code: $EXIT_CODE)${NC}"
FAILED=$((FAILED + 1))
FAILED_TESTS+=("$test_name")
fi
echo ""
# Small delay between tests to allow resource cleanup
sleep 0.5
done <<< "$TEST_LIST"
# Print summary
echo ""
echo "=========================================="
echo "Test Summary"
echo "=========================================="
echo "Total: $TOTAL_TESTS"
echo -e "${GREEN}Passed: $PASSED${NC}"
echo -e "${YELLOW}Ignored: $IGNORED${NC}"
echo -e "${RED}Failed: $FAILED${NC}"
echo -e "${RED}Timed out: $TIMED_OUT${NC}"
if [ $FAILED -gt 0 ]; then
echo ""
echo "Failed tests:"
for test in "${FAILED_TESTS[@]}"; do
echo " - $test"
done
fi
if [ $TIMED_OUT -gt 0 ]; then
echo ""
echo "Timed out tests:"
for test in "${TIMED_OUT_TESTS[@]}"; do
echo " - $test"
done
fi
echo ""
# Clean up temp file
rm -f /tmp/test_output.log
# Exit with error if any tests failed
if [ $FAILED -gt 0 ] || [ $TIMED_OUT -gt 0 ]; then
exit 1
fi
exit 0