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() }