@@ -38,44 +38,73 @@ jobs:
3838 ports :
3939 - 8888:80
4040 options : >-
41- --health-cmd "curl -f http://localhost/get || exit 1 "
41+ --health-cmd "python -c 'import requests; requests.get(\" http://localhost/get\")' "
4242 --health-interval 10s
43- --health-timeout 5s
44- --health-retries 5
45- --health-start-period 5s
46-
47- # JSON API 服务
48- json-api :
49- image : clue/json-server:latest
50- ports :
51- - 8889:80
52- volumes :
53- - ${{ github.workspace }}/tests/mock-data:/data:ro
54- options : >-
55- --health-cmd "wget --spider -q http://localhost/posts || exit 1"
56- --health-interval 10s
57- --health-timeout 5s
58- --health-retries 5
59- --health-start-period 5s
60-
61- # WebSocket Echo 服务
62- ws-echo :
63- image : jmalloc/echo-server:latest
64- ports :
65- - 8890:8080
66- env :
67- PORT : 8080
68- options : >-
69- --health-cmd "wget --spider -q http://localhost:8080 || exit 1"
70- --health-interval 10s
71- --health-timeout 5s
72- --health-retries 5
73- --health-start-period 5s
43+ --health-timeout 10s
44+ --health-retries 10
45+ --health-start-period 20s
7446
7547 steps :
7648 - name : Checkout code
7749 uses : actions/checkout@v5
7850
51+ - name : Start json-server
52+ run : |
53+ # 使用 Node.js 官方镜像运行 json-server
54+ docker run -d \
55+ --name json-api \
56+ -p 8889:3000 \
57+ -v ${{ github.workspace }}/tests/mock-data:/data:ro \
58+ -w /data \
59+ node:18-alpine \
60+ sh -c "npm install -g [email protected] && json-server --host 0.0.0.0 --port 3000 db.json" 61+
62+ # 等待服务就绪
63+ echo "Waiting for json-api to be ready..."
64+ for i in {1..60}; do
65+ if curl -f http://localhost:8889/posts > /dev/null 2>&1; then
66+ echo "✅ json-api is ready!"
67+ break
68+ fi
69+ if [ $i -eq 60 ]; then
70+ echo "❌ json-api failed to start"
71+ docker logs json-api
72+ exit 1
73+ fi
74+ sleep 2
75+ done
76+
77+ - name : Start WebSocket echo server
78+ run : |
79+ # 使用简单的 Python WebSocket echo server
80+ cat > ws-echo.py << 'EOF'
81+ import asyncio
82+ import websockets
83+
84+ async def echo(websocket, path):
85+ async for message in websocket:
86+ await websocket.send(message)
87+
88+ async def main():
89+ async with websockets.serve(echo, "0.0.0.0", 8890):
90+ await asyncio.Future()
91+
92+ if __name__ == "__main__":
93+ asyncio.run(main())
94+ EOF
95+
96+ docker run -d \
97+ --name ws-echo \
98+ -p 8890:8890 \
99+ -v ${{ github.workspace }}/ws-echo.py:/ws-echo.py:ro \
100+ python:3.11-alpine \
101+ sh -c "pip install websockets && python /ws-echo.py"
102+
103+ # 等待服务就绪
104+ echo "Waiting for ws-echo to be ready..."
105+ sleep 5
106+ echo "✅ ws-echo started!"
107+
79108 - name : Install Rust-stable
80109 uses : actions-rust-lang/setup-rust-toolchain@v1
81110 with :
@@ -121,3 +150,11 @@ jobs:
121150 path : |
122151 *.log
123152 sessions.db
153+
154+ - name : Cleanup test services
155+ if : always()
156+ run : |
157+ docker stop json-api || true
158+ docker rm json-api || true
159+ docker stop ws-echo || true
160+ docker rm ws-echo || true
0 commit comments