github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/stores/tsdb/head_wal_test.go (about)

     1  package tsdb
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/go-kit/log"
     8  	"github.com/prometheus/prometheus/tsdb/chunks"
     9  	"github.com/prometheus/prometheus/tsdb/record"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/grafana/loki/pkg/storage/stores/tsdb/index"
    13  )
    14  
    15  func Test_Encoding_Series(t *testing.T) {
    16  	record := &WALRecord{
    17  		UserID: "foo",
    18  		Series: record.RefSeries{
    19  			Ref:    chunks.HeadSeriesRef(1),
    20  			Labels: mustParseLabels(`{foo="bar"}`),
    21  		},
    22  	}
    23  	buf := record.encodeSeries(nil)
    24  	decoded := &WALRecord{}
    25  
    26  	err := decodeWALRecord(buf, decoded)
    27  	require.Nil(t, err)
    28  	require.Equal(t, record, decoded)
    29  }
    30  
    31  func Test_Encoding_Chunks(t *testing.T) {
    32  	record := &WALRecord{
    33  		UserID: "foo",
    34  		Chks: ChunkMetasRecord{
    35  			Ref: 1,
    36  			Chks: index.ChunkMetas{
    37  				{
    38  					Checksum: 1,
    39  					MinTime:  1,
    40  					MaxTime:  4,
    41  					KB:       5,
    42  					Entries:  6,
    43  				},
    44  				{
    45  					Checksum: 2,
    46  					MinTime:  5,
    47  					MaxTime:  10,
    48  					KB:       7,
    49  					Entries:  8,
    50  				},
    51  			},
    52  		},
    53  	}
    54  	buf := record.encodeChunks(nil)
    55  	decoded := &WALRecord{}
    56  
    57  	err := decodeWALRecord(buf, decoded)
    58  	require.Nil(t, err)
    59  	require.Equal(t, record, decoded)
    60  }
    61  
    62  func Test_HeadWALLog(t *testing.T) {
    63  	dir := t.TempDir()
    64  	w, err := newHeadWAL(log.NewNopLogger(), dir, time.Now())
    65  	require.Nil(t, err)
    66  
    67  	newSeries := &WALRecord{
    68  		UserID: "foo",
    69  		Series: record.RefSeries{Ref: 1, Labels: mustParseLabels(`{foo="bar"}`)},
    70  		Chks: ChunkMetasRecord{
    71  			Chks: []index.ChunkMeta{
    72  				{
    73  					Checksum: 1,
    74  					MinTime:  1,
    75  					MaxTime:  10,
    76  					KB:       5,
    77  					Entries:  50,
    78  				},
    79  			},
    80  			Ref: 1,
    81  		},
    82  	}
    83  	require.Nil(t, w.Log(newSeries))
    84  
    85  	chunksOnly := &WALRecord{
    86  		UserID: "foo",
    87  		Chks: ChunkMetasRecord{
    88  			Chks: []index.ChunkMeta{
    89  				{
    90  					Checksum: 2,
    91  					MinTime:  5,
    92  					MaxTime:  100,
    93  					KB:       3,
    94  					Entries:  25,
    95  				},
    96  			},
    97  			Ref: 1,
    98  		},
    99  	}
   100  	require.Nil(t, w.Log(chunksOnly))
   101  	require.Nil(t, w.Stop())
   102  }