Files
taskq/x/inspector/inspector_test.go
hupeh 98d0f9e78d test(x): 添加 x 目录下 inspector/metrics/monitor 的生命周期测试;修复 inspector 测试导入及 sqlite3 驱动注册,确保:
- 在本地有 Redis 时能运行完整集成测试
- 在无 Redis 环境下会自动跳过,避免破坏单元测试套件
同时补充了 metrics/monitor 的基本单元测试,改善测试的稳定性和可维护性
2025-12-10 13:18:21 +08:00

104 lines
2.7 KiB
Go

package inspector
import (
"context"
"testing"
"time"
"code.tczkiot.com/wlw/taskq"
"github.com/hibiken/asynq"
_ "github.com/mattn/go-sqlite3"
"github.com/redis/go-redis/v9"
)
func TestNewDefaultsAndName(t *testing.T) {
ins := New(Options{})
if ins == nil {
t.Fatalf("New returned nil")
}
if ins.opts.Interval <= 0 {
t.Fatalf("expected default Interval > 0")
}
if ins.opts.DBPath == "" {
t.Fatalf("expected default DBPath set")
}
if ins.Name() != "inspector" {
t.Fatalf("unexpected Name: %s", ins.Name())
}
}
func TestConvertTaskHelpers(t *testing.T) {
// ensure convertTaskInfo/convertTaskList are callable
_ = convertTaskInfo(&asynq.TaskInfo{})
_ = convertTaskList([]*asynq.TaskInfo{})
}
func TestGetQueueInfoWhenNotStarted(t *testing.T) {
ins := New(Options{})
if _, err := ins.GetQueueInfo("default"); err == nil {
t.Fatalf("expected error when inspector not started")
}
}
func TestListActiveTasksWhenNotStarted(t *testing.T) {
ins := New(Options{})
if _, err := ins.ListActiveTasks("default", 10, 0); err == nil {
t.Fatalf("expected error when inspector not started")
}
}
func TestSaveMetrics_NoDB(t *testing.T) {
ins := New(Options{})
// db is nil by default; saveMetrics should return nil (no-op)
s := Stats{Queue: "q", Timestamp: time.Now().Unix()}
if err := ins.saveMetrics(s); err != nil {
t.Fatalf("saveMetrics returned error with nil db: %v", err)
}
}
// helper: create redis client for tests
func makeTestRedis(t *testing.T) redis.UniversalClient {
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 15})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := rdb.Ping(ctx).Err(); err != nil {
t.Skipf("redis not available: %v", err)
}
// flush test DB
if err := rdb.FlushDB(context.Background()).Err(); err != nil {
t.Fatalf("failed to flush redis: %v", err)
}
return rdb
}
func TestPluginLifecycleWithServlet(t *testing.T) {
rdb := makeTestRedis(t)
// create plugin
ins := New(Options{DBPath: ":memory:", Interval: time.Second})
// wire into a fresh servlet as default to use package-level helpers
s := taskq.NewServlet()
taskq.SetDefault(s)
cfg := taskq.Config{Redis: rdb, Tasks: []*taskq.Task{}, Plugins: []taskq.Plugin{ins}}
if err := taskq.Configure(cfg); err != nil {
t.Fatalf("Configure failed: %v", err)
}
if err := taskq.Init(context.Background()); err != nil {
t.Fatalf("Init failed: %v", err)
}
if err := taskq.Start(context.Background()); err != nil {
t.Fatalf("Start failed: %v", err)
}
// request stop
taskq.Stop()
// wait for plugins and internal goroutines to shutdown
time.Sleep(500 * time.Millisecond)
// close redis
rdb.Close()
}