-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_go_test.go
169 lines (157 loc) · 3.75 KB
/
conn_go_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
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
// +build !js
package dtls
import (
"bytes"
"context"
"crypto/tls"
"net"
"testing"
"time"
"github.com/pion/dtls/v2/internal/net/dpipe"
"github.com/pion/dtls/v2/pkg/crypto/selfsign"
"github.com/pion/transport/test"
)
func TestContextConfig(t *testing.T) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 20)
defer lim.Stop()
report := test.CheckRoutines(t)
defer report()
addrListen, err := net.ResolveUDPAddr("udp", "localhost:0")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Dummy listener
listen, err := net.ListenUDP("udp", addrListen)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer func() {
_ = listen.Close()
}()
addr := listen.LocalAddr().(*net.UDPAddr)
cert, err := selfsign.GenerateSelfSigned()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
config := &Config{
ConnectContextMaker: func() (context.Context, func()) {
return context.WithTimeout(context.Background(), 40*time.Millisecond)
},
Certificates: []tls.Certificate{cert},
}
dials := map[string]struct {
f func() (func() (net.Conn, error), func())
order []byte
}{
"Dial": {
f: func() (func() (net.Conn, error), func()) {
return func() (net.Conn, error) {
return Dial("udp", addr, config)
}, func() {
}
},
order: []byte{0, 1, 2},
},
"DialWithContext": {
f: func() (func() (net.Conn, error), func()) {
ctx, cancel := context.WithTimeout(context.Background(), 80*time.Millisecond)
return func() (net.Conn, error) {
return DialWithContext(ctx, "udp", addr, config)
}, func() {
cancel()
}
},
order: []byte{0, 2, 1},
},
"Client": {
f: func() (func() (net.Conn, error), func()) {
ca, _ := dpipe.Pipe()
return func() (net.Conn, error) {
return Client(ca, config)
}, func() {
_ = ca.Close()
}
},
order: []byte{0, 1, 2},
},
"ClientWithContext": {
f: func() (func() (net.Conn, error), func()) {
ctx, cancel := context.WithTimeout(context.Background(), 80*time.Millisecond)
ca, _ := dpipe.Pipe()
return func() (net.Conn, error) {
return ClientWithContext(ctx, ca, config)
}, func() {
cancel()
_ = ca.Close()
}
},
order: []byte{0, 2, 1},
},
"Server": {
f: func() (func() (net.Conn, error), func()) {
ca, _ := dpipe.Pipe()
return func() (net.Conn, error) {
return Server(ca, config)
}, func() {
_ = ca.Close()
}
},
order: []byte{0, 1, 2},
},
"ServerWithContext": {
f: func() (func() (net.Conn, error), func()) {
ctx, cancel := context.WithTimeout(context.Background(), 80*time.Millisecond)
ca, _ := dpipe.Pipe()
return func() (net.Conn, error) {
return ServerWithContext(ctx, ca, config)
}, func() {
cancel()
_ = ca.Close()
}
},
order: []byte{0, 2, 1},
},
}
for name, dial := range dials {
dial := dial
t.Run(name, func(t *testing.T) {
done := make(chan struct{})
go func() {
d, cancel := dial.f()
conn, err := d()
defer cancel()
if err != errHandshakeTimeout {
t.Errorf("Expected error: '%v', got: '%v'", errHandshakeTimeout, err)
close(done)
return
}
done <- struct{}{}
if err == nil {
_ = conn.Close()
}
}()
var order []byte
early := time.After(20 * time.Millisecond)
late := time.After(60 * time.Millisecond)
func() {
for len(order) < 3 {
select {
case <-early:
order = append(order, 0)
case _, ok := <-done:
if !ok {
return
}
order = append(order, 1)
case <-late:
order = append(order, 2)
}
}
}()
if !bytes.Equal(dial.order, order) {
t.Errorf("Invalid cancel timing, expected: %v, got: %v", dial.order, order)
}
})
}
}