gomog/test_reload_simple.sh

149 lines
3.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
echo "=== 测试服务器重启后数据加载 ==="
# 清理旧的测试数据(可选)
rm -f gomog_test.db
# 创建临时配置(只启用 HTTP
cat > config_test.yaml <<EOF
server:
http_addr: ":8081"
tcp_addr: ""
mode: "dev"
database:
type: "sqlite"
dsn: "gomog_test.db"
max_open: 10
max_idle: 5
log:
level: "debug"
format: "text"
EOF
echo "1. 启动服务器..."
./bin/gomog -config config_test.yaml &
SERVER_PID=$!
sleep 3
# 检查服务器是否启动
if ! kill -0 $SERVER_PID 2>/dev/null; then
echo "❌ 服务器启动失败"
exit 1
fi
echo "✅ 服务器已启动 (PID: $SERVER_PID)"
# 插入测试数据
echo ""
echo "2. 插入测试数据..."
curl -s -X POST http://localhost:8081/api/v1/testdb/users/insert \
-H "Content-Type: application/json" \
-d '{
"documents": [
{"name": "Alice", "age": 30, "email": "alice@example.com"},
{"name": "Bob", "age": 25, "email": "bob@example.com"}
]
}' | jq .
echo ""
sleep 2
# 验证数据在内存中
echo ""
echo "3. 查询数据(第一次)..."
curl -s -X POST http://localhost:8081/api/v1/testdb/users/find \
-H "Content-Type: application/json" \
-d '{"filter": {}}' | jq .
# 查看数据库文件
echo ""
echo "4. 查看 SQLite 数据库中的数据..."
sqlite3 gomog_test.db "SELECT id, json_extract(data, '$.name') as name FROM users;" 2>/dev/null || echo "数据库文件不存在或无数据"
# 停止服务器
echo ""
echo "5. 停止服务器..."
kill $SERVER_PID
sleep 2
echo "✅ 服务器已停止"
# 重启服务器
echo ""
echo "6. 重启服务器..."
./bin/gomog -config config_test.yaml &
SERVER_PID=$!
sleep 3
if ! kill -0 $SERVER_PID 2>/dev/null; then
echo "❌ 服务器重启失败"
exit 1
fi
echo "✅ 服务器已重启 (PID: $SERVER_PID)"
# 验证数据是否被正确加载
echo ""
echo "7. 查询数据(重启后)..."
RESULT=$(curl -s -X POST http://localhost:8081/api/v1/testdb/users/find \
-H "Content-Type: application/json" \
-d '{"filter": {}}')
echo "$RESULT" | jq .
# 检查数据是否正确加载
COUNT=$(echo "$RESULT" | jq '.documents | length')
if [ "$COUNT" -eq 2 ]; then
echo ""
echo "✅ 成功!重启后加载了 $COUNT 条数据"
else
echo ""
echo "❌ 失败!只加载了 $COUNT 条数据(期望 2 条)"
fi
# 再次插入数据,验证增量
echo ""
echo "8. 再次插入数据..."
curl -s -X POST http://localhost:8081/api/v1/testdb/users/insert \
-H "Content-Type: application/json" \
-d '{
"documents": [
{"name": "Charlie", "age": 35, "email": "charlie@example.com"}
]
}' | jq .
echo ""
sleep 2
# 验证总数据量
echo ""
echo "9. 查询所有数据..."
RESULT=$(curl -s -X POST http://localhost:8081/api/v1/testdb/users/find \
-H "Content-Type: application/json" \
-d '{"filter": {}}')
echo "$RESULT" | jq .
TOTAL=$(echo "$RESULT" | jq '.documents | length')
echo ""
echo "✅ 数据库中共有 $TOTAL 条数据"
# 停止服务器
echo ""
echo "10. 停止服务器..."
kill $SERVER_PID
sleep 2
# 最终验证数据库
echo ""
echo "11. 最终数据库状态..."
sqlite3 gomog_test.db "SELECT COUNT(*) as total FROM users;" 2>/dev/null || echo "无法查询数据库"
# 清理
rm -f config_test.yaml
echo ""
echo "=== 测试完成 ==="