-
Notifications
You must be signed in to change notification settings - Fork 3
/
testing.go
118 lines (100 loc) · 2.24 KB
/
testing.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
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
package dmsg
import (
"errors"
"fmt"
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
noDelay = time.Duration(0)
smallDelay = 300 * time.Millisecond
chanReadThreshold = 5 * time.Second
testTimeout = 5 * time.Second
)
func closeClosers(closers ...io.Closer) error {
for _, closer := range closers {
if err := closer.Close(); err != nil {
return err
}
}
return nil
}
type connCounter interface {
connCount() int
}
func checkConnCount(t *testing.T, delay time.Duration, count int, ccs ...connCounter) {
require.NoError(t, testWithTimeout(delay, func() error {
for _, cc := range ccs {
if cc.connCount() != count {
return fmt.Errorf("connCount equals to %d, want %d", cc.connCount(), count)
}
}
return nil
}))
}
func checkTransportsClosed(t *testing.T, transports ...*Transport) {
for _, transport := range transports {
assert.False(t, isDoneChanOpen(transport.done))
assert.False(t, isReadChanOpen(transport.inCh))
}
}
func checkClientConnsClosed(t *testing.T, conns ...*ClientConn) {
for _, conn := range conns {
assert.False(t, isDoneChanOpen(conn.done))
}
}
// intended to test some func of `func() error` signature with a given timeout.
// Exceeding timeout results in error.
func testWithTimeout(timeout time.Duration, run func() error) error {
timer := time.NewTimer(timeout)
defer timer.Stop()
for {
if err := run(); err != nil {
select {
case <-timer.C:
return err
default:
time.Sleep(testTimeout)
continue
}
}
return nil
}
}
func isDoneChanOpen(ch <-chan struct{}) bool {
select {
case _, ok := <-ch:
return ok
case <-time.After(chanReadThreshold):
return false
}
}
func isReadChanOpen(ch <-chan Frame) bool {
select {
case _, ok := <-ch:
return ok
case <-time.After(chanReadThreshold):
return false
}
}
func errWithTimeout(ch <-chan error) error {
select {
case err := <-ch:
return err
case <-time.After(5 * time.Second):
return errors.New("timeout")
}
}
func getNextInitID(conn *ClientConn) uint16 {
conn.mx.Lock()
defer conn.mx.Unlock()
return conn.nextInitID
}
func getNextRespID(conn *ServerConn) uint16 {
conn.mx.Lock()
defer conn.mx.Unlock()
return conn.nextRespID
}