- 在本地有 Redis 时能运行完整集成测试 - 在无 Redis 环境下会自动跳过,避免破坏单元测试套件 同时补充了 metrics/monitor 的基本单元测试,改善测试的稳定性和可维护性
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
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")
|
|
}
|
|
}
|