Skip to content

Commit c2d3426

Browse files
committed
basic rcmgr integration tests
1 parent 78e08c9 commit c2d3426

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

itest/rcmgr_test.go

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package itest
2+
3+
import (
4+
"context"
5+
"sync"
6+
"testing"
7+
"time"
8+
9+
rcmgr "github.com/libp2p/go-libp2p-resource-manager"
10+
11+
"github.com/libp2p/go-libp2p"
12+
"github.com/libp2p/go-libp2p-core/peer"
13+
)
14+
15+
func TestResourceManagerConnInbound(t *testing.T) {
16+
// this test checks that we can not exceed the inbound conn limit at system level
17+
// we specify: 1 conn per peer, 3 conns total, and we try to create 4 conns
18+
limiter := rcmgr.NewFixedLimiter(1 << 30)
19+
limiter.SystemLimits = limiter.SystemLimits.
20+
WithConnLimit(3, 1024)
21+
limiter.DefaultPeerLimits = limiter.DefaultPeerLimits.
22+
WithConnLimit(1, 16)
23+
echos := createEchos(t, 5, libp2p.ResourceManager(rcmgr.NewResourceManager(limiter)))
24+
25+
for i := 1; i < 4; i++ {
26+
err := echos[i].Host.Connect(context.Background(), peer.AddrInfo{ID: echos[0].Host.ID()})
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
}
31+
32+
for i := 1; i < 4; i++ {
33+
count := len(echos[i].Host.Network().ConnsToPeer(echos[0].Host.ID()))
34+
if count != 1 {
35+
t.Fatalf("expected %d connections to peer, got %d", 1, count)
36+
}
37+
}
38+
39+
err := echos[4].Host.Connect(context.Background(), peer.AddrInfo{ID: echos[0].Host.ID()})
40+
if err == nil {
41+
t.Fatal("expected ResourceManager to block incoming connection")
42+
}
43+
}
44+
45+
func TestResourceManagerConnOutbound(t *testing.T) {
46+
// this test checks that we can not exceed the inbound conn limit at system level
47+
// we specify: 1 conn per peer, 3 conns total, and we try to create 4 conns
48+
limiter := rcmgr.NewFixedLimiter(1 << 30)
49+
limiter.SystemLimits = limiter.SystemLimits.
50+
WithConnLimit(1024, 3)
51+
limiter.DefaultPeerLimits = limiter.DefaultPeerLimits.
52+
WithConnLimit(16, 1)
53+
echos := createEchos(t, 5, libp2p.ResourceManager(rcmgr.NewResourceManager(limiter)))
54+
55+
for i := 1; i < 4; i++ {
56+
err := echos[0].Host.Connect(context.Background(), peer.AddrInfo{ID: echos[i].Host.ID()})
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
}
61+
62+
for i := 1; i < 4; i++ {
63+
count := len(echos[i].Host.Network().ConnsToPeer(echos[0].Host.ID()))
64+
if count != 1 {
65+
t.Fatalf("expected %d connections to peer, got %d", 1, count)
66+
}
67+
}
68+
69+
err := echos[0].Host.Connect(context.Background(), peer.AddrInfo{ID: echos[4].Host.ID()})
70+
if err == nil {
71+
t.Fatal("expected ResourceManager to block incoming connection")
72+
}
73+
}
74+
75+
func TestResourceManagerServiceInbound(t *testing.T) {
76+
// this test checks that we can not exceed the inbound stream limit at service level
77+
// we specify: 3 streams for the service, and we try to create 4 streams
78+
limiter := rcmgr.NewFixedLimiter(1 << 30)
79+
limiter.DefaultServiceLimits = limiter.DefaultServiceLimits.
80+
WithStreamLimit(3, 1024)
81+
echos := createEchos(t, 5, libp2p.ResourceManager(rcmgr.NewResourceManager(limiter)))
82+
83+
echos[0].WaitBeforeRead = func() error {
84+
time.Sleep(100 * time.Millisecond)
85+
return nil
86+
}
87+
88+
for i := 1; i < 5; i++ {
89+
err := echos[i].Host.Connect(context.Background(), peer.AddrInfo{ID: echos[0].Host.ID()})
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
}
94+
95+
var wg sync.WaitGroup
96+
for i := 1; i < 5; i++ {
97+
wg.Add(1)
98+
go func(i int) {
99+
defer wg.Done()
100+
101+
err := echos[i].Echo(echos[0].Host.ID(), "hello libp2p")
102+
if err != nil {
103+
t.Log(err)
104+
}
105+
}(i)
106+
}
107+
wg.Wait()
108+
109+
checkEchoStatus(t, echos[0], EchoStatus{
110+
StreamsIn: 4,
111+
EchosIn: 3,
112+
EchosOut: 3,
113+
ResourceServiceErrors: 1,
114+
})
115+
}
116+
117+
func TestResourceManagerServicePeerInbound(t *testing.T) {
118+
// this test checks that we cannot exceed the per peer inbound stream limit at service level
119+
// we specify: 2 streams per peer for echo, and we try to create 3 streams
120+
limiter := rcmgr.NewFixedLimiter(1 << 30)
121+
limiter.ServicePeerLimits = map[string]rcmgr.Limit{
122+
EchoService: limiter.DefaultPeerLimits.WithStreamLimit(2, 1024),
123+
}
124+
echos := createEchos(t, 5, libp2p.ResourceManager(rcmgr.NewResourceManager(limiter)))
125+
126+
echos[0].WaitBeforeRead = func() error {
127+
time.Sleep(100 * time.Millisecond)
128+
return nil
129+
}
130+
131+
for i := 1; i < 5; i++ {
132+
err := echos[i].Host.Connect(context.Background(), peer.AddrInfo{ID: echos[0].Host.ID()})
133+
if err != nil {
134+
t.Fatal(err)
135+
}
136+
}
137+
138+
var wg sync.WaitGroup
139+
for i := 1; i < 5; i++ {
140+
wg.Add(1)
141+
go func(i int) {
142+
defer wg.Done()
143+
144+
err := echos[i].Echo(echos[0].Host.ID(), "hello libp2p")
145+
if err != nil {
146+
t.Log(err)
147+
}
148+
}(i)
149+
}
150+
wg.Wait()
151+
152+
checkEchoStatus(t, echos[0], EchoStatus{
153+
StreamsIn: 4,
154+
EchosIn: 4,
155+
EchosOut: 4,
156+
ResourceServiceErrors: 0,
157+
})
158+
159+
for i := 0; i < 3; i++ {
160+
wg.Add(1)
161+
go func() {
162+
defer wg.Done()
163+
164+
err := echos[2].Echo(echos[0].Host.ID(), "hello libp2p")
165+
if err != nil {
166+
t.Log(err)
167+
}
168+
}()
169+
}
170+
wg.Wait()
171+
172+
checkEchoStatus(t, echos[0], EchoStatus{
173+
StreamsIn: 7,
174+
EchosIn: 6,
175+
EchosOut: 6,
176+
ResourceServiceErrors: 1,
177+
})
178+
}

0 commit comments

Comments
 (0)