@cryptotaxi247 / kubo / commits / 55b94751c

test: replace `go-clock` with `testing/synctest` (#11131)

Use testing/synctest instead of go-clock for artificial time control.

Andrew Gillis committed Jan 7, 2026 at 13:30 UTC 55b94751cc068939092be360446cf52f95c44d93
4 files changed +42 -45
core/node/libp2p/rcmgr.go
-2
@@ -12,7 +12,6 @@ import (
12 "github.com/ipfs/kubo/core/node/helpers"
13 "github.com/ipfs/kubo/repo"
14
15 - "github.com/filecoin-project/go-clock"
15 logging "github.com/ipfs/go-log/v2"
16 "github.com/libp2p/go-libp2p"
17 "github.com/libp2p/go-libp2p/core/network"
@@ -112,7 +111,6 @@ filled in with autocomputed defaults.`)
111 return nil, opts, fmt.Errorf("creating libp2p resource manager: %w", err)
112 }
113 lrm := &loggingResourceManager{
115 - clock: clock.New(),
114 logger: &logging.Logger("resourcemanager").SugaredLogger,
115 delegate: manager,
116 }
core/node/libp2p/rcmgr_logging.go
+1 -3
@@ -7,7 +7,6 @@ import (
7 "sync"
8 "time"
9
10 - "github.com/filecoin-project/go-clock"
10 "github.com/libp2p/go-libp2p/core/network"
11 "github.com/libp2p/go-libp2p/core/peer"
12 "github.com/libp2p/go-libp2p/core/protocol"
@@ -17,7 +16,6 @@ import (
16 )
17
18 type loggingResourceManager struct {
20 - clock clock.Clock
19 logger *zap.SugaredLogger
20 delegate network.ResourceManager
21 logInterval time.Duration
@@ -42,7 +40,7 @@ func (n *loggingResourceManager) start(ctx context.Context) {
40 if logInterval == 0 {
41 logInterval = 10 * time.Second
42 }
45 - ticker := n.clock.Ticker(logInterval)
43 + ticker := time.NewTicker(logInterval)
44 go func() {
45 defer ticker.Stop()
46 for {
core/node/libp2p/rcmgr_logging_test.go
+40 -39
@@ -2,9 +2,9 @@ package libp2p
2
3 import (
4 "testing"
5 + "testing/synctest"
6 "time"
7
7 - "github.com/filecoin-project/go-clock"
8 "github.com/libp2p/go-libp2p/core/network"
9 rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
10 ma "github.com/multiformats/go-multiaddr"
@@ -14,48 +14,49 @@ import (
14 )
15
16 func TestLoggingResourceManager(t *testing.T) {
17 - clock := clock.NewMock()
18 - orig := rcmgr.DefaultLimits.AutoScale()
19 - limits := orig.ToPartialLimitConfig()
20 - limits.System.Conns = 1
21 - limits.System.ConnsInbound = 1
22 - limits.System.ConnsOutbound = 1
23 - limiter := rcmgr.NewFixedLimiter(limits.Build(orig))
24 - rm, err := rcmgr.NewResourceManager(limiter)
25 - if err != nil {
26 - t.Fatal(err)
27 - }
17 + synctest.Test(t, func(t *testing.T) {
18 + orig := rcmgr.DefaultLimits.AutoScale()
19 + limits := orig.ToPartialLimitConfig()
20 + limits.System.Conns = 1
21 + limits.System.ConnsInbound = 1
22 + limits.System.ConnsOutbound = 1
23 + limiter := rcmgr.NewFixedLimiter(limits.Build(orig))
24 + rm, err := rcmgr.NewResourceManager(limiter)
25 + if err != nil {
26 + t.Fatal(err)
27 + }
28 + defer rm.Close()
29
29 - oCore, oLogs := observer.New(zap.WarnLevel)
30 - oLogger := zap.New(oCore)
31 - lrm := &loggingResourceManager{
32 - clock: clock,
33 - logger: oLogger.Sugar(),
34 - delegate: rm,
35 - logInterval: 1 * time.Second,
36 - }
30 + oCore, oLogs := observer.New(zap.WarnLevel)
31 + oLogger := zap.New(oCore)
32 + lrm := &loggingResourceManager{
33 + logger: oLogger.Sugar(),
34 + delegate: rm,
35 + logInterval: 1 * time.Second,
36 + }
37
38 - // 2 of these should result in resource limit exceeded errors and subsequent log messages
39 - for i := 0; i < 3; i++ {
40 - _, _ = lrm.OpenConnection(network.DirInbound, false, ma.StringCast("/ip4/127.0.0.1/tcp/1234"))
41 - }
38 + // 2 of these should result in resource limit exceeded errors and subsequent log messages
39 + for i := 0; i < 3; i++ {
40 + _, _ = lrm.OpenConnection(network.DirInbound, false, ma.StringCast("/ip4/127.0.0.1/tcp/1234"))
41 + }
42
43 - // run the logger which will write an entry for those errors
44 - ctx := t.Context()
45 - lrm.start(ctx)
46 - clock.Add(3 * time.Second)
43 + // run the logger which will write an entry for those errors
44 + ctx := t.Context()
45 + lrm.start(ctx)
46 + time.Sleep(3 * time.Second)
47
48 - timer := time.NewTimer(1 * time.Second)
49 - for {
50 - select {
51 - case <-timer.C:
52 - t.Fatalf("expected logs never arrived")
53 - default:
54 - if oLogs.Len() == 0 {
55 - continue
48 + timer := time.NewTimer(1 * time.Second)
49 + for {
50 + select {
51 + case <-timer.C:
52 + t.Fatalf("expected logs never arrived")
53 + default:
54 + if oLogs.Len() == 0 {
55 + continue
56 + }
57 + require.Equal(t, "Protected from exceeding resource limits 2 times. libp2p message: \"system: cannot reserve inbound connection: resource limit exceeded\".", oLogs.All()[0].Message)
58 + return
59 }
57 - require.Equal(t, "Protected from exceeding resource limits 2 times. libp2p message: \"system: cannot reserve inbound connection: resource limit exceeded\".", oLogs.All()[0].Message)
58 - return
60 }
60 - }
61 + })
62 }
go.mod
+1 -1
@@ -16,7 +16,6 @@ require (
16 github.com/dustin/go-humanize v1.0.1
17 github.com/elgris/jsondiff v0.0.0-20160530203242-765b5c24c302
18 github.com/facebookgo/atomicfile v0.0.0-20151019160806-2de1f203e7d5
19 - github.com/filecoin-project/go-clock v0.1.0
19 github.com/fsnotify/fsnotify v1.9.0
20 github.com/google/uuid v1.6.0
21 github.com/hashicorp/go-version v1.7.0
@@ -126,6 +125,7 @@ require (
125 github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
126 github.com/fatih/color v1.15.0 // indirect
127 github.com/felixge/httpsnoop v1.0.4 // indirect
128 + github.com/filecoin-project/go-clock v0.1.0 // indirect
129 github.com/flynn/noise v1.1.0 // indirect
130 github.com/gabriel-vasile/mimetype v1.4.10 // indirect
131 github.com/gammazero/chanqueue v1.1.1 // indirect