github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/promutils/labeled/stopwatch_test.go (about)

     1  package labeled
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/lyft/flytestdlib/contextutils"
     9  	"github.com/lyft/flytestdlib/promutils"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestLabeledStopWatch(t *testing.T) {
    14  	UnsetMetricKeys()
    15  	assert.NotPanics(t, func() {
    16  		SetMetricKeys(contextutils.ProjectKey, contextutils.DomainKey, contextutils.WorkflowIDKey, contextutils.TaskIDKey)
    17  	})
    18  
    19  	t.Run("always labeled", func(t *testing.T) {
    20  		scope := promutils.NewTestScope()
    21  		c := NewStopWatch("lbl_counter", "help", time.Second, scope)
    22  		assert.NotNil(t, c)
    23  		ctx := context.TODO()
    24  		w := c.Start(ctx)
    25  		w.Stop()
    26  
    27  		ctx = contextutils.WithProjectDomain(ctx, "project", "domain")
    28  		w = c.Start(ctx)
    29  		w.Stop()
    30  
    31  		ctx = contextutils.WithTaskID(ctx, "task")
    32  		w = c.Start(ctx)
    33  		w.Stop()
    34  
    35  		c.Observe(ctx, time.Now(), time.Now().Add(time.Second))
    36  		c.Time(ctx, func() {
    37  			// Do nothing
    38  		})
    39  	})
    40  
    41  	t.Run("unlabeled", func(t *testing.T) {
    42  		scope := promutils.NewTestScope()
    43  		c := NewStopWatch("lbl_counter_2", "help", time.Second, scope, EmitUnlabeledMetric)
    44  		assert.NotNil(t, c)
    45  
    46  		c.Start(context.TODO())
    47  	})
    48  }