faults_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package faults_test
  2. import (
  3. "testing"
  4. "time"
  5. "hth/internal/faults"
  6. hthv1 "hth/internal/gen/hth/api/v1"
  7. )
  8. func TestAllProtoTypesClassified(t *testing.T) {
  9. for tpe := hthv1.FaultType_FAULT_TYPE_CONFIG_INVALID; tpe <= hthv1.FaultType_FAULT_TYPE_WATCHDOG_TRIPPED; tpe++ {
  10. if faults.ClassOf(tpe) == hthv1.FaultClass_FAULT_CLASS_UNSPECIFIED {
  11. t.Fatalf("unclassified %v", tpe)
  12. }
  13. }
  14. }
  15. func TestDeviceReportedVsHostInferred(t *testing.T) {
  16. if faults.HostInferred(hthv1.FaultType_FAULT_TYPE_DEVICE_AXIS_FAULT) {
  17. t.Fatal("axis fault is device-reported")
  18. }
  19. if faults.HostInferred(hthv1.FaultType_FAULT_TYPE_WATCHDOG_TRIPPED) {
  20. t.Fatal("watchdog tripped is device-reported")
  21. }
  22. if !faults.HostInferred(hthv1.FaultType_FAULT_TYPE_CONTROL_LEASE_EXPIRED) {
  23. t.Fatal("lease expired is host-inferred")
  24. }
  25. }
  26. func TestTransitionUpdateThenClear(t *testing.T) {
  27. b := faults.NewBook(8)
  28. now := time.Unix(100, 0)
  29. f, kind := b.Transition(now, hthv1.FaultType_FAULT_TYPE_SERIAL_DISCONNECTED, "d0", "", "gone", "", nil)
  30. if kind != "FAULT_RAISED" || f.Count != 1 {
  31. t.Fatalf("raise %s %+v", kind, f)
  32. }
  33. if f.Latched {
  34. t.Fatal("auto-cleared faults must not be latched without an Acknowledge RPC")
  35. }
  36. f, kind = b.Transition(now.Add(time.Second), hthv1.FaultType_FAULT_TYPE_SERIAL_DISCONNECTED, "d0", "", "still gone", "", nil)
  37. if kind != "FAULT_UPDATED" || f.Count != 2 {
  38. t.Fatalf("update %s %+v", kind, f)
  39. }
  40. cleared := b.Clear(hthv1.FaultType_FAULT_TYPE_SERIAL_DISCONNECTED, "d0", "", now.Add(2*time.Second))
  41. if cleared == nil || cleared.Active {
  42. t.Fatal("cleared")
  43. }
  44. }