@@ -3,6 +3,7 @@ package itest
3
3
import (
4
4
"context"
5
5
"sync"
6
+ "sync/atomic"
6
7
"testing"
7
8
"time"
8
9
@@ -16,11 +17,10 @@ func TestResourceManagerConnInbound(t *testing.T) {
16
17
// this test checks that we can not exceed the inbound conn limit at system level
17
18
// we specify: 1 conn per peer, 3 conns total, and we try to create 4 conns
18
19
limiter := rcmgr .NewFixedLimiter (1 << 30 )
19
- limiter .SystemLimits = limiter .SystemLimits .
20
- WithConnLimit (3 , 1024 )
21
- limiter .DefaultPeerLimits = limiter .DefaultPeerLimits .
22
- WithConnLimit (1 , 16 )
20
+ limiter .SystemLimits = limiter .SystemLimits .WithConnLimit (3 , 1024 )
21
+ limiter .DefaultPeerLimits = limiter .DefaultPeerLimits .WithConnLimit (1 , 16 )
23
22
echos := createEchos (t , 5 , libp2p .ResourceManager (rcmgr .NewResourceManager (limiter )))
23
+ defer closeEchos (echos )
24
24
25
25
for i := 1 ; i < 4 ; i ++ {
26
26
err := echos [i ].Host .Connect (context .Background (), peer.AddrInfo {ID : echos [0 ].Host .ID ()})
@@ -47,11 +47,10 @@ func TestResourceManagerConnOutbound(t *testing.T) {
47
47
// this test checks that we can not exceed the inbound conn limit at system level
48
48
// we specify: 1 conn per peer, 3 conns total, and we try to create 4 conns
49
49
limiter := rcmgr .NewFixedLimiter (1 << 30 )
50
- limiter .SystemLimits = limiter .SystemLimits .
51
- WithConnLimit (1024 , 3 )
52
- limiter .DefaultPeerLimits = limiter .DefaultPeerLimits .
53
- WithConnLimit (16 , 1 )
50
+ limiter .SystemLimits = limiter .SystemLimits .WithConnLimit (1024 , 3 )
51
+ limiter .DefaultPeerLimits = limiter .DefaultPeerLimits .WithConnLimit (16 , 1 )
54
52
echos := createEchos (t , 5 , libp2p .ResourceManager (rcmgr .NewResourceManager (limiter )))
53
+ defer closeEchos (echos )
55
54
56
55
for i := 1 ; i < 4 ; i ++ {
57
56
err := echos [0 ].Host .Connect (context .Background (), peer.AddrInfo {ID : echos [i ].Host .ID ()})
@@ -78,15 +77,34 @@ func TestResourceManagerServiceInbound(t *testing.T) {
78
77
// this test checks that we can not exceed the inbound stream limit at service level
79
78
// we specify: 3 streams for the service, and we try to create 4 streams
80
79
limiter := rcmgr .NewFixedLimiter (1 << 30 )
81
- limiter .DefaultServiceLimits = limiter .DefaultServiceLimits .
82
- WithStreamLimit (3 , 1024 )
80
+ limiter .DefaultServiceLimits = limiter .DefaultServiceLimits .WithStreamLimit (3 , 1024 )
83
81
echos := createEchos (t , 5 , libp2p .ResourceManager (rcmgr .NewResourceManager (limiter )))
84
-
85
- echos [0 ].WaitBeforeRead = func () error {
86
- time .Sleep (100 * time .Millisecond )
87
- return nil
82
+ defer closeEchos (echos )
83
+
84
+ waitForSignal := func (count * int32 , ready * chan struct {}) func () error {
85
+ return func () error {
86
+ if atomic .AddInt32 (count , - 1 ) == 0 {
87
+ close (* ready )
88
+ } else {
89
+ <- * ready
90
+ }
91
+ return nil
92
+ }
93
+ }
94
+ waitForChannel := func (ready * chan struct {}) func () error {
95
+ return func () error {
96
+ <- * ready
97
+ return nil
98
+ }
88
99
}
89
100
101
+ count1 := new (int32 )
102
+ ready1 := new (chan struct {})
103
+ ready2 := new (chan struct {})
104
+
105
+ echos [0 ].BeforeReserve = waitForSignal (count1 , ready1 )
106
+ echos [0 ].BeforeRead = waitForChannel (ready2 )
107
+
90
108
for i := 1 ; i < 5 ; i ++ {
91
109
err := echos [i ].Host .Connect (context .Background (), peer.AddrInfo {ID : echos [0 ].Host .ID ()})
92
110
if err != nil {
@@ -95,6 +113,10 @@ func TestResourceManagerServiceInbound(t *testing.T) {
95
113
time .Sleep (10 * time .Millisecond )
96
114
}
97
115
116
+ * count1 = 4
117
+ * ready1 = make (chan struct {})
118
+ * ready2 = make (chan struct {})
119
+
98
120
var wg sync.WaitGroup
99
121
for i := 1 ; i < 5 ; i ++ {
100
122
wg .Add (1 )
@@ -104,6 +126,7 @@ func TestResourceManagerServiceInbound(t *testing.T) {
104
126
err := echos [i ].Echo (echos [0 ].Host .ID (), "hello libp2p" )
105
127
if err != nil {
106
128
t .Log (err )
129
+ close (* ready2 )
107
130
}
108
131
}(i )
109
132
}
@@ -125,12 +148,32 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
125
148
EchoService : limiter .DefaultPeerLimits .WithStreamLimit (2 , 1024 ),
126
149
}
127
150
echos := createEchos (t , 5 , libp2p .ResourceManager (rcmgr .NewResourceManager (limiter )))
128
-
129
- echos [0 ].WaitBeforeRead = func () error {
130
- time .Sleep (100 * time .Millisecond )
131
- return nil
151
+ defer closeEchos (echos )
152
+
153
+ waitForSignal := func (count * int32 , ready * chan struct {}) func () error {
154
+ return func () error {
155
+ if atomic .AddInt32 (count , - 1 ) == 0 {
156
+ close (* ready )
157
+ } else {
158
+ <- * ready
159
+ }
160
+ return nil
161
+ }
162
+ }
163
+ waitForChannel := func (ready * chan struct {}) func () error {
164
+ return func () error {
165
+ <- * ready
166
+ return nil
167
+ }
132
168
}
133
169
170
+ count1 := new (int32 )
171
+ count2 := new (int32 )
172
+ ready1 := new (chan struct {})
173
+ ready2 := new (chan struct {})
174
+ echos [0 ].BeforeReserve = waitForSignal (count1 , ready1 )
175
+ echos [0 ].BeforeRead = waitForSignal (count2 , ready2 )
176
+
134
177
for i := 1 ; i < 5 ; i ++ {
135
178
err := echos [i ].Host .Connect (context .Background (), peer.AddrInfo {ID : echos [0 ].Host .ID ()})
136
179
if err != nil {
@@ -139,6 +182,11 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
139
182
time .Sleep (10 * time .Millisecond )
140
183
}
141
184
185
+ * count1 = 4
186
+ * count2 = 4
187
+ * ready1 = make (chan struct {})
188
+ * ready2 = make (chan struct {})
189
+
142
190
var wg sync.WaitGroup
143
191
for i := 1 ; i < 5 ; i ++ {
144
192
wg .Add (1 )
@@ -160,6 +208,11 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
160
208
ResourceServiceErrors : 0 ,
161
209
})
162
210
211
+ * count1 = 3
212
+ * ready1 = make (chan struct {})
213
+ * ready2 = make (chan struct {})
214
+ echos [0 ].BeforeRead = waitForChannel (ready2 )
215
+
163
216
for i := 0 ; i < 3 ; i ++ {
164
217
wg .Add (1 )
165
218
go func () {
@@ -168,6 +221,7 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
168
221
err := echos [2 ].Echo (echos [0 ].Host .ID (), "hello libp2p" )
169
222
if err != nil {
170
223
t .Log (err )
224
+ close (* ready2 )
171
225
}
172
226
}()
173
227
}
0 commit comments