github.com/msales/pkg/v3@v3.24.0/stats/heartbeat_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 TestHeartbeat(t *testing.T) {
    13  	m := new(MockStats)
    14  	m.On("Inc", "heartbeat", int64(1), float32(1.0), mock.Anything).Return(nil)
    15  	stats.DefaultHeartbeatInterval = time.Millisecond
    16  
    17  	go stats.Heartbeat(m)
    18  
    19  	time.Sleep(100 * time.Millisecond)
    20  
    21  	m.AssertCalled(t, "Inc", "heartbeat", int64(1), float32(1.0), mock.Anything)
    22  }
    23  
    24  func TestHeartbeatFromContext(t *testing.T) {
    25  	m := new(MockStats)
    26  	m.On("Inc", "heartbeat", int64(1), float32(1.0), mock.Anything).Return(nil)
    27  	ctx := stats.WithStats(context.Background(), m)
    28  
    29  	go stats.HeartbeatFromContext(ctx, time.Millisecond)
    30  
    31  	time.Sleep(100 * time.Millisecond)
    32  
    33  	m.AssertCalled(t, "Inc", "heartbeat", int64(1), float32(1.0), mock.Anything)
    34  }