test(x): 添加 x 目录下 inspector/metrics/monitor 的生命周期测试;修复 inspector 测试导入及 sqlite3 驱动注册,确保:
- 在本地有 Redis 时能运行完整集成测试 - 在无 Redis 环境下会自动跳过,避免破坏单元测试套件 同时补充了 metrics/monitor 的基本单元测试,改善测试的稳定性和可维护性
This commit is contained in:
48
x/monitor/monitor_test.go
Normal file
48
x/monitor/monitor_test.go
Normal file
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user