Files

41 lines
963 B
Go

package observability
import (
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
)
func TestFormatPrometheusCounter(t *testing.T) {
out := FormatPrometheusCounter("http_requests_total", "Total HTTP requests", 42)
for _, want := range []string{
"# HELP http_requests_total Total HTTP requests",
"# TYPE http_requests_total counter",
"http_requests_total 42",
} {
if !strings.Contains(out, want) {
t.Errorf("missing %q in:\n%s", want, out)
}
}
}
func TestReadinessHandler(t *testing.T) {
var ready atomic.Bool
h := ReadinessHandler(&ready)
rec1 := httptest.NewRecorder()
h(rec1, httptest.NewRequest(http.MethodGet, "/readyz", nil))
if rec1.Code != http.StatusServiceUnavailable {
t.Fatalf("got %d, want 503", rec1.Code)
}
ready.Store(true)
rec2 := httptest.NewRecorder()
h(rec2, httptest.NewRequest(http.MethodGet, "/readyz", nil))
if rec2.Code != http.StatusOK {
t.Fatalf("got %d, want 200", rec2.Code)
}
}