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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
package clients
import (
"context"
"math/rand"
"testing"
"time"
"github.com/mimecast/dtail/internal/clients/connectors"
"github.com/mimecast/dtail/internal/clients/handlers"
"github.com/mimecast/dtail/internal/io/dlog"
"github.com/mimecast/dtail/internal/omode"
sshclient "github.com/mimecast/dtail/internal/ssh/client"
gossh "golang.org/x/crypto/ssh"
)
func TestNextRetryDelay(t *testing.T) {
tests := []struct {
name string
current time.Duration
want time.Duration
}{
{name: "zero uses initial", current: 0, want: initialRetryDelay},
{name: "doubles normally", current: 4 * time.Second, want: 8 * time.Second},
{name: "caps at max", current: 40 * time.Second, want: maxRetryDelay},
{name: "stays max at max", current: maxRetryDelay, want: maxRetryDelay},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := nextRetryDelay(tt.current); got != tt.want {
t.Fatalf("nextRetryDelay(%v) = %v, want %v", tt.current, got, tt.want)
}
})
}
}
func TestJitterRetryDelayWithinBounds(t *testing.T) {
base := 10 * time.Second
random := rand.New(rand.NewSource(1))
min := 8 * time.Second
max := 12 * time.Second
for i := 0; i < 100; i++ {
got := jitterRetryDelay(base, random)
if got < min || got > max {
t.Fatalf("jitterRetryDelay() = %v, expected between %v and %v", got, min, max)
}
}
}
func TestSleepWithContextCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
start := time.Now()
if sleepWithContext(ctx, time.Second) {
t.Fatalf("sleepWithContext should stop when context is canceled")
}
if time.Since(start) > 100*time.Millisecond {
t.Fatalf("sleepWithContext took too long to exit on canceled context")
}
}
func TestStartConnectionReconnectsWithLatestSessionSpec(t *testing.T) {
originalLogger := dlog.Client
dlog.Client = &dlog.DLog{}
t.Cleanup(func() {
dlog.Client = originalLogger
})
first := &retryTestConnector{
server: "srv1",
handler: &retryTestHandler{},
}
second := &retryTestConnector{
server: "srv1",
handler: &retryTestHandler{},
}
originalSpec := SessionSpec{
Mode: omode.TailClient,
Files: []string{"/var/log/app.log"},
Regex: "ERROR",
}
updatedSpec := SessionSpec{
Mode: omode.TailClient,
Files: []string{"/var/log/next.log"},
Regex: "WARN",
}
sleepCalls := 0
var capturedSpec SessionSpec
client := &baseClient{
retry: true,
sessionSpec: originalSpec,
stats: &stats{
connectionsEstCh: make(chan struct{}, 1),
},
connections: []connectors.Connector{first},
connectionFactory: func(server string, _ []gossh.AuthMethod,
_ sshclient.HostKeyCallback, sessionSpec SessionSpec, _ bool) connectors.Connector {
if server != "srv1" {
t.Fatalf("unexpected reconnect server %q", server)
}
capturedSpec = sessionSpec
return second
},
}
client.sleepFn = func(context.Context, time.Duration) bool {
if sleepCalls == 0 {
sleepCalls++
client.sessionSpec = updatedSpec
return true
}
return false
}
status := client.startConnection(context.Background(), 0, first)
if status != 0 {
t.Fatalf("startConnection() status = %d, want 0", status)
}
if capturedSpec.Regex != updatedSpec.Regex || len(capturedSpec.Files) != 1 || capturedSpec.Files[0] != updatedSpec.Files[0] {
t.Fatalf("reconnect used stale session spec: got %#v want %#v", capturedSpec, updatedSpec)
}
if client.connections[0] != second {
t.Fatalf("expected retried connector to replace the original connection")
}
}
type retryTestConnector struct {
handler handlers.Handler
server string
}
func (c *retryTestConnector) Start(context.Context, context.CancelFunc, chan struct{}, chan struct{}) {
}
func (c *retryTestConnector) Server() string { return c.server }
func (c *retryTestConnector) Handler() handlers.Handler { return c.handler }
func (*retryTestConnector) SupportsQueryUpdates(time.Duration) bool { return false }
func (*retryTestConnector) ApplySessionSpec(SessionSpec, time.Duration) error { return nil }
func (*retryTestConnector) CommittedSession() (SessionSpec, uint64, bool) {
return SessionSpec{}, 0, false
}
type retryTestHandler struct{}
func (*retryTestHandler) Read([]byte) (int, error) { return 0, nil }
func (*retryTestHandler) Write(p []byte) (int, error) { return len(p), nil }
func (*retryTestHandler) Capabilities() []string { return nil }
func (*retryTestHandler) HasCapability(string) bool { return false }
func (*retryTestHandler) SendMessage(string) error { return nil }
func (*retryTestHandler) Server() string { return "srv1" }
func (*retryTestHandler) Status() int { return 0 }
func (*retryTestHandler) Shutdown() {}
func (*retryTestHandler) Done() <-chan struct{} {
done := make(chan struct{})
close(done)
return done
}
func (*retryTestHandler) WaitForCapabilities(time.Duration) bool { return false }
func (*retryTestHandler) WaitForSessionAck(time.Duration) (handlers.SessionAck, bool) {
return handlers.SessionAck{}, false
}
|