package timing

import (
	"context"
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestTiming(t *testing.T) {
	ctx := NewContext(context.Background(), NewHeader())
	h := ctx.Value(ctxKey).(*Header)

	f1 := Start(ctx, "test1", "desc1")
	f2 := Start(ctx, "test1", "desc1")
	f3 := Start(ctx, "test1", "desc1")
	time.Sleep(time.Millisecond * 100)
	f1.End()
	f2.End()
	f3.End()
	assert.Len(t, h.metrics["test1;desc=desc1"], 3)
	assert.True(t, strings.HasPrefix(h.String(), "test1;desc=desc1 x3;dur="))

	Start(ctx, "test2")
	Start(ctx, "test3", "desc3")
	Start(ctx, "test3", "desc3")
	assert.Len(t, h.metrics, 3)
	assert.Len(t, h.metrics["test2"], 1)
	assert.Len(t, h.metrics["test3;desc=desc3"], 2)
}
