github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/ingester/client/client_test.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"net/http/httptest"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/cortexproject/cortex/pkg/cortexpb"
    12  	"github.com/cortexproject/cortex/pkg/util"
    13  )
    14  
    15  // TestMarshall is useful to try out various optimisation on the unmarshalling code.
    16  func TestMarshall(t *testing.T) {
    17  	const numSeries = 10
    18  	recorder := httptest.NewRecorder()
    19  	{
    20  		req := cortexpb.WriteRequest{}
    21  		for i := 0; i < numSeries; i++ {
    22  			req.Timeseries = append(req.Timeseries, cortexpb.PreallocTimeseries{
    23  				TimeSeries: &cortexpb.TimeSeries{
    24  					Labels: []cortexpb.LabelAdapter{
    25  						{Name: "foo", Value: strconv.Itoa(i)},
    26  					},
    27  					Samples: []cortexpb.Sample{
    28  						{TimestampMs: int64(i), Value: float64(i)},
    29  					},
    30  				},
    31  			})
    32  		}
    33  		err := util.SerializeProtoResponse(recorder, &req, util.RawSnappy)
    34  		require.NoError(t, err)
    35  	}
    36  
    37  	{
    38  		const (
    39  			tooSmallSize = 1
    40  			plentySize   = 1024 * 1024
    41  		)
    42  		req := cortexpb.WriteRequest{}
    43  		err := util.ParseProtoReader(context.Background(), recorder.Body, recorder.Body.Len(), tooSmallSize, &req, util.RawSnappy)
    44  		require.Error(t, err)
    45  		err = util.ParseProtoReader(context.Background(), recorder.Body, recorder.Body.Len(), plentySize, &req, util.RawSnappy)
    46  		require.NoError(t, err)
    47  		require.Equal(t, numSeries, len(req.Timeseries))
    48  	}
    49  }