Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions .github/workflows/go-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,8 @@ jobs:
with:
go-version: "1.24.2"

- name: Start Docker Compose
run: |
docker compose -f ./.script/docker-compose.yaml -p myapp up -d

- name: Test for MySQL
run: |
# 等待MySQL服务就绪(最多尝试30次,每次等待2秒)
timeout=60
elapsed=0
until docker exec myapp-mysql8-1 mysqladmin -uroot -proot -h127.0.0.1 ping; do
sleep 2
elapsed=$((elapsed + 2))
if [ $elapsed -ge $timeout ]; then
echo "MySQL未在${timeout}秒内启动!"
exit 1
fi
done

# MySQL就绪后执行命令
docker exec myapp-mysql8-1 mysql -uroot -proot -h127.0.0.1 -e "SHOW DATABASES;"

- name: Build
run: go build -v ./...

- name: Test
run: go test -race -coverprofile=cover.out -v ./...

- name: Stop Docker Compose
run: docker compose -f ./.script/docker-compose.yaml down
run: make e2e

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v5
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go-fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

- name: Check
run: |
make check
make fmt
if [ -n "$(git status --porcelain)" ]; then
echo >&2 "错误: 请在本地运行命令'make check'后再提交."
exit 1
Expand Down
2 changes: 1 addition & 1 deletion .script/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ services:
# 注意这里我映射为了 13306 端口
- "13306:3306"
redis:
image: 'bitnami/redis:latest'
image: 'bitnami/redis:8.0.3'
environment:
- ALLOW_EMPTY_PASSWORD=yes
ports:
Expand Down
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ endif
.PHONY: e2e
e2e:
@docker compose -f ./.script/docker-compose.yaml up -d
@echo "等待 10 秒确保容器启动完成..."
@$(SLEEP_CMD) # 根据系统动态选择命令
@go test -race -v -failfast ./...
@go test -race -v -failfast -coverprofile=cover.out ./...
@docker compose -f ./.script/docker-compose.yaml down


Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# ai-gateway-go
AI 网关,一个和业务整合在一起的 AI 网关,并不是一个纯粹一个AI 网关。

## License 说明
- 并不需要在每个文件里面都添加 license 说明,根目录保留一个就可以。
## 撰写测试
- 每个测试都必须是可以独立运行的。
- 使用 TestSuite 的形态来组织。
Expand Down
124 changes: 0 additions & 124 deletions internal/admin/biz_config_test.go

This file was deleted.

19 changes: 17 additions & 2 deletions internal/test/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,23 @@

package test

import "github.com/gotomicro/ego"
import (
"bytes"
_ "embed"

"github.com/gotomicro/ego/core/econf"
"gopkg.in/yaml.v3"
)

var (

Check failure on line 25 in internal/test/init.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gofumpt)
//go:embed config.yaml
cfg string
)

func init() {
ego.New(ego.WithArguments([]string{"--config=config.yaml"}))
// 用这种形式来规避运行部分测试加载配置失败的问题
err := econf.LoadFromReader(bytes.NewReader([]byte(cfg)), yaml.Unmarshal)
if err != nil {
panic(err)
}
}
33 changes: 33 additions & 0 deletions internal/test/ioc/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
package ioc

import (
"context"
"database/sql"
"log"
"os"
"time"

"github.com/ecodeclub/ai-gateway-go/internal/repository/dao"
"github.com/ecodeclub/ekit/retry"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
Expand All @@ -30,6 +34,7 @@ func InitDB() *gorm.DB {
dsn = "root:root@tcp(localhost:13306)/ai_gateway_platform"
}
log.Print("测试 MySQL:" + dsn)
WaitForDBSetup(dsn)
db, err := gorm.Open(mysql.Open(dsn))
if err != nil {
panic(err)
Expand All @@ -40,3 +45,31 @@ func InitDB() *gorm.DB {
}
return db
}

func WaitForDBSetup(dsn string) {
sqlDB, err := sql.Open("mysql", dsn)
if err != nil {
panic(err)
}
const maxInterval = 10 * time.Second
const maxRetries = 10
strategy, err := retry.NewExponentialBackoffRetryStrategy(time.Second, maxInterval, maxRetries)
if err != nil {
panic(err)
}

const timeout = 5 * time.Second
for {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
err = sqlDB.PingContext(ctx)
cancel()
if err == nil {
break
}
next, ok := strategy.Next()
if !ok {
panic("WaitForDBSetup 重试失败......")
}
time.Sleep(next)
}
}
56 changes: 56 additions & 0 deletions internal/test/ioc/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading