| 1 | package daemon |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/FelineStateMachine/tinyrelay/internal/relay" |
| 12 | "github.com/FelineStateMachine/tinyrelay/internal/telemetry" |
| 13 | ) |
| 14 | |
| 15 | func TestInstrumentRelayConfigRecordsConnectionLifecycle(t *testing.T) { |
| 16 | metrics, err := telemetry.New(context.Background(), telemetry.Config{DebugToken: "test" }) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | defer metrics.Close(context.Background()) |
| 21 | cfg := InstrumentRelayConfig(relay.Config{}, metrics) |
| 22 | if cfg.OnConnection == nil || cfg.OnSubscription == nil { |
| 23 | t.Fatal("relay callbacks were not attached" ) |
| 24 | } |
| 25 | cfg.OnConnection(1) |
| 26 | cfg.OnSubscription(1) |
| 27 | cfg.OnSubscription(-1) |
| 28 | cfg.OnConnection(-1) |
| 29 | req := httptest.NewRequest(http.MethodGet, "/metrics" , nil) |
| 30 | req.Header.Set("Authorization" , "Bearer test" ) |
| 31 | rec := httptest.NewRecorder() |
| 32 | metrics.Handler().ServeHTTP(rec, req) |
| 33 | body, err := io.ReadAll(rec.Result().Body) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | text := string(body) |
| 38 | if !strings.Contains(text, "relay_connections 0" ) { |
| 39 | t.Fatalf("connection gauge missing: %s" , text) |
| 40 | } |
| 41 | if !strings.Contains(text, "relay_subscriptions 0" ) { |
| 42 | t.Fatalf("subscription gauge missing: %s" , text) |
| 43 | } |
| 44 | } |