-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest-spawn-api-server.sh
More file actions
52 lines (46 loc) · 1.54 KB
/
test-spawn-api-server.sh
File metadata and controls
52 lines (46 loc) · 1.54 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
#!/usr/bin/env bash
# Spawns the objectiveai-api server on a random free port for testing.
# MOCK_DELAY_MS=0 so tests run fast.
#
# Prints "PORT PID" to stdout once the server is ready, then exits.
# The server continues running as a background process.
# Caller is responsible for killing it.
#
# Usage:
# read PORT PID < <(bash test-spawn-api-server)
# OBJECTIVEAI_TEST_PORT=$PORT pytest tests/
# kill $PID
set -euo pipefail
# Find a free port
get_free_port() {
python3 -c "import socket; s=socket.socket(); s.bind(('127.0.0.1',0)); print(s.getsockname()[1]); s.close()"
}
PORT=$(get_free_port)
# Build the server binary, then run from a copy so the original is not locked.
# Windows locks running executables, which blocks cargo test from relinking.
cargo build --package objectiveai-api --quiet >&2
REPO_ROOT="$(cd "$(dirname "$0")" && pwd)"
BINARY="$REPO_ROOT/target/debug/objectiveai-api"
if [ -f "$BINARY.exe" ]; then BINARY="$BINARY.exe"; fi
TMPDIR="$(mktemp -d)"
TMPBIN="$TMPDIR/$(basename "$BINARY")"
cp "$BINARY" "$TMPBIN"
ADDRESS=127.0.0.1 PORT="$PORT" MOCK_DELAY_MS=0 "$TMPBIN" &
SERVER_PID=$!
# Wait for the server to accept connections (up to 120s)
TIMEOUT=120
ELAPSED=0
while ! (echo > /dev/tcp/127.0.0.1/"$PORT") 2>/dev/null; do
if ! kill -0 "$SERVER_PID" 2>/dev/null; then
echo "Server process died" >&2
exit 1
fi
if [ "$ELAPSED" -ge "$TIMEOUT" ]; then
echo "Server did not become ready within ${TIMEOUT}s" >&2
kill "$SERVER_PID" 2>/dev/null
exit 1
fi
sleep 0.1
ELAPSED=$((ELAPSED + 1))
done
echo "$PORT $SERVER_PID"