cosmos-sdk/store/internal/conv/string_test.go
Hwangjae Lee 56775c81d0
fix(tests): resolve timer resource leaks causing "too many concurrent timer firings" panic (#25102)
Signed-off-by: Hwangjae Lee <meetrick@gmail.com>
Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io>
2025-08-06 15:43:59 +00:00

59 lines
1.2 KiB
Go

package conv
import (
"runtime"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
func TestStringSuite(t *testing.T) {
suite.Run(t, new(StringSuite))
}
type StringSuite struct{ suite.Suite }
func unsafeConvertStr() []byte {
return UnsafeStrToBytes("abc")
}
func (s *StringSuite) TestUnsafeStrToBytes() {
// we convert in other function to trigger GC. We want to check that
// the underlying array in []bytes is accessible after GC will finish swapping.
for i := 0; i < 5; i++ {
b := unsafeConvertStr()
runtime.GC()
timer := time.NewTimer(2 * time.Millisecond)
<-timer.C
timer.Stop()
b2 := append(b, 'd')
s.Equal("abc", string(b))
s.Equal("abcd", string(b2))
}
}
func unsafeConvertBytes() string {
return UnsafeBytesToStr([]byte("abc"))
}
func (s *StringSuite) TestUnsafeBytesToStr() {
// we convert in other function to trigger GC. We want to check that
// the underlying array in []bytes is accessible after GC will finish swapping.
for i := 0; i < 5; i++ {
str := unsafeConvertBytes()
runtime.GC()
timer := time.NewTimer(2 * time.Millisecond)
<-timer.C
timer.Stop()
s.Equal("abc", str)
}
}
func BenchmarkUnsafeStrToBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
UnsafeStrToBytes(strconv.Itoa(i))
}
}