-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconn_test.go
160 lines (145 loc) · 4.59 KB
/
conn_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
package masque
import (
"bytes"
"context"
"errors"
"golang.org/x/exp/rand"
"io"
"log"
"os"
"testing"
"time"
"github.com/quic-go/quic-go/http3"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
func TestCapsuleSkipping(t *testing.T) {
log.SetOutput(io.Discard)
defer log.SetOutput(os.Stderr)
var buf bytes.Buffer
require.NoError(t, http3.WriteCapsule(&buf, 1337, []byte("foo")))
require.NoError(t, http3.WriteCapsule(&buf, 42, []byte("bar")))
require.ErrorIs(t, skipCapsules(&buf), io.EOF)
}
func TestReadDeadline(t *testing.T) {
setupStreamAndConn := func() (*MockStream, *proxiedConn) {
str := NewMockStream(gomock.NewController(t))
done := make(chan struct{})
t.Cleanup(func() {
str.EXPECT().Close().MaxTimes(1)
close(done)
})
str.EXPECT().Read(gomock.Any()).DoAndReturn(func([]byte) (int, error) {
<-done
return 0, errors.New("test done")
}).MaxTimes(1)
return str, newProxiedConn(str, nil)
}
t.Run("read after deadline", func(t *testing.T) {
str, conn := setupStreamAndConn()
str.EXPECT().ReceiveDatagram(gomock.Any()).DoAndReturn(func(ctx context.Context) ([]byte, error) {
<-ctx.Done()
return nil, ctx.Err()
})
require.NoError(t, conn.SetReadDeadline(time.Now().Add(-time.Second)))
_, _, err := conn.ReadFrom(make([]byte, 100))
require.ErrorIs(t, err, os.ErrDeadlineExceeded)
})
t.Run("unblocking read", func(t *testing.T) {
str, conn := setupStreamAndConn()
str.EXPECT().ReceiveDatagram(gomock.Any()).DoAndReturn(func(ctx context.Context) ([]byte, error) {
<-ctx.Done()
return nil, ctx.Err()
}).Times(2)
errChan := make(chan error, 1)
go func() {
_, _, err := conn.ReadFrom(make([]byte, 100))
errChan <- err
}()
select {
case err := <-errChan:
t.Fatalf("didn't expect ReadFrom to return early: %v", err)
case <-time.After(scaleDuration(50 * time.Millisecond)):
}
require.NoError(t, conn.SetReadDeadline(time.Now().Add(-time.Second)))
select {
case err := <-errChan:
require.ErrorIs(t, err, os.ErrDeadlineExceeded)
case <-time.After(scaleDuration(100 * time.Millisecond)):
t.Fatal("timeout")
}
_, _, err := conn.ReadFrom(make([]byte, 100))
require.ErrorIs(t, err, os.ErrDeadlineExceeded)
})
t.Run("extending the deadline", func(t *testing.T) {
str, conn := setupStreamAndConn()
str.EXPECT().ReceiveDatagram(gomock.Any()).DoAndReturn(func(ctx context.Context) ([]byte, error) {
<-ctx.Done()
return nil, ctx.Err()
}).MaxTimes(2) // might be called a 2nd time depending on when the cancellation Go routine does its job
start := time.Now()
d := scaleDuration(75 * time.Millisecond)
require.NoError(t, conn.SetReadDeadline(start.Add(d)))
errChan := make(chan error, 1)
go func() {
_, _, err := conn.ReadFrom(make([]byte, 100))
errChan <- err
}()
require.NoError(t, conn.SetReadDeadline(start.Add(2*d)))
select {
case err := <-errChan:
if since := time.Since(start); since < 2*d {
require.ErrorIs(t, err, os.ErrDeadlineExceeded)
t.Fatalf("ReadFrom returned early: %s, expected >= %s", since, 2*d)
}
case <-time.After(10 * d):
t.Fatal("timeout")
}
})
t.Run("cancelling the deadline", func(t *testing.T) {
str, conn := setupStreamAndConn()
str.EXPECT().ReceiveDatagram(gomock.Any()).DoAndReturn(func(ctx context.Context) ([]byte, error) {
<-ctx.Done()
return nil, ctx.Err()
})
start := time.Now()
d := scaleDuration(75 * time.Millisecond)
require.NoError(t, conn.SetReadDeadline(start.Add(d)))
errChan := make(chan error, 1)
go func() {
_, _, err := conn.ReadFrom(make([]byte, 100))
errChan <- err
}()
require.NoError(t, conn.SetReadDeadline(time.Time{}))
select {
case <-errChan:
t.Fatal("deadline was cancelled")
case <-time.After(2 * d):
}
// test shutdown
require.NoError(t, conn.SetReadDeadline(time.Now()))
select {
case err := <-errChan:
require.Error(t, err)
case <-time.After(scaleDuration(100 * time.Millisecond)):
t.Fatal("timeout")
}
})
t.Run("multiple deadlines", func(t *testing.T) {
str, conn := setupStreamAndConn()
const num = 10
const maxDeadline = 5 * time.Millisecond
str.EXPECT().ReceiveDatagram(gomock.Any()).DoAndReturn(func(ctx context.Context) ([]byte, error) {
<-ctx.Done()
return nil, ctx.Err()
}).MinTimes(num)
for i := 0; i < num; i++ {
// random duration between -5ms and 5ms
d := scaleDuration(maxDeadline - time.Duration(rand.Intn(int(2*maxDeadline))))
t.Logf("setting deadline to %v", d)
require.NoError(t, conn.SetReadDeadline(time.Now().Add(d)))
_, _, err := conn.ReadFrom(make([]byte, 100))
require.ErrorIs(t, err, os.ErrDeadlineExceeded)
}
})
}