@@ -2,7 +2,9 @@ package itest
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"sync"
7
+ "sync/atomic"
6
8
"testing"
7
9
"time"
8
10
@@ -16,11 +18,10 @@ func TestResourceManagerConnInbound(t *testing.T) {
16
18
// this test checks that we can not exceed the inbound conn limit at system level
17
19
// we specify: 1 conn per peer, 3 conns total, and we try to create 4 conns
18
20
limiter := rcmgr .NewFixedLimiter (1 << 30 )
19
- limiter .SystemLimits = limiter .SystemLimits .
20
- WithConnLimit (3 , 1024 )
21
- limiter .DefaultPeerLimits = limiter .DefaultPeerLimits .
22
- WithConnLimit (1 , 16 )
21
+ limiter .SystemLimits = limiter .SystemLimits .WithConnLimit (3 , 1024 )
22
+ limiter .DefaultPeerLimits = limiter .DefaultPeerLimits .WithConnLimit (1 , 16 )
23
23
echos := createEchos (t , 5 , libp2p .ResourceManager (rcmgr .NewResourceManager (limiter )))
24
+ defer closeEchos (echos )
24
25
25
26
for i := 1 ; i < 4 ; i ++ {
26
27
err := echos [i ].Host .Connect (context .Background (), peer.AddrInfo {ID : echos [0 ].Host .ID ()})
@@ -47,11 +48,10 @@ func TestResourceManagerConnOutbound(t *testing.T) {
47
48
// this test checks that we can not exceed the inbound conn limit at system level
48
49
// we specify: 1 conn per peer, 3 conns total, and we try to create 4 conns
49
50
limiter := rcmgr .NewFixedLimiter (1 << 30 )
50
- limiter .SystemLimits = limiter .SystemLimits .
51
- WithConnLimit (1024 , 3 )
52
- limiter .DefaultPeerLimits = limiter .DefaultPeerLimits .
53
- WithConnLimit (16 , 1 )
51
+ limiter .SystemLimits = limiter .SystemLimits .WithConnLimit (1024 , 3 )
52
+ limiter .DefaultPeerLimits = limiter .DefaultPeerLimits .WithConnLimit (16 , 1 )
54
53
echos := createEchos (t , 5 , libp2p .ResourceManager (rcmgr .NewResourceManager (limiter )))
54
+ defer closeEchos (echos )
55
55
56
56
for i := 1 ; i < 4 ; i ++ {
57
57
err := echos [0 ].Host .Connect (context .Background (), peer.AddrInfo {ID : echos [i ].Host .ID ()})
@@ -78,14 +78,12 @@ func TestResourceManagerServiceInbound(t *testing.T) {
78
78
// this test checks that we can not exceed the inbound stream limit at service level
79
79
// we specify: 3 streams for the service, and we try to create 4 streams
80
80
limiter := rcmgr .NewFixedLimiter (1 << 30 )
81
- limiter .DefaultServiceLimits = limiter .DefaultServiceLimits .
82
- WithStreamLimit (3 , 1024 )
81
+ limiter .DefaultServiceLimits = limiter .DefaultServiceLimits .WithStreamLimit (3 , 1024 )
83
82
echos := createEchos (t , 5 , libp2p .ResourceManager (rcmgr .NewResourceManager (limiter )))
83
+ defer closeEchos (echos )
84
84
85
- echos [0 ].WaitBeforeRead = func () error {
86
- time .Sleep (100 * time .Millisecond )
87
- return nil
88
- }
85
+ ready := new (chan struct {})
86
+ echos [0 ].BeforeDone = waitForChannel (ready , time .Minute )
89
87
90
88
for i := 1 ; i < 5 ; i ++ {
91
89
err := echos [i ].Host .Connect (context .Background (), peer.AddrInfo {ID : echos [0 ].Host .ID ()})
@@ -95,6 +93,9 @@ func TestResourceManagerServiceInbound(t *testing.T) {
95
93
time .Sleep (10 * time .Millisecond )
96
94
}
97
95
96
+ * ready = make (chan struct {})
97
+
98
+ var once sync.Once
98
99
var wg sync.WaitGroup
99
100
for i := 1 ; i < 5 ; i ++ {
100
101
wg .Add (1 )
@@ -104,6 +105,9 @@ func TestResourceManagerServiceInbound(t *testing.T) {
104
105
err := echos [i ].Echo (echos [0 ].Host .ID (), "hello libp2p" )
105
106
if err != nil {
106
107
t .Log (err )
108
+ once .Do (func () {
109
+ close (* ready )
110
+ })
107
111
}
108
112
}(i )
109
113
}
@@ -125,11 +129,11 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
125
129
EchoService : limiter .DefaultPeerLimits .WithStreamLimit (2 , 1024 ),
126
130
}
127
131
echos := createEchos (t , 5 , libp2p .ResourceManager (rcmgr .NewResourceManager (limiter )))
132
+ defer closeEchos (echos )
128
133
129
- echos [0 ].WaitBeforeRead = func () error {
130
- time .Sleep (100 * time .Millisecond )
131
- return nil
132
- }
134
+ count := new (int32 )
135
+ ready := new (chan struct {})
136
+ echos [0 ].BeforeDone = waitForBarrier (count , ready , time .Minute )
133
137
134
138
for i := 1 ; i < 5 ; i ++ {
135
139
err := echos [i ].Host .Connect (context .Background (), peer.AddrInfo {ID : echos [0 ].Host .ID ()})
@@ -139,6 +143,9 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
139
143
time .Sleep (10 * time .Millisecond )
140
144
}
141
145
146
+ * count = 4
147
+ * ready = make (chan struct {})
148
+
142
149
var wg sync.WaitGroup
143
150
for i := 1 ; i < 5 ; i ++ {
144
151
wg .Add (1 )
@@ -160,6 +167,10 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
160
167
ResourceServiceErrors : 0 ,
161
168
})
162
169
170
+ * ready = make (chan struct {})
171
+ echos [0 ].BeforeDone = waitForChannel (ready , time .Minute )
172
+
173
+ var once sync.Once
163
174
for i := 0 ; i < 3 ; i ++ {
164
175
wg .Add (1 )
165
176
go func () {
@@ -168,6 +179,9 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
168
179
err := echos [2 ].Echo (echos [0 ].Host .ID (), "hello libp2p" )
169
180
if err != nil {
170
181
t .Log (err )
182
+ once .Do (func () {
183
+ close (* ready )
184
+ })
171
185
}
172
186
}()
173
187
}
@@ -180,3 +194,29 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
180
194
ResourceServiceErrors : 1 ,
181
195
})
182
196
}
197
+
198
+ func waitForBarrier (count * int32 , ready * chan struct {}, timeout time.Duration ) func () error {
199
+ return func () error {
200
+ if atomic .AddInt32 (count , - 1 ) == 0 {
201
+ close (* ready )
202
+ }
203
+
204
+ select {
205
+ case <- * ready :
206
+ return nil
207
+ case <- time .After (timeout ):
208
+ return fmt .Errorf ("timeout" )
209
+ }
210
+ }
211
+ }
212
+
213
+ func waitForChannel (ready * chan struct {}, timeout time.Duration ) func () error {
214
+ return func () error {
215
+ select {
216
+ case <- * ready :
217
+ return nil
218
+ case <- time .After (timeout ):
219
+ return fmt .Errorf ("timeout" )
220
+ }
221
+ }
222
+ }
0 commit comments