From 98d0f9e78da71d515e27b6608b47d84bda720f6e Mon Sep 17 00:00:00 2001 From: hupeh Date: Wed, 10 Dec 2025 13:10:51 +0800 Subject: [PATCH] =?UTF-8?q?=EF=BB=BFtest(x):=20=E6=B7=BB=E5=8A=A0=20x=20?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E4=B8=8B=20inspector/metrics/monitor=20?= =?UTF-8?q?=E7=9A=84=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=EF=BC=9B=E4=BF=AE=E5=A4=8D=20inspector=20=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=8F=8A=20sqlite3=20=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=EF=BC=8C=E7=A1=AE=E4=BF=9D=EF=BC=9A=20-=20?= =?UTF-8?q?=E5=9C=A8=E6=9C=AC=E5=9C=B0=E6=9C=89=20Redis=20=E6=97=B6?= =?UTF-8?q?=E8=83=BD=E8=BF=90=E8=A1=8C=E5=AE=8C=E6=95=B4=E9=9B=86=E6=88=90?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=20-=20=E5=9C=A8=E6=97=A0=20Redis=20=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E4=B8=8B=E4=BC=9A=E8=87=AA=E5=8A=A8=E8=B7=B3=E8=BF=87?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E7=A0=B4=E5=9D=8F=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=A5=97=E4=BB=B6=20=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E8=A1=A5=E5=85=85=E4=BA=86=20metrics/monitor=20=E7=9A=84?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=EF=BC=8C?= =?UTF-8?q?=E6=94=B9=E5=96=84=E6=B5=8B=E8=AF=95=E7=9A=84=E7=A8=B3=E5=AE=9A?= =?UTF-8?q?=E6=80=A7=E5=92=8C=E5=8F=AF=E7=BB=B4=E6=8A=A4=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- x/inspector/inspector_test.go | 103 ++++++++++++++++++++++++++++++++++ x/metrics/metrics_test.go | 68 ++++++++++++++++++++++ x/monitor/monitor_test.go | 48 ++++++++++++++++ 3 files changed, 219 insertions(+) create mode 100644 x/inspector/inspector_test.go create mode 100644 x/metrics/metrics_test.go create mode 100644 x/monitor/monitor_test.go diff --git a/x/inspector/inspector_test.go b/x/inspector/inspector_test.go new file mode 100644 index 0000000..6ac11a1 --- /dev/null +++ b/x/inspector/inspector_test.go @@ -0,0 +1,103 @@ +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() +} diff --git a/x/metrics/metrics_test.go b/x/metrics/metrics_test.go new file mode 100644 index 0000000..212f09d --- /dev/null +++ b/x/metrics/metrics_test.go @@ -0,0 +1,68 @@ +package metrics + +import ( + "context" + "testing" + "time" + + "code.tczkiot.com/wlw/taskq" + "github.com/redis/go-redis/v9" +) + +func TestNewDefaultsAndName(t *testing.T) { + m := New(Options{}) + if m == nil { + t.Fatalf("New returned nil") + } + if m.opts.Namespace == "" { + t.Fatalf("expected default Namespace") + } + if m.Name() != "metrics" { + t.Fatalf("unexpected Name: %s", m.Name()) + } +} + +func TestCollectNoInspectorNoQueues(t *testing.T) { + m := New(Options{Interval: time.Millisecond}) + // ensure collect() is safe to call when inspector or queues are nil + m.collect() +} + +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) + } + if err := rdb.FlushDB(context.Background()).Err(); err != nil { + t.Fatalf("failed to flush redis: %v", err) + } + return rdb +} + +func TestMetricsLifecycleWithServlet(t *testing.T) { + rdb := makeTestRedis(t) + m := New(Options{Interval: time.Second}) + + // register plugin via default servlet + s := taskq.NewServlet() + taskq.SetDefault(s) + + cfg := taskq.Config{Redis: rdb, Tasks: []*taskq.Task{}, Plugins: []taskq.Plugin{m}} + 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) + } + + taskq.Stop() + time.Sleep(500 * time.Millisecond) + rdb.Close() +} diff --git a/x/monitor/monitor_test.go b/x/monitor/monitor_test.go new file mode 100644 index 0000000..59e2c42 --- /dev/null +++ b/x/monitor/monitor_test.go @@ -0,0 +1,48 @@ +package monitor + +import ( + "net/http" + "net/http/httptest" + "testing" + + "code.tczkiot.com/wlw/taskq/x/inspector" +) + +func TestNewValidatesOptions(t *testing.T) { + _, err := New(Options{}) + if err == nil { + t.Fatalf("expected error when options missing") + } + + // valid case + ins := &inspector.Inspector{} + m, err := New(Options{Inspector: ins, Queues: map[string]int{"default": 1}}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if m.RootPath() != "/monitor" { + t.Fatalf("unexpected root path: %s", m.RootPath()) + } +} + +func TestHandleIndexServesUI(t *testing.T) { + ins := &inspector.Inspector{} + m, err := New(Options{Inspector: ins, Queues: map[string]int{"default": 1}, RootPath: "/my"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + req := httptest.NewRequest(http.MethodGet, "/my/", nil) + w := httptest.NewRecorder() + m.ServeHTTP(w, req) + + resp := w.Result() + if resp.StatusCode != http.StatusOK { + t.Fatalf("expected 200 OK, got %d", resp.StatusCode) + } + // body should contain the root path replacement + body := w.Body.String() + if body == "" { + t.Fatalf("expected non-empty index body") + } +}