-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathheartbeat_test.go
61 lines (54 loc) · 1.19 KB
/
heartbeat_test.go
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
package muxado
import (
"context"
"net"
"testing"
"time"
)
// TestHeartbeatFast is a regression test for a 0ms
// timeout being detectable
func TestHeartbeatFast(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client, serv := net.Pipe()
stopServerHBs := make(chan struct{})
go func() {
sess := Server(serv, nil)
typed := NewTypedStreamSession(sess)
hb := NewHeartbeat(typed, func(d time.Duration, timeout bool) {
if timeout {
panic("timeout")
}
}, &HeartbeatConfig{
Interval: 5 * time.Millisecond,
Tolerance: 100 * time.Millisecond,
Type: defaultStreamType,
})
str, err := hb.AcceptTypedStream()
if err != nil {
panic(err)
}
<-stopServerHBs
str.Close()
<-ctx.Done()
sess.Close()
}()
clientSess := Client(client, nil)
clientTyped := NewTypedStreamSession(clientSess)
hb := NewHeartbeat(clientTyped, func(d time.Duration, timeout bool) {
if timeout {
panic("timeout")
}
}, &HeartbeatConfig{
Interval: 5 * time.Millisecond,
Tolerance: 500 * time.Millisecond,
Type: defaultStreamType,
})
hb.Start()
for i := 0; i < 10; i++ {
_, ok := hb.Beat()
if !ok {
t.Fatal("beat failed")
}
}
}