Skip to content

Commit

Permalink
support rcmgr tracing in itests
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Jan 16, 2022
1 parent 8130967 commit 0de38ce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions itest/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"github.com/stretchr/testify/require"
)

func createEchos(t *testing.T, count int, makeOpts ...func() libp2p.Option) []*Echo {
func createEchos(t *testing.T, count int, makeOpts ...func(int) libp2p.Option) []*Echo {
result := make([]*Echo, 0, count)

for i := 0; i < count; i++ {
opts := make([]libp2p.Option, 0, len(makeOpts))
for _, makeOpt := range makeOpts {
opts = append(opts, makeOpt())
opts = append(opts, makeOpt(i))
}

h, err := libp2p.New(opts...)
Expand Down
31 changes: 24 additions & 7 deletions itest/rcmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package itest
import (
"context"
"fmt"
"os"
"sync"
"sync/atomic"
"testing"
Expand All @@ -14,25 +15,38 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
)

func makeRcmgrOption(t *testing.T, limiter *rcmgr.BasicLimiter) func() libp2p.Option {
return func() libp2p.Option {
mgr, err := rcmgr.NewResourceManager(limiter)
func makeRcmgrOption(t *testing.T, limiter *rcmgr.BasicLimiter, test string) func(int) libp2p.Option {
return func(i int) libp2p.Option {
var opts []rcmgr.Option

if os.Getenv("LIBP2P_TEST_RCMGR_TRACE") == "1" {
opts = append(opts, rcmgr.WithTrace(fmt.Sprintf("%s-%d.json.gz", test, i)))
}

mgr, err := rcmgr.NewResourceManager(limiter, opts...)
if err != nil {
t.Fatal(err)
}
return libp2p.ResourceManager(mgr)
}
}

func closeRcmgrs(echos []*Echo) {
for _, e := range echos {
e.Host.Network().ResourceManager().Close()
}
}

func TestResourceManagerConnInbound(t *testing.T) {
// this test checks that we can not exceed the inbound conn limit at system level
// we specify: 1 conn per peer, 3 conns total, and we try to create 4 conns
limiter := rcmgr.NewDefaultLimiter()
limiter.SystemLimits = limiter.SystemLimits.WithConnLimit(3, 1024, 1024)
limiter.DefaultPeerLimits = limiter.DefaultPeerLimits.WithConnLimit(1, 16, 16)

echos := createEchos(t, 5, makeRcmgrOption(t, limiter))
echos := createEchos(t, 5, makeRcmgrOption(t, limiter, "TestResourceManagerConnInbound"))
defer closeEchos(echos)
defer closeRcmgrs(echos)

for i := 1; i < 4; i++ {
err := echos[i].Host.Connect(context.Background(), peer.AddrInfo{ID: echos[0].Host.ID()})
Expand Down Expand Up @@ -61,8 +75,9 @@ func TestResourceManagerConnOutbound(t *testing.T) {
limiter := rcmgr.NewDefaultLimiter()
limiter.SystemLimits = limiter.SystemLimits.WithConnLimit(1024, 3, 1024)
limiter.DefaultPeerLimits = limiter.DefaultPeerLimits.WithConnLimit(16, 1, 16)
echos := createEchos(t, 5, makeRcmgrOption(t, limiter))
echos := createEchos(t, 5, makeRcmgrOption(t, limiter, "TestResourceManagerConnOutbound"))
defer closeEchos(echos)
defer closeRcmgrs(echos)

for i := 1; i < 4; i++ {
err := echos[0].Host.Connect(context.Background(), peer.AddrInfo{ID: echos[i].Host.ID()})
Expand Down Expand Up @@ -90,8 +105,9 @@ func TestResourceManagerServiceInbound(t *testing.T) {
// we specify: 3 streams for the service, and we try to create 4 streams
limiter := rcmgr.NewDefaultLimiter()
limiter.DefaultServiceLimits = limiter.DefaultServiceLimits.WithStreamLimit(3, 1024, 1024)
echos := createEchos(t, 5, makeRcmgrOption(t, limiter))
echos := createEchos(t, 5, makeRcmgrOption(t, limiter, "TestResourceManagerServiceInbound"))
defer closeEchos(echos)
defer closeRcmgrs(echos)

ready := new(chan struct{})
echos[0].BeforeDone = waitForChannel(ready, time.Minute)
Expand Down Expand Up @@ -139,8 +155,9 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
limiter.ServicePeerLimits = map[string]rcmgr.Limit{
EchoService: limiter.DefaultPeerLimits.WithStreamLimit(2, 1024, 1024),
}
echos := createEchos(t, 5, makeRcmgrOption(t, limiter))
echos := createEchos(t, 5, makeRcmgrOption(t, limiter, "TestResourceManagerServicePeerInbound"))
defer closeEchos(echos)
defer closeRcmgrs(echos)

count := new(int32)
ready := new(chan struct{})
Expand Down

0 comments on commit 0de38ce

Please sign in to comment.