(t *testing.T)
| 10 | ) |
| 11 | |
| 12 | func TestRegisterNodeHubMetrics_OK(t *testing.T) { |
| 13 | hub := nodehub.New(nodehub.Options{}) |
| 14 | mux := http.NewServeMux() |
| 15 | RegisterNodeHubMetrics(mux, hub) |
| 16 | |
| 17 | // 触发一次 enroll 失败计数(包级原子,影响测试间状态;这里只检查字段存在) |
| 18 | enrollFailureTotal.Add(1) |
| 19 | |
| 20 | srv := httptest.NewServer(mux) |
| 21 | defer srv.Close() |
| 22 | |
| 23 | resp, err := http.Get(srv.URL + "/v1/system/nodehub/metrics") |
| 24 | if err != nil { |
| 25 | t.Fatalf("get: %v", err) |
| 26 | } |
| 27 | defer resp.Body.Close() |
| 28 | if resp.StatusCode != http.StatusOK { |
| 29 | t.Fatalf("status=%d", resp.StatusCode) |
| 30 | } |
| 31 | var body map[string]any |
| 32 | if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { |
| 33 | t.Fatalf("decode: %v", err) |
| 34 | } |
| 35 | for _, k := range []string{ |
| 36 | "online_count", "online_node_ids", "calls_total", "calls_err_total", |
| 37 | "calls_offline_total", "push_usage_total", "push_usage_ack_total", |
| 38 | "reconnect_total", "call_latency_avg_ns", "last_seen", |
| 39 | "enroll_success_total", "enroll_failure_total", |
| 40 | } { |
| 41 | if _, ok := body[k]; !ok { |
| 42 | t.Errorf("missing key %q in response: %v", k, body) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestRegisterNodeHubMetrics_NilHub(t *testing.T) { |
| 48 | mux := http.NewServeMux() |
nothing calls this directly
no test coverage detected