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