github.com/m3db/m3@v1.5.0/src/cmd/services/m3coordinator/downsample/options_test.go (about) 1 // Copyright (c) 2019 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 downsample 22 23 import ( 24 "fmt" 25 "testing" 26 "time" 27 28 "github.com/m3db/m3/src/dbnode/client" 29 "github.com/m3db/m3/src/metrics/aggregation" 30 "github.com/m3db/m3/src/metrics/policy" 31 "github.com/m3db/m3/src/query/storage/m3" 32 "github.com/m3db/m3/src/x/ident" 33 xtest "github.com/m3db/m3/src/x/test" 34 xtime "github.com/m3db/m3/src/x/time" 35 36 "github.com/stretchr/testify/require" 37 ) 38 39 func TestBufferForPastTimedMetric(t *testing.T) { 40 limits := defaultBufferPastLimits 41 tests := []struct { 42 value time.Duration 43 expected time.Duration 44 }{ 45 {value: -1 * time.Second, expected: 15 * time.Second}, 46 {value: 0, expected: 15 * time.Second}, 47 {value: 1 * time.Second, expected: 15 * time.Second}, 48 {value: 29 * time.Second, expected: 15 * time.Second}, 49 {value: 30 * time.Second, expected: 30 * time.Second}, 50 {value: 59 * time.Second, expected: 30 * time.Second}, 51 {value: 60 * time.Second, expected: time.Minute}, 52 {value: 2 * time.Minute, expected: 2 * time.Minute}, 53 {value: 59 * time.Minute, expected: 2 * time.Minute}, 54 {value: 61 * time.Minute, expected: 2 * time.Minute}, 55 } 56 for _, test := range tests { 57 t.Run(fmt.Sprintf("test_value_%v", test.value), func(t *testing.T) { 58 result := bufferForPastTimedMetric(limits, test.value) 59 require.Equal(t, test.expected, result) 60 }) 61 } 62 } 63 64 func TestAutoMappingRules(t *testing.T) { 65 ctrl := xtest.NewController(t) 66 defer ctrl.Finish() 67 68 session := client.NewMockSession(ctrl) 69 70 clusters, err := m3.NewClusters( 71 m3.UnaggregatedClusterNamespaceDefinition{ 72 NamespaceID: ident.StringID("default"), 73 Retention: 48 * time.Hour, 74 Session: session, 75 }, m3.AggregatedClusterNamespaceDefinition{ 76 NamespaceID: ident.StringID("2s:1d"), 77 Resolution: 2 * time.Second, 78 Retention: 24 * time.Hour, 79 Session: session, 80 }, m3.AggregatedClusterNamespaceDefinition{ 81 NamespaceID: ident.StringID("4s:2d"), 82 Resolution: 4 * time.Second, 83 Retention: 48 * time.Hour, 84 Session: session, 85 Downsample: &m3.ClusterNamespaceDownsampleOptions{All: false}, 86 }, m3.AggregatedClusterNamespaceDefinition{ 87 NamespaceID: ident.StringID("10s:4d"), 88 Resolution: 10 * time.Second, 89 Retention: 96 * time.Hour, 90 Session: session, 91 ReadOnly: true, 92 }, 93 ) 94 require.NoError(t, err) 95 96 rules, err := NewAutoMappingRules(clusters.ClusterNamespaces()) 97 require.NoError(t, err) 98 99 require.Len(t, rules, 1) 100 require.Equal(t, []AutoMappingRule{ 101 { 102 Aggregations: []aggregation.Type{aggregation.Last}, 103 Policies: policy.StoragePolicies{ 104 policy.NewStoragePolicy(2*time.Second, xtime.Second, 24*time.Hour), 105 }, 106 }, 107 }, rules) 108 }