github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logproto/timeseries_test.go (about)

     1  package logproto
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestLabelAdapter_Marshal(t *testing.T) {
    11  	tests := []struct {
    12  		bs *LabelAdapter
    13  	}{
    14  		{&LabelAdapter{Name: "foo", Value: "bar"}},
    15  		{&LabelAdapter{Name: "very long label name", Value: "very long label value"}},
    16  		{&LabelAdapter{Name: "", Value: "foo"}},
    17  		{&LabelAdapter{}},
    18  	}
    19  	for _, tt := range tests {
    20  		t.Run(tt.bs.Name, func(t *testing.T) {
    21  			bytes, err := tt.bs.Marshal()
    22  			require.NoError(t, err)
    23  			lbs := &LabelAdapter{}
    24  			require.NoError(t, lbs.Unmarshal(bytes))
    25  			require.EqualValues(t, tt.bs, lbs)
    26  		})
    27  	}
    28  }
    29  
    30  func TestPreallocTimeseriesSliceFromPool(t *testing.T) {
    31  	t.Run("new instance is provided when not available to reuse", func(t *testing.T) {
    32  		first := PreallocTimeseriesSliceFromPool()
    33  		second := PreallocTimeseriesSliceFromPool()
    34  
    35  		assert.NotSame(t, first, second)
    36  	})
    37  
    38  	t.Run("instance is cleaned before reusing", func(t *testing.T) {
    39  		slice := PreallocTimeseriesSliceFromPool()
    40  		slice = append(slice, PreallocTimeseries{TimeSeries: &TimeSeries{}})
    41  		ReuseSlice(slice)
    42  
    43  		reused := PreallocTimeseriesSliceFromPool()
    44  		assert.Len(t, reused, 0)
    45  	})
    46  }
    47  
    48  func TestTimeseriesFromPool(t *testing.T) {
    49  	t.Run("new instance is provided when not available to reuse", func(t *testing.T) {
    50  		first := TimeseriesFromPool()
    51  		second := TimeseriesFromPool()
    52  
    53  		assert.NotSame(t, first, second)
    54  	})
    55  
    56  	t.Run("instance is cleaned before reusing", func(t *testing.T) {
    57  		ts := TimeseriesFromPool()
    58  		ts.Labels = []LabelAdapter{{Name: "foo", Value: "bar"}}
    59  		ts.Samples = []LegacySample{{Value: 1, TimestampMs: 2}}
    60  		ReuseTimeseries(ts)
    61  
    62  		reused := TimeseriesFromPool()
    63  		assert.Len(t, reused.Labels, 0)
    64  		assert.Len(t, reused.Samples, 0)
    65  	})
    66  }