forked from libp2p/go-yamux
-
Notifications
You must be signed in to change notification settings - Fork 4
/
session_norace_test.go
169 lines (150 loc) · 3.1 KB
/
session_norace_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
//go:build !race
package yamux
import (
"bytes"
"context"
"io"
"sync"
"testing"
"time"
)
func TestSession_PingOfDeath(t *testing.T) {
conf := testConfNoKeepAlive()
// This test is slow and can easily time out on writes on CI.
//
// In the future, we might want to prioritize ping-replies over even
// other control messages, but that seems like overkill for now.
conf.ConnectionWriteTimeout = 1 * time.Second
client, server := testClientServerConfig(conf)
defer client.Close()
defer server.Close()
count := 10000
var wg sync.WaitGroup
begin := make(chan struct{})
for i := 0; i < count; i++ {
wg.Add(2)
go func() {
defer wg.Done()
<-begin
if _, err := server.Ping(); err != nil {
t.Error(err)
}
}()
go func() {
defer wg.Done()
<-begin
if _, err := client.Ping(); err != nil {
t.Error(err)
}
}()
}
close(begin)
wg.Wait()
}
func TestSendData_VeryLarge(t *testing.T) {
client, server := testClientServer()
defer client.Close()
defer server.Close()
var n int64 = 1 * 1024 * 1024 * 1024
var workers int = 16
wg := &sync.WaitGroup{}
wg.Add(workers * 2)
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
stream, err := server.AcceptStream()
if err != nil {
t.Errorf("err: %v", err)
return
}
defer stream.Close()
buf := make([]byte, 4)
_, err = io.ReadFull(stream, buf)
if err != nil {
t.Errorf("err: %v", err)
return
}
if !bytes.Equal(buf, []byte{0, 1, 2, 3}) {
t.Errorf("bad header")
return
}
recv, err := io.Copy(io.Discard, stream)
if err != nil {
t.Errorf("err: %v", err)
return
}
if recv != n {
t.Errorf("bad: %v", recv)
return
}
}()
}
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
stream, err := client.Open(context.Background())
if err != nil {
t.Errorf("err: %v", err)
return
}
defer stream.Close()
_, err = stream.Write([]byte{0, 1, 2, 3})
if err != nil {
t.Errorf("err: %v", err)
return
}
unlimited := &UnlimitedReader{}
sent, err := io.Copy(stream, io.LimitReader(unlimited, n))
if err != nil {
t.Errorf("err: %v", err)
return
}
if sent != n {
t.Errorf("bad: %v", sent)
return
}
}()
}
doneCh := make(chan struct{})
go func() {
wg.Wait()
close(doneCh)
}()
select {
case <-doneCh:
case <-time.After(20 * time.Second):
server.Close()
client.Close()
wg.Wait()
t.Fatal("timeout")
}
}
func TestLargeWindow(t *testing.T) {
conf := DefaultConfig()
conf.MaxStreamWindowSize *= 2
client, server := testClientServerConfig(conf)
defer client.Close()
defer server.Close()
stream, err := client.Open(context.Background())
if err != nil {
t.Fatalf("err: %v", err)
}
defer stream.Close()
stream2, err := server.Accept()
if err != nil {
t.Fatalf("err: %v", err)
}
defer stream2.Close()
err = stream.SetWriteDeadline(time.Now().Add(10 * time.Millisecond))
if err != nil {
t.Fatal(err)
}
buf := make([]byte, initialStreamWindow)
n, err := stream.Write(buf)
if err != nil {
t.Fatalf("err: %v", err)
}
if n != len(buf) {
t.Fatalf("short write: %d", n)
}
}