kubo/core/node/libp2p/rcmgr_logging_test.go
Marcin Rataj 6a008fc74c
refactor: apply go fix modernizers from Go 1.26 (#11190)
* chore: apply go fix modernizers from Go 1.26

automated refactoring: interface{} to any, slices.Contains,
and other idiomatic updates.

* feat(ci): add `go fix` check to Go analysis workflow

ensures Go 1.26 modernizers are applied, fails CI if `go fix ./...`
produces any changes (similar to existing `go fmt` enforcement)
2026-02-11 01:01:32 +01:00

63 lines
1.6 KiB
Go

package libp2p
import (
"testing"
"testing/synctest"
"time"
"github.com/libp2p/go-libp2p/core/network"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
)
func TestLoggingResourceManager(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
orig := rcmgr.DefaultLimits.AutoScale()
limits := orig.ToPartialLimitConfig()
limits.System.Conns = 1
limits.System.ConnsInbound = 1
limits.System.ConnsOutbound = 1
limiter := rcmgr.NewFixedLimiter(limits.Build(orig))
rm, err := rcmgr.NewResourceManager(limiter)
if err != nil {
t.Fatal(err)
}
defer rm.Close()
oCore, oLogs := observer.New(zap.WarnLevel)
oLogger := zap.New(oCore)
lrm := &loggingResourceManager{
logger: oLogger.Sugar(),
delegate: rm,
logInterval: 1 * time.Second,
}
// 2 of these should result in resource limit exceeded errors and subsequent log messages
for range 3 {
_, _ = lrm.OpenConnection(network.DirInbound, false, ma.StringCast("/ip4/127.0.0.1/tcp/1234"))
}
// run the logger which will write an entry for those errors
ctx := t.Context()
lrm.start(ctx)
time.Sleep(3 * time.Second)
timer := time.NewTimer(1 * time.Second)
for {
select {
case <-timer.C:
t.Fatalf("expected logs never arrived")
default:
if oLogs.Len() == 0 {
continue
}
require.Equal(t, "Protected from exceeding resource limits 2 times. libp2p message: \"system: cannot reserve inbound connection: resource limit exceeded\".", oLogs.All()[0].Message)
return
}
}
})
}