1
0

runtime_test.go 653 B

123456789101112131415161718192021222324252627282930313233343536
  1. package prof
  2. import (
  3. "strings"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestDisplayStats(t *testing.T) {
  10. writer := &threadSafeBuffer{
  11. buf: strings.Builder{},
  12. }
  13. displayStatsWithWriter(writer, time.Millisecond*10)
  14. time.Sleep(time.Millisecond * 50)
  15. assert.Contains(t, writer.String(), "Goroutines: ")
  16. }
  17. type threadSafeBuffer struct {
  18. buf strings.Builder
  19. lock sync.Mutex
  20. }
  21. func (b *threadSafeBuffer) String() string {
  22. b.lock.Lock()
  23. defer b.lock.Unlock()
  24. return b.buf.String()
  25. }
  26. func (b *threadSafeBuffer) Write(p []byte) (n int, err error) {
  27. b.lock.Lock()
  28. defer b.lock.Unlock()
  29. return b.buf.Write(p)
  30. }