github.com/m3db/m3@v1.5.0/src/metrics/aggregation/id_compress_test.go (about) 1 // Copyright (c) 2017 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 aggregation 22 23 import ( 24 "testing" 25 26 "github.com/m3db/m3/src/x/pool" 27 28 "github.com/stretchr/testify/require" 29 ) 30 31 func TestIDCompressRoundTrip(t *testing.T) { 32 testcases := []struct { 33 input Types 34 result Types 35 expectErr bool 36 }{ 37 {DefaultTypes, DefaultTypes, false}, 38 {[]Type{UnknownType}, DefaultTypes, true}, 39 {[]Type{Min, Max}, []Type{Min, Max}, false}, 40 {[]Type{Last}, []Type{Last}, false}, 41 {[]Type{P999, P9999}, []Type{P999, P9999}, false}, 42 {[]Type{1, 5, 9, 3, 2}, []Type{1, 2, 3, 5, 9}, false}, 43 // 50 is an unknown aggregation type. 44 {[]Type{10, 50}, DefaultTypes, true}, 45 } 46 47 p := NewTypesPool(pool.NewObjectPoolOptions().SetSize(1)) 48 p.Init(func() Types { 49 return make(Types, 0, maxTypeID) 50 }) 51 compressor, decompressor := NewIDCompressor(), NewPooledIDDecompressor(p) 52 for _, test := range testcases { 53 codes, err := compressor.Compress(test.input) 54 if test.expectErr { 55 require.Error(t, err) 56 continue 57 } 58 res, err := decompressor.Decompress(codes) 59 require.NoError(t, err) 60 require.Equal(t, test.result, res) 61 } 62 } 63 64 func TestIDDecompressError(t *testing.T) { 65 compressor, decompressor := NewIDCompressor(), NewIDDecompressor() 66 _, err := decompressor.Decompress([IDLen]uint64{1}) 67 require.Error(t, err) 68 69 max, err := compressor.Compress( 70 []Type{Last, Min, Max, Mean, Median, Count, Sum, SumSq, Stdev, P95, P99, P999, P9999, P25, P75}) 71 require.NoError(t, err) 72 73 max[0] = max[0] << 1 74 _, err = decompressor.Decompress(max) 75 require.Error(t, err) 76 } 77 78 func TestIDMustDecompress(t *testing.T) { 79 compressor, decompressor := NewIDCompressor(), NewIDDecompressor() 80 inputs := []struct { 81 id ID 82 shouldPanic bool 83 }{ 84 { 85 id: compressor.MustCompress([]Type{Last, Min, Max, Mean, Median, Count, Sum, SumSq}), 86 shouldPanic: false, 87 }, 88 { 89 id: [IDLen]uint64{1}, 90 shouldPanic: true, 91 }, 92 } 93 94 for _, input := range inputs { 95 if input.shouldPanic { 96 require.Panics(t, func() { decompressor.MustDecompress(input.id) }) 97 } else { 98 require.NotPanics(t, func() { decompressor.MustDecompress(input.id) }) 99 } 100 } 101 102 }