-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkeepalive_test.go
More file actions
120 lines (107 loc) · 3.1 KB
/
Copy pathkeepalive_test.go
File metadata and controls
120 lines (107 loc) · 3.1 KB
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
package iago
import (
"errors"
"sync"
"testing"
"time"
)
// fakePinger records SendRequest calls and returns a configurable error, so the
// keepalive loop can be exercised without a live SSH connection.
type fakePinger struct {
mu sync.Mutex
calls int
err error
}
func (f *fakePinger) SendRequest(name string, wantReply bool, payload []byte) (bool, []byte, error) {
f.mu.Lock()
defer f.mu.Unlock()
f.calls++
return false, nil, f.err
}
func (f *fakePinger) callCount() int {
f.mu.Lock()
defer f.mu.Unlock()
return f.calls
}
func TestKeepAliveOption(t *testing.T) {
if got := applyGroupOptions(KeepAlive(30 * time.Second)).keepAliveInterval; got != 30*time.Second {
t.Errorf("keepAliveInterval = %v, want 30s", got)
}
if got := applyGroupOptions().keepAliveInterval; got != 0 {
t.Errorf("default keepAliveInterval = %v, want 0", got)
}
}
// TestKeepAliveLoopOnDead verifies that a failing ping invokes onDead once and
// stops the loop, so a dead connection is torn down instead of hanging.
func TestKeepAliveLoopOnDead(t *testing.T) {
p := &fakePinger{err: errors.New("connection dead")}
tick := make(chan time.Time)
done := make(chan struct{})
dead := make(chan struct{}, 1)
finished := make(chan struct{})
go func() {
keepAliveLoop(p, tick, func() { dead <- struct{}{} }, done)
close(finished)
}()
tick <- time.Now()
select {
case <-dead:
case <-time.After(time.Second):
t.Fatal("onDead not called after ping failure")
}
select {
case <-finished:
case <-time.After(time.Second):
t.Fatal("loop did not return after ping failure")
}
}
// TestKeepAliveLoopStopsOnDone verifies that closing done stops the loop without
// reporting the connection dead.
func TestKeepAliveLoopStopsOnDone(t *testing.T) {
p := &fakePinger{}
tick := make(chan time.Time)
done := make(chan struct{})
var deadCalled bool
finished := make(chan struct{})
go func() {
keepAliveLoop(p, tick, func() { deadCalled = true }, done)
close(finished)
}()
close(done)
select {
case <-finished:
case <-time.After(time.Second):
t.Fatal("loop did not return after done closed")
}
if deadCalled {
t.Error("onDead should not be called on a clean stop")
}
}
// TestKeepAliveLoopPingsUntilStopped verifies that successful pings keep the loop
// running across multiple ticks until done is closed.
func TestKeepAliveLoopPingsUntilStopped(t *testing.T) {
p := &fakePinger{}
tick := make(chan time.Time)
done := make(chan struct{})
finished := make(chan struct{})
go func() {
keepAliveLoop(p, tick, nil, done)
close(finished)
}()
// Each send returns only after the loop receives the previous tick, so two
// successful sends prove the loop kept running rather than exiting early.
tick <- time.Now()
tick <- time.Now()
close(done)
<-finished
if got := p.callCount(); got < 2 {
t.Errorf("ping calls = %d, want >= 2", got)
}
}
// TestStartKeepAliveStopIsIdempotent verifies the stop function can be called
// repeatedly (Close may run after the loop already exited) without panicking.
func TestStartKeepAliveStopIsIdempotent(t *testing.T) {
stop := startKeepAlive(&fakePinger{}, time.Hour, nil)
stop()
stop()
}