You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
653 B
Go
37 lines
653 B
Go
2 years ago
|
package prof
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
"sync"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestDisplayStats(t *testing.T) {
|
||
|
writer := &threadSafeBuffer{
|
||
|
buf: strings.Builder{},
|
||
|
}
|
||
|
displayStatsWithWriter(writer, time.Millisecond*10)
|
||
|
time.Sleep(time.Millisecond * 50)
|
||
|
assert.Contains(t, writer.String(), "Goroutines: ")
|
||
|
}
|
||
|
|
||
|
type threadSafeBuffer struct {
|
||
|
buf strings.Builder
|
||
|
lock sync.Mutex
|
||
|
}
|
||
|
|
||
|
func (b *threadSafeBuffer) String() string {
|
||
|
b.lock.Lock()
|
||
|
defer b.lock.Unlock()
|
||
|
return b.buf.String()
|
||
|
}
|
||
|
|
||
|
func (b *threadSafeBuffer) Write(p []byte) (n int, err error) {
|
||
|
b.lock.Lock()
|
||
|
defer b.lock.Unlock()
|
||
|
return b.buf.Write(p)
|
||
|
}
|