Follow-up to https://github.com/coder/coder/pull/25601, where I missed the other way agents avoid `*_internal_test.go`: an `export_test.go` file that re-exports unexported symbols, just so a `package foo_test` test can reach them. There were 13 of these on `main`, most recently from #32063. This PR removes 8 of them. Tests of package internals are now `*_internal_test.go` files, with no changes beyond the package name and the symbols they call. `messagepartbuffer` and `agentcontext` only needed one small accessor each, so `Buffer.EpisodeCount` and `Manager.Started` are just exported for real, and their tests stay where they were. To stop new ones appearing, the `testpackage` linter no longer skips `export_test.go` (the 5 that are left are excluded by path), and `AGENTS.md` and `.claude/docs/TESTING.md` now state the rule. `export_test.go` is an agent idiom, and imo we should try and get rid of it. So, once this PR is approved, we'll fix up all the remaining ones in a follow-up PR, somehow, and undo the lint exclusion when we do.
123 lines
2.8 KiB
Go
123 lines
2.8 KiB
Go
package testutil
|
|
|
|
import (
|
|
"math"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/pprof/profile"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestStartMemoryGuard(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("Exceeded", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
exceeded := make(chan uint64, 1)
|
|
stop := watchMemory(1, func(total uint64) {
|
|
exceeded <- total
|
|
})
|
|
defer stop()
|
|
|
|
select {
|
|
case total := <-exceeded:
|
|
require.NotZero(t, total)
|
|
case <-time.After(WaitShort):
|
|
t.Fatal("memory guard did not trip")
|
|
}
|
|
})
|
|
|
|
t.Run("Stops", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
stop := watchMemory(math.MaxUint64, func(uint64) {
|
|
t.Error("memory guard tripped below the limit")
|
|
})
|
|
stopped := make(chan struct{})
|
|
go func() {
|
|
defer close(stopped)
|
|
stop()
|
|
}()
|
|
select {
|
|
case <-stopped:
|
|
case <-time.After(WaitShort):
|
|
t.Fatal("memory guard did not stop")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestWriteHeapProfile(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
dir := t.TempDir()
|
|
path, err := writeHeapProfile(dir, "")
|
|
require.NoError(t, err)
|
|
info, err := os.Stat(path)
|
|
require.NoError(t, err)
|
|
require.NotZero(t, info.Size())
|
|
}
|
|
|
|
func TestWriteHeapProfiles(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Two collections publish everything allocated so far. The burst
|
|
// below then sits in an unpublished profile cycle, as it would when
|
|
// the guard trips, until the forced collection in WriteHeapProfiles
|
|
// publishes it. The allocation is large enough that the profiler
|
|
// cannot skip it at the default sampling rate.
|
|
runtime.GC() //nolint:revive // Two collections publish everything allocated so far.
|
|
runtime.GC() //nolint:revive // See above.
|
|
burst := allocateBurst()
|
|
|
|
immediate, collected, err := writeHeapProfiles(t.TempDir(), WaitLong)
|
|
require.NoError(t, err)
|
|
require.FileExists(t, immediate)
|
|
require.NotEmpty(t, collected, "collection did not finish")
|
|
require.True(t, allocatedIn(t, collected, "allocateBurst"),
|
|
"profile after collection misses the burst")
|
|
runtime.KeepAlive(burst)
|
|
}
|
|
|
|
//go:noinline
|
|
func allocateBurst() []byte {
|
|
return make([]byte, 32<<20)
|
|
}
|
|
|
|
// allocatedIn reports whether the heap profile at path attributes
|
|
// allocated space to a function with the given name.
|
|
func allocatedIn(t *testing.T, path, function string) bool {
|
|
t.Helper()
|
|
f, err := os.Open(path)
|
|
require.NoError(t, err)
|
|
defer f.Close()
|
|
prof, err := profile.Parse(f)
|
|
require.NoError(t, err)
|
|
|
|
allocSpace := -1
|
|
for i, st := range prof.SampleType {
|
|
if st.Type == "alloc_space" {
|
|
allocSpace = i
|
|
}
|
|
}
|
|
require.NotEqual(t, -1, allocSpace, "profile has no alloc_space samples")
|
|
|
|
for _, s := range prof.Sample {
|
|
if s.Value[allocSpace] == 0 {
|
|
continue
|
|
}
|
|
for _, loc := range s.Location {
|
|
for _, line := range loc.Line {
|
|
if strings.HasSuffix(line.Function.Name, "."+function) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|