|
- package app
-
- import (
- "testing"
-
- cfgpkg "github.com/jan/fm-rds-tx/internal/config"
- "github.com/jan/fm-rds-tx/internal/output"
- "github.com/jan/fm-rds-tx/internal/platform"
- )
-
- func TestEngineRuntimeStateReporting(t *testing.T) {
- e := NewEngine(cfgpkg.Default(), platform.NewSimulatedDriver(nil))
-
- if got := e.Stats().State; got != string(RuntimeStateIdle) {
- t.Fatalf("expected initial state idle, got %s", got)
- }
-
- e.setRuntimeState(RuntimeStatePrebuffering)
- if got := e.Stats().State; got != string(RuntimeStatePrebuffering) {
- t.Fatalf("expected prebuffering, got %s", got)
- }
-
- e.setRuntimeState(RuntimeStateRunning)
- if got := e.currentRuntimeState(); got != RuntimeStateRunning {
- t.Fatalf("currentRuntimeState mismatch: %s", got)
- }
- }
-
- func TestEngineRuntimeStateTransitions(t *testing.T) {
- e := NewEngine(cfgpkg.Default(), platform.NewSimulatedDriver(nil))
- e.setRuntimeState(RuntimeStatePrebuffering)
-
- queue := output.QueueStats{Depth: 1, FillLevel: 0.75, Health: output.QueueHealthNormal}
- e.evaluateRuntimeState(queue, false)
- if got := e.currentRuntimeState(); got != RuntimeStateRunning {
- t.Fatalf("expected running after full buffer, got %s", got)
- }
-
- queue.Health = output.QueueHealthCritical
- for i := 0; i < queueCriticalStreakThreshold; i++ {
- e.evaluateRuntimeState(queue, false)
- }
- if got := e.currentRuntimeState(); got != RuntimeStateDegraded {
- t.Fatalf("expected degraded on queue critical streak, got %s", got)
- }
-
- queue.Health = output.QueueHealthNormal
- e.evaluateRuntimeState(queue, false)
- if got := e.currentRuntimeState(); got != RuntimeStateRunning {
- t.Fatalf("expected running once queue healthy, got %s", got)
- }
-
- e.evaluateRuntimeState(queue, true)
- if got := e.currentRuntimeState(); got != RuntimeStateDegraded {
- t.Fatalf("expected degraded when late buffers seen, got %s", got)
- }
- }
-
- func TestEngineRuntimeStateMuteOnPersistentQueueCritical(t *testing.T) {
- e := NewEngine(cfgpkg.Default(), platform.NewSimulatedDriver(nil))
- e.setRuntimeState(RuntimeStateRunning)
-
- queue := output.QueueStats{Depth: 1, Health: output.QueueHealthCritical}
- for i := 0; i < queueMutedStreakThreshold; i++ {
- e.evaluateRuntimeState(queue, false)
- }
-
- if got := e.currentRuntimeState(); got != RuntimeStateMuted {
- t.Fatalf("expected muted after prolonged queue critical, got %s", got)
- }
-
- last := e.LastFault()
- if last == nil {
- t.Fatal("expected fault recorded for the mute transition")
- }
- if last.Reason != FaultReasonQueueCritical {
- t.Fatalf("expected queue critical reason, got %s", last.Reason)
- }
- if last.Severity != FaultSeverityMuted {
- t.Fatalf("expected muted severity, got %s", last.Severity)
- }
- }
|