maintenance_manager_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package maintenance
  2. import (
  3. "errors"
  4. "testing"
  5. "time"
  6. )
  7. func TestMaintenanceManager_ErrorHandling(t *testing.T) {
  8. config := DefaultMaintenanceConfig()
  9. config.ScanIntervalSeconds = 1 // Short interval for testing (1 second)
  10. manager := NewMaintenanceManager(nil, config)
  11. // Test initial state
  12. if manager.errorCount != 0 {
  13. t.Errorf("Expected initial error count to be 0, got %d", manager.errorCount)
  14. }
  15. if manager.backoffDelay != time.Second {
  16. t.Errorf("Expected initial backoff delay to be 1s, got %v", manager.backoffDelay)
  17. }
  18. // Test error handling
  19. err := errors.New("dial tcp [::1]:19333: connect: connection refused")
  20. manager.handleScanError(err)
  21. if manager.errorCount != 1 {
  22. t.Errorf("Expected error count to be 1, got %d", manager.errorCount)
  23. }
  24. if manager.lastError != err {
  25. t.Errorf("Expected last error to be set")
  26. }
  27. // Test exponential backoff
  28. initialDelay := manager.backoffDelay
  29. manager.handleScanError(err)
  30. if manager.backoffDelay != initialDelay*2 {
  31. t.Errorf("Expected backoff delay to double, got %v", manager.backoffDelay)
  32. }
  33. if manager.errorCount != 2 {
  34. t.Errorf("Expected error count to be 2, got %d", manager.errorCount)
  35. }
  36. // Test backoff cap
  37. for i := 0; i < 10; i++ {
  38. manager.handleScanError(err)
  39. }
  40. if manager.backoffDelay > 5*time.Minute {
  41. t.Errorf("Expected backoff delay to be capped at 5 minutes, got %v", manager.backoffDelay)
  42. }
  43. // Test error reset
  44. manager.resetErrorTracking()
  45. if manager.errorCount != 0 {
  46. t.Errorf("Expected error count to be reset to 0, got %d", manager.errorCount)
  47. }
  48. if manager.backoffDelay != time.Second {
  49. t.Errorf("Expected backoff delay to be reset to 1s, got %v", manager.backoffDelay)
  50. }
  51. if manager.lastError != nil {
  52. t.Errorf("Expected last error to be reset to nil")
  53. }
  54. }
  55. func TestIsConnectionError(t *testing.T) {
  56. tests := []struct {
  57. err error
  58. expected bool
  59. }{
  60. {nil, false},
  61. {errors.New("connection refused"), true},
  62. {errors.New("dial tcp [::1]:19333: connect: connection refused"), true},
  63. {errors.New("connection error: desc = \"transport: Error while dialing\""), true},
  64. {errors.New("connection timeout"), true},
  65. {errors.New("no route to host"), true},
  66. {errors.New("network unreachable"), true},
  67. {errors.New("some other error"), false},
  68. {errors.New("invalid argument"), false},
  69. }
  70. for _, test := range tests {
  71. result := isConnectionError(test.err)
  72. if result != test.expected {
  73. t.Errorf("For error %v, expected %v, got %v", test.err, test.expected, result)
  74. }
  75. }
  76. }
  77. func TestMaintenanceManager_GetErrorState(t *testing.T) {
  78. config := DefaultMaintenanceConfig()
  79. manager := NewMaintenanceManager(nil, config)
  80. // Test initial state
  81. errorCount, lastError, backoffDelay := manager.GetErrorState()
  82. if errorCount != 0 || lastError != nil || backoffDelay != time.Second {
  83. t.Errorf("Expected initial state to be clean")
  84. }
  85. // Add some errors
  86. err := errors.New("test error")
  87. manager.handleScanError(err)
  88. manager.handleScanError(err)
  89. errorCount, lastError, backoffDelay = manager.GetErrorState()
  90. if errorCount != 2 || lastError != err || backoffDelay != 2*time.Second {
  91. t.Errorf("Expected error state to be tracked correctly: count=%d, err=%v, delay=%v",
  92. errorCount, lastError, backoffDelay)
  93. }
  94. }
  95. func TestMaintenanceManager_LogThrottling(t *testing.T) {
  96. config := DefaultMaintenanceConfig()
  97. manager := NewMaintenanceManager(nil, config)
  98. // This is a basic test to ensure the error handling doesn't panic
  99. // In practice, you'd want to capture log output to verify throttling
  100. err := errors.New("test error")
  101. // Generate many errors to test throttling
  102. for i := 0; i < 25; i++ {
  103. manager.handleScanError(err)
  104. }
  105. // Should not panic and should have capped backoff
  106. if manager.backoffDelay > 5*time.Minute {
  107. t.Errorf("Expected backoff to be capped at 5 minutes")
  108. }
  109. if manager.errorCount != 25 {
  110. t.Errorf("Expected error count to be 25, got %d", manager.errorCount)
  111. }
  112. }