github.com/msales/pkg/v3@v3.24.0/stats/runtime_test.go (about) 1 package stats_test 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/msales/pkg/v3/stats" 9 "github.com/stretchr/testify/mock" 10 ) 11 12 func TestRuntime(t *testing.T) { 13 m := new(MockStats) 14 m.On("Gauge", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) 15 m.On("Timing", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) 16 stats.DefaultRuntimeInterval = time.Microsecond 17 18 go stats.Runtime(m) 19 20 time.Sleep(100 * time.Millisecond) 21 22 m.AssertCalled(t, "Gauge", "runtime.cpu.goroutines", mock.Anything, mock.Anything, mock.Anything) 23 } 24 25 func TestRuntimeFromContext(t *testing.T) { 26 m := new(MockStats) 27 m.On("Gauge", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) 28 m.On("Timing", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) 29 ctx := stats.WithStats(context.Background(), m) 30 31 go stats.RuntimeFromContext(ctx, time.Millisecond) 32 33 time.Sleep(100 * time.Millisecond) 34 35 m.AssertCalled(t, "Gauge", "runtime.cpu.goroutines", mock.Anything, mock.Anything, mock.Anything) 36 }