Skip to content

Commit d42b53e

Browse files
authored
Merge pull request #27 from flycash/dev
修复测试问题
2 parents b29d58c + c561469 commit d42b53e

File tree

13 files changed

+112
-477
lines changed

13 files changed

+112
-477
lines changed

.github/workflows/go-coverage.yml

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,8 @@ jobs:
4141
with:
4242
go-version: "1.24.2"
4343

44-
- name: Start Docker Compose
45-
run: |
46-
docker compose -f ./.script/docker-compose.yaml -p myapp up -d
47-
48-
- name: Test for MySQL
49-
run: |
50-
# 等待MySQL服务就绪(最多尝试30次,每次等待2秒)
51-
timeout=60
52-
elapsed=0
53-
until docker exec myapp-mysql8-1 mysqladmin -uroot -proot -h127.0.0.1 ping; do
54-
sleep 2
55-
elapsed=$((elapsed + 2))
56-
if [ $elapsed -ge $timeout ]; then
57-
echo "MySQL未在${timeout}秒内启动!"
58-
exit 1
59-
fi
60-
done
61-
62-
# MySQL就绪后执行命令
63-
docker exec myapp-mysql8-1 mysql -uroot -proot -h127.0.0.1 -e "SHOW DATABASES;"
64-
65-
- name: Build
66-
run: go build -v ./...
67-
6844
- name: Test
69-
run: go test -race -coverprofile=cover.out -v ./...
70-
71-
- name: Stop Docker Compose
72-
run: docker compose -f ./.script/docker-compose.yaml down
45+
run: make e2e
7346

7447
- name: Upload coverage reports to Codecov
7548
uses: codecov/codecov-action@v5

.github/workflows/go-fmt.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939

4040
- name: Check
4141
run: |
42-
make check
42+
make fmt
4343
if [ -n "$(git status --porcelain)" ]; then
4444
echo >&2 "错误: 请在本地运行命令'make check'后再提交."
4545
exit 1

.script/docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ services:
2727
# 注意这里我映射为了 13306 端口
2828
- "13306:3306"
2929
redis:
30-
image: 'bitnami/redis:latest'
30+
image: 'bitnami/redis:8.0.3'
3131
environment:
3232
- ALLOW_EMPTY_PASSWORD=yes
3333
ports:

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ endif
1616
.PHONY: e2e
1717
e2e:
1818
@docker compose -f ./.script/docker-compose.yaml up -d
19-
@echo "等待 10 秒确保容器启动完成..."
20-
@$(SLEEP_CMD) # 根据系统动态选择命令
21-
@go test -race -v -failfast ./...
19+
@go test -race -v -failfast -coverprofile=cover.out ./...
2220
@docker compose -f ./.script/docker-compose.yaml down
2321

2422

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# ai-gateway-go
22
AI 网关,一个和业务整合在一起的 AI 网关,并不是一个纯粹一个AI 网关。
33

4+
## License 说明
5+
- 并不需要在每个文件里面都添加 license 说明,根目录保留一个就可以。
46
## 撰写测试
57
- 每个测试都必须是可以独立运行的。
68
- 使用 TestSuite 的形态来组织。

internal/admin/biz_config_test.go

Lines changed: 0 additions & 124 deletions
This file was deleted.

internal/test/init.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,23 @@
1414

1515
package test
1616

17-
import "github.com/gotomicro/ego"
17+
import (
18+
"bytes"
19+
_ "embed"
20+
21+
"github.com/gotomicro/ego/core/econf"
22+
"gopkg.in/yaml.v3"
23+
)
24+
25+
var (
26+
//go:embed config.yaml
27+
cfg string
28+
)
1829

1930
func init() {
20-
ego.New(ego.WithArguments([]string{"--config=config.yaml"}))
31+
// 用这种形式来规避运行部分测试加载配置失败的问题
32+
err := econf.LoadFromReader(bytes.NewReader([]byte(cfg)), yaml.Unmarshal)
33+
if err != nil {
34+
panic(err)
35+
}
2136
}

internal/test/ioc/db.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
package ioc
1616

1717
import (
18+
"context"
19+
"database/sql"
1820
"log"
1921
"os"
22+
"time"
2023

2124
"github.com/ecodeclub/ai-gateway-go/internal/repository/dao"
25+
"github.com/ecodeclub/ekit/retry"
2226
"gorm.io/driver/mysql"
2327
"gorm.io/gorm"
2428
)
@@ -30,6 +34,7 @@ func InitDB() *gorm.DB {
3034
dsn = "root:root@tcp(localhost:13306)/ai_gateway_platform"
3135
}
3236
log.Print("测试 MySQL:" + dsn)
37+
WaitForDBSetup(dsn)
3338
db, err := gorm.Open(mysql.Open(dsn))
3439
if err != nil {
3540
panic(err)
@@ -40,3 +45,31 @@ func InitDB() *gorm.DB {
4045
}
4146
return db
4247
}
48+
49+
func WaitForDBSetup(dsn string) {
50+
sqlDB, err := sql.Open("mysql", dsn)
51+
if err != nil {
52+
panic(err)
53+
}
54+
const maxInterval = 10 * time.Second
55+
const maxRetries = 10
56+
strategy, err := retry.NewExponentialBackoffRetryStrategy(time.Second, maxInterval, maxRetries)
57+
if err != nil {
58+
panic(err)
59+
}
60+
61+
const timeout = 5 * time.Second
62+
for {
63+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
64+
err = sqlDB.PingContext(ctx)
65+
cancel()
66+
if err == nil {
67+
break
68+
}
69+
next, ok := strategy.Next()
70+
if !ok {
71+
panic("WaitForDBSetup 重试失败......")
72+
}
73+
time.Sleep(next)
74+
}
75+
}

internal/test/ioc/wire_gen.go

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)