Files
scrabble-game/backend/internal/telemetry/telemetry_test.go
T
Ilia Denisov eeaad62b10
Tests · Go / test (push) Successful in 11s
Tests · Integration / integration (push) Successful in 8s
Stage 1: backend foundation (Postgres, sessions, accounts, OTel)
- internal/postgres: pgx-over-database/sql pool (otelsql), embedded goose
  migrations into schema 'backend', committed go-jet code + cmd/jetgen tool.
- internal/account: durable accounts + unified telegram/email identities
  (UUIDv7 keys), find-or-create provisioning with unique-conflict handling.
- internal/session: opaque 256-bit tokens stored as a SHA-256 hash, revoke-only
  (no TTL); write-through cache gating /readyz; store + service.
- internal/telemetry: OTel tracer/meter providers (none/stdout) + request-timing
  middleware; internal/config gains Postgres + OTel env loading.
- internal/server: /api/v1 {public,user,internal,admin} skeleton + X-User-ID
  middleware; /readyz checks DB ping + cache; main wires
  telemetry -> db+migrate -> warm cache -> server.
- Tests: unit + integration (build tag 'integration', testcontainers
  postgres:17) for migrations, accounts, sessions, readyz; new integration.yaml.
- Docs: ARCHITECTURE, TESTING, PLAN refinements, root + backend READMEs.

Session/account REST handlers deferred to Stage 6 (gateway); OTLP + dashboards
to Stage 11.
2026-06-02 13:52:26 +02:00

83 lines
2.3 KiB
Go

package telemetry
import (
"context"
"testing"
)
// TestConfigValidate covers the supported and rejected exporter selections.
func TestConfigValidate(t *testing.T) {
if err := DefaultConfig().Validate(); err != nil {
t.Fatalf("default config must be valid: %v", err)
}
otlp := DefaultConfig()
otlp.TracesExporter = "otlp"
if err := otlp.Validate(); err == nil {
t.Error("otlp exporter must be rejected in the MVP set")
}
noName := DefaultConfig()
noName.ServiceName = ""
if err := noName.Validate(); err == nil {
t.Error("empty service name must be rejected")
}
}
// TestNewNoneAndShutdown builds the providers with the none exporter and shuts
// them down.
func TestNewNoneAndShutdown(t *testing.T) {
rt, err := New(context.Background(), DefaultConfig())
if err != nil {
t.Fatalf("New: %v", err)
}
if rt.TracerProvider() == nil {
t.Error("TracerProvider must not be nil")
}
if rt.MeterProvider() == nil {
t.Error("MeterProvider must not be nil")
}
if err := rt.Shutdown(context.Background()); err != nil {
t.Errorf("Shutdown: %v", err)
}
}
// TestNewStdoutTraces builds the providers with the stdout trace exporter; no
// spans are recorded, so shutdown flushes nothing to stdout.
func TestNewStdoutTraces(t *testing.T) {
cfg := DefaultConfig()
cfg.TracesExporter = ExporterStdout
rt, err := New(context.Background(), cfg)
if err != nil {
t.Fatalf("New: %v", err)
}
if err := rt.Shutdown(context.Background()); err != nil {
t.Errorf("Shutdown: %v", err)
}
}
// TestTraceFieldsFromContextEmpty returns no fields without an active span.
func TestTraceFieldsFromContextEmpty(t *testing.T) {
if TraceFieldsFromContext(context.Background()) != nil {
t.Error("expected nil fields without an active span")
}
var nilCtx context.Context
if TraceFieldsFromContext(nilCtx) != nil {
t.Error("expected nil fields for a nil context")
}
}
// TestNilRuntime checks the nil-receiver fallbacks used before initialisation.
func TestNilRuntime(t *testing.T) {
var rt *Runtime
if rt.TracerProvider() == nil {
t.Error("nil runtime must fall back to the global tracer provider")
}
if rt.MeterProvider() == nil {
t.Error("nil runtime must fall back to the global meter provider")
}
if err := rt.Shutdown(context.Background()); err != nil {
t.Errorf("nil runtime Shutdown: %v", err)
}
}