Skip to content

Commit f688343

Browse files
authored
ethclient/simulated: add goroutine leak test (#31033)
Adds a basic sanity test case to catch any go-routines leaked from instantiation/closing of a simulated backend.
1 parent 7d8aca9 commit f688343

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

ethclient/simulated/backend_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"testing"
2626
"time"
2727

28+
"go.uber.org/goleak"
29+
2830
"github.com/ethereum/go-ethereum/crypto/kzg4844"
2931
"github.com/holiman/uint256"
3032

@@ -350,3 +352,20 @@ func TestAdjustTimeAfterFork(t *testing.T) {
350352
t.Errorf("failed to build block on fork")
351353
}
352354
}
355+
356+
func createAndCloseSimBackend() {
357+
genesisData := types.GenesisAlloc{}
358+
simulatedBackend := NewBackend(genesisData)
359+
defer simulatedBackend.Close()
360+
}
361+
362+
// TestCheckSimBackendGoroutineLeak checks whether creation of a simulated backend leaks go-routines. Any long-lived go-routines
363+
// spawned by global variables are not considered leaked.
364+
func TestCheckSimBackendGoroutineLeak(t *testing.T) {
365+
createAndCloseSimBackend()
366+
ignoreCur := goleak.IgnoreCurrent()
367+
// ignore this leveldb function: this go-routine is guaranteed to be terminated 1 second after closing db handle
368+
ignoreLdb := goleak.IgnoreAnyFunction("github.com/syndtr/goleveldb/leveldb.(*DB).mpoolDrain")
369+
createAndCloseSimBackend()
370+
goleak.VerifyNone(t, ignoreCur, ignoreLdb)
371+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ require (
6363
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
6464
github.com/urfave/cli/v2 v2.27.5
6565
go.uber.org/automaxprocs v1.5.2
66+
go.uber.org/goleak v1.3.0
6667
golang.org/x/crypto v0.32.0
6768
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
6869
golang.org/x/sync v0.10.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
528528
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
529529
go.uber.org/automaxprocs v1.5.2 h1:2LxUOGiR3O6tw8ui5sZa2LAaHnsviZdVOUZw4fvbnME=
530530
go.uber.org/automaxprocs v1.5.2/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
531+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
532+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
531533
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
532534
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
533535
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=

0 commit comments

Comments
 (0)