github.com/m3db/m3@v1.5.0/src/metrics/encoding/protobuf/aggregated_roundtrip_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package protobuf
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/metrics/metric"
    27  	"github.com/m3db/m3/src/metrics/metric/aggregated"
    28  	"github.com/m3db/m3/src/metrics/policy"
    29  	"github.com/m3db/m3/src/x/pool"
    30  
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  var (
    35  	testAggregatedMetric1 = aggregated.MetricWithStoragePolicy{
    36  		Metric: aggregated.Metric{
    37  			Type:      metric.CounterType,
    38  			ID:        []byte("foo"),
    39  			TimeNanos: 1234,
    40  			Value:     100,
    41  		},
    42  		StoragePolicy: policy.MustParseStoragePolicy("10s:2d"),
    43  	}
    44  	testAggregatedMetric2 = aggregated.MetricWithStoragePolicy{
    45  		Metric: aggregated.Metric{
    46  			Type:  metric.CounterType,
    47  			ID:    []byte("bar"),
    48  			Value: 200,
    49  		},
    50  		StoragePolicy: policy.MustParseStoragePolicy("1m:2d"),
    51  	}
    52  )
    53  
    54  func TestAggregatedEncoderDecoder_RoundTrip(t *testing.T) {
    55  	enc := NewAggregatedEncoder(nil)
    56  	dec := NewAggregatedDecoder(nil)
    57  	require.NoError(t, enc.Encode(testAggregatedMetric1, 2000))
    58  	require.NoError(t, dec.Decode(enc.Buffer().Bytes()))
    59  	require.Equal(t, int64(2000), dec.EncodeNanos())
    60  	sp := dec.StoragePolicy()
    61  	require.Equal(t, testAggregatedMetric1.StoragePolicy, sp)
    62  	require.Equal(t, string(testAggregatedMetric1.ID), string(dec.ID()))
    63  	require.Equal(t, testAggregatedMetric1.TimeNanos, dec.TimeNanos())
    64  	require.Equal(t, testAggregatedMetric1.Value, dec.Value())
    65  }
    66  
    67  func TestAggregatedEncoderDecoder_WithBytesPool(t *testing.T) {
    68  	buckets := []pool.Bucket{
    69  		// Use a capacity way larger than the metric size.
    70  		{Capacity: 2048, Count: 1},
    71  	}
    72  	p := pool.NewBytesPool(buckets, nil)
    73  	p.Init()
    74  	enc := NewAggregatedEncoder(p)
    75  	dec := NewAggregatedDecoder(nil)
    76  	require.NoError(t, enc.Encode(testAggregatedMetric1, 2000))
    77  	require.NoError(t, dec.Decode(enc.Buffer().Bytes()))
    78  	require.Equal(t, int64(2000), dec.EncodeNanos())
    79  	sp := dec.StoragePolicy()
    80  	require.Equal(t, testAggregatedMetric1.StoragePolicy, sp)
    81  	require.Equal(t, string(testAggregatedMetric1.ID), string(dec.ID()))
    82  	require.Equal(t, testAggregatedMetric1.TimeNanos, dec.TimeNanos())
    83  	require.Equal(t, testAggregatedMetric1.Value, dec.Value())
    84  }
    85  
    86  func TestAggregatedEncoderDecoder_ResetProtobuf(t *testing.T) {
    87  	enc := NewAggregatedEncoder(nil)
    88  	dec := NewAggregatedDecoder(nil)
    89  	require.NoError(t, enc.Encode(testAggregatedMetric1, 2000))
    90  	require.NoError(t, dec.Decode(enc.Buffer().Bytes()))
    91  	require.Equal(t, int64(2000), dec.EncodeNanos())
    92  	sp := dec.StoragePolicy()
    93  	require.Equal(t, testAggregatedMetric1.StoragePolicy, sp)
    94  	require.Equal(t, string(testAggregatedMetric1.ID), string(dec.ID()))
    95  	require.Equal(t, testAggregatedMetric1.TimeNanos, dec.TimeNanos())
    96  	require.Equal(t, testAggregatedMetric1.Value, dec.Value())
    97  
    98  	// Must close the decoder to reset all the fields in the protobuf object
    99  	// to avoid previously decoded value being applied in the next decoding.
   100  	dec.Close()
   101  
   102  	require.NoError(t, enc.Encode(testAggregatedMetric2, 3000))
   103  	require.NoError(t, dec.Decode(enc.Buffer().Bytes()))
   104  	require.Equal(t, int64(3000), dec.EncodeNanos())
   105  	sp = dec.StoragePolicy()
   106  	require.Equal(t, testAggregatedMetric2.StoragePolicy, sp)
   107  	require.Equal(t, string(testAggregatedMetric2.ID), string(dec.ID()))
   108  	require.Equal(t, testAggregatedMetric2.TimeNanos, dec.TimeNanos())
   109  	require.Equal(t, testAggregatedMetric2.Value, dec.Value())
   110  }